How to programmatically calculate Chrome extension id?

I am creating an automated process for creating extensions. Is there any code sample to calculate the extension identifier directly and completely bypassing the interaction with the browser?

(I answer my question below).

+6
source share
2 answers

I managed to find a related article with a Ruby snippet, and it is available only in IA: http://web.archive.org/web/20120606044635/http://supercollider.dk/2010/01/calculating-chrome-extension-id- from-your-private-key-233

It's important to know:

  • It depends on the DER (public binary) public key, and not on the PEM-encoded key (good ASCII generated by the base64-encoded DER key encoding).
  • The extension identifiers are base-16, but are encoded using [ap] (called "mpdecimal"), not [0-9a-f].

Using the PEM public key, follow these steps:

  • If your PEM format public key still has a header and footer and is split into several lines, reformat it manually so that you have one character string that excludes the header and footer and works together so that each key binding line to the next.
  • Base64 decodes the public key to render the DER public key.
  • Create a SHA256 hex digest of the key in DER format.
  • Take the first 32 bytes of the hash. You will not need the rest.
  • For each character, convert it to base-10 and add the ASCII code for 'a'.

The following is the Python procedure for this:

import hashlib from base64 import b64decode def build_id(pub_key_pem): pub_key_der = b64decode(pub_key_pem) sha = hashlib.sha256(pub_key_der).hexdigest() prefix = sha[:32] reencoded = "" ord_a = ord('a') for old_char in prefix: code = int(old_char, 16) new_char = chr(ord_a + code) reencoded += new_char return reencoded def main(): pub_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjvF5pjuK8gRaw/2LoRYi37QqRd48B/FeO9yFtT6ueY84z/u0NrJ/xbPFc9OCGBi8RKIblVvcbY0ySGqdmp0QsUr/oXN0b06GL4iB8rMhlO082HhMzrClV8OKRJ+eJNhNBl8viwmtJs3MN0x9ljA4HQLaAPBA9a14IUKLjP0pWuwIDAQAB' id_ = build_id(pub_key) print(id_) if __name__ == '__main__': main() 

You can more than test this against the existing extension and its identifier. To get a public PEM-formatted key:

  • Go to the list of existing extensions in Chrome. Take the extension id.
  • Locate the directory where the extension is located. In my Windows 7 window this is: C: \ Users \\ AppData \ Local \ Google \ Chrome \ User Data \ Default \ Extensions \
  • Take the public key from the manifest.json file in the "key" section. Since the key is already ready for base64 decoding, you can skip step (1) of the process.

The public key in this example refers to the Chrome Reader extension. Its extension identifier is "lojpenhmoajbiciapkjkiekmobleogjc".

See also:

+7
source

A good and easy way to get the public key from a .crx file using python, since chrome only generates the .pem private key for you. The public key is actually stored in the .crx file.

This is based on the .crx file format found here http://developer.chrome.com/extensions/crx.html

 import struct import hashlib import string def get_pub_key_from_crx(crx_file): with open(crx_file, 'rb') as f: data = f.read() header = struct.unpack('<4sIII', data[:16]) pubkey = struct.unpack('<%ds' % header[2], data[16:16+header[2]])[0] return pubkey def get_extension_id(crx_file): pubkey = get_pub_key_from_crx(crx_file) digest = hashlib.sha256(pubkey).hexdigest() trans = string.maketrans('0123456789abcdef', string.ascii_lowercase[:16]) return string.translate(digest[:32], trans) if __name__ == '__main__': import sys if len(sys.argv) != 2: print 'usage: %s crx_file' % sys.argv[0] print get_extension_id(sys.argv[1]) 

Although this cannot be done "bypassing the interaction with the browser", because you still need to generate the .crx file with a command like

 chrome.exe --pack-extension=my_extension --pack-extension-key=my_extension.pem 
+3
source

Source: https://habr.com/ru/post/946822/


All Articles