How to programmatically calculate Chrome extension ID?

后端 未结 3 656
醉话见心
醉话见心 2020-12-29 14:08

I\'m building an automated process to produce extensions. Is there a code example of calculating the extension-ID directly and entirely bypassing interaction with the browse

3条回答
  •  伪装坚强ぢ
    2020-12-29 14:46

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

    Important to know:

    1. This depends on a DER-encoded public key (raw binary), not a PEM-encoded key (nice ASCII generated by base64-encoding the DER key).
    2. The extension-IDs are base-16, but are encoded using [a-p] (called "mpdecimal"), rather than [0-9a-f].

    Using a PEM-encoded public key, follow the following steps:

    1. If your PEM-formatted public-key still has the header and footer and is split into multiple lines, reformat it by hand so that you have a single string of characters that excludes the header and footer, and runs together such that every line of the key wraps to the next.
    2. Base64-decode the public key to render a DER-formatted public-key.
    3. Generate a SHA256 hex-digest of the DER-formatted key.
    4. Take the first 32-bytes of the hash. You will not need the rest.
    5. For each character, convert it to base-10, and add the ASCII code for 'a'.

    The following is a Python routine to do 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're more than welcome to test this against an existing extension and its ID. To retrieve its PEM-formatted public-key:

    1. Go into the list of your existing extensions in Chrome. Grab the extension-ID of one.
    2. Find the directory where the extension is hosted. On my Windows 7 box, it is: C:\Users\AppData\Local\Google\Chrome\User Data\Default\Extensions
    3. Grab the public-key from the manifest.json file under "key". Since the key is already ready to be base64-decoded, you can skip step (1) of the process.

    The public-key in the example is from the "Chrome Reader" extension. Its extension ID is "lojpenhmoajbiciapkjkiekmobleogjc".

    See also:

    1. Google Chrome - Alphanumeric hashes to identify extensions
    2. http://blog.roomanna.com/12-14-2010/getting-an-extensions-id

提交回复
热议问题