How to programmatically calculate Chrome extension ID?

后端 未结 3 683
醉话见心
醉话见心 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:36

    Starting with Chrome 64, Chrome changed the package format for extensions to the CRX₃ file format, which supports multiple signatures and explicitly declares its CRX ID. Extracting the CRX ID from a CRX₃ file requires parsing a protocol buffer.

    Here is a small python script for extracting the ID from a CRX₃ file. This solution should only be used with trusted CRX₃ files or in contexts where security is not a concern: unlike CRX₂, the package format does not restrict what CRX ID a CRX₃ file declares. (In practice, consumers of the file (i.e. Chrome) will place restrictions upon it, such as requiring the file to be signed with at least one key that hashes to the declared CRX ID).

    import binascii
    import string
    import struct
    import sys
    
    def decode(proto, data):
        index = 0
        length = len(data)
        msg = dict()
        while index < length:
            item = 128
            key = 0
            left = 0
            while item & 128:
                item = data[index]
                index += 1
                value = (item & 127) << left
                key += value
                left += 7
            field = key >> 3
            wire = key & 7
            if wire == 0:
                item = 128
                num = 0
                left = 0
                while item & 128:
                    item = data[index]
                    index += 1
                    value = (item & 127) << left
                    num += value
                    left += 7
                continue
            elif wire == 1:
                index += 8
                continue
            elif wire == 2:
                item = 128
                _length = 0
                left = 0
                while item & 128:
                    item = data[index]
                    index += 1
                    value = (item & 127) << left
                    _length += value
                    left += 7
                last = index
                index += _length
                item = data[last:index]
                if field not in proto:
                    continue
                msg[proto[field]] = item
                continue
            elif wire == 5:
                index += 4
                continue
            raise ValueError(
                'invalid wire type: {wire}'.format(wire=wire)
            )
        return msg
    
    def get_extension_id(crx_file):
        with open(crx_file, 'rb') as f:
          f.read(8); # 'Cr24\3\0\0\0'
          data = f.read(struct.unpack('

    (Thanks to https://github.com/thelinuxkid/python-protolite for the protobuf parser skeleton.)

提交回复
热议问题