kylehuff / buildcrx

Standalone binary for Windows/Linux/Mac to pack signed chrome extension without needing the chrome binary
Other
12 stars 2 forks source link

Print out AppID #7

Open nmoinvaz opened 10 years ago

nmoinvaz commented 10 years ago

It would be great if the tool could also print out the extension's app id. Here is some code that I have scratched together real quick to do just that.

bool hexencode(char* buf, int size, char *target, int maxtarget) {
    static const char hexChars[] = "0123456789ABCDEF";
    int i = 0;
    for (i = 0; i < size; ++i) {
        target[(i * 2)] = hexChars[(buf[i] >> 4) & 0xf];
        target[(i * 2) + 1] = hexChars[buf[i] & 0xf];
    }
    return true;
}

void hextoidalpha(char* id, int idsize) {
    char x[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
    for (size_t i = 0; i < idsize; ++i) {
        id[i] = x[id[i]-'0'] + 'a';
    }
}

void generate_appid(char *buf, int size, char out[SHA256_DIGEST_LENGTH]) {
    SHA256_CTX sha;
    byte digest[SHA256_DIGEST_LENGTH];
    char hexDigest[SHA256_DIGEST_LENGTH * 2] = {0};
    SHA256_Init(&sha);
    SHA256_Update (&sha, buf, size);
    SHA256_Final(digest, &sha);
    hexencode((char *)digest, SHA256_DIGEST_LENGTH, hexDigest, SHA256_DIGEST_LENGTH*2);
    hextoidalpha(hexDigest, SHA256_DIGEST_LENGTH);
    strncpy(out, hexDigest, SHA256_DIGEST_LENGTH);
}

Then in main function after you convert the RSA PublicKey to DER format

    // Print the appid
    char appid[SHA256_DIGEST_LENGTH] = {0};
    generate_appid((char *)derkey,derlen,appid);
    printf("appid: %.32s\n", appid);
nmoinvaz commented 10 years ago

Optionally you could have it automatically dump to a file which can be useful for build purposes.

char appid_filename[320];
strncpy(appid_filename, output_filename, 320);
strncat(appid_filename, ".appid", 320);
FILE *appidfile = fopen(appid_filename,"wb");
if (appidfile) {
    fwrite(appid, 1, SHA256_DIGEST_LENGTH, appidfile);
    fclose(appidfile);
}
kylehuff commented 10 years ago

That's a good idea, I will see slate it for the next release (maybe a few weeks, I'm in the process of moving)

kylehuff commented 10 years ago

I've tagged this for v0.3 (projected release date of 27MAR14)