4

Possible Duplicate:
Google Chrome - Alphanumeric hashes to identify extensions

I'm building a Chrome extension packager, and am trying to figure out how to programmatically generate the appID from the package contents.

The appID is a 32-byte string consisting of lower-case letters, like these:

extension page screenshot

According to the Chrome extension documentation, the appID is "generated based on a hash of the extension's public key," and is used to uniquely identify an extension.

Since I'd like to be able to package an extension without using the Chrome GUI, and the public key is already included in the package contents, can anyone tell me how these are generated?

2
  • 1
    Curious... why are you packaging it as a *.crx instead of just uploading it to the Chrome webstore? Commented Oct 9, 2011 at 10:13
  • Because I want to be able to self-host for automated deployment. Commented Oct 9, 2011 at 10:22

2 Answers 2

6

It is SHA256 of the public key encoded into string in a special way:

http://codesearch.google.com/#OAMlx_jo-ck/src/chrome/browser/extensions/extension_service.cc&exact_package=chromium&q=Extension::GenerateId&type=cs&l=1200

http://codesearch.google.com/#OAMlx_jo-ck/src/chrome/common/extensions/extension.cc&exact_package=chromium&q=GenerateId&type=cs&l=375

1
3

I've got a write up with Ruby example code:

Chrome Extension developer Erik Kay explains the format on Stack Overflow:

To be precise, it’s the first 128 bits of the SHA256 of an RSA public key encoded in base 16. Another random bit of trivia is that the encoding uses a-p instead of 0-9a-f. The reason is that leading numeric characters in the host field of an origin can wind up being treated as potential IP addresses by Chrome. We refer to it internally as “mpdecimal” after the guy who came up with it.

Here’s a short Ruby script to do exactly this:

require "openssl"
require "digest/sha2"

def pkey_to_id(pkey)
  # Key algorithm, found in <http://github.com/Constellation/crxmake>.
  algo = %w(30 81 9F 30 0D 06 09 2A 86 48 86 F7 0D 01 01 01 05 00 03 81 8D 00).map{ |s| s.hex }.pack("C*")
  # Calculate public key, get hex hash of first 128 bits / 32 characters
  hash = Digest::SHA256.hexdigest(algo + OpenSSL::PKey::RSA.new(pkey).public_key.to_der)[0...32]
  # Shift hex from 0-9a-f to a-p
  hash.unpack("C*").map{ |c| c < 97 ? c + 49 : c + 10 }.pack("C*")
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.