如何以编程方式计算Chrome扩展程序ID? [英] How to programmatically calculate Chrome extension ID?
问题描述
我正在构建一个自动化过程来生成扩展。是否有代码示例直接计算扩展ID并完全绕过与浏览器的交互?
(我正在回答我自己的问题,下面。)
我只能找到一个带有Ruby片段的相关文章,并且仅在IA中可用: http://web.archive.org/web/20120606044635/http://supercollider.dk/2010/01/calculating-chrome-extension-id-from-your-private-key-233
重要提示:
- 这取决于DER编码的公钥(原始二进制),而不是PEM编码的密钥(由base64编码DER密钥生成的漂亮ASCII)。
- 扩展ID为基数为16,但使用[ap]编码mpdecimal),而不是[0-9a-f]。
使用PEM编码公共密钥,请按照以下步骤操作:
- 如果您的PEM格式的公共密钥仍具有页眉和页脚并被分成多行,手动重新格式化它,以便您有一个排除页眉和页脚的单个字符串,并且一起运行,以使每一行的密钥都包含到下一个。
- Base64解码使用DER格式的公钥生成公钥。
- 生成DER格式化密钥的SHA256十六进制摘要。前32个字节的散列。您将不需要其余的。
- 对于每个字符,将其转换为base-10,并为a添加ASCII代码。 >
以下是执行此操作的Python例程:
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()
前缀= sha [:32]
reencoded =
ord_a = ord('a')
前缀中的old_char:
code = int(old_char, 16)
new_char = chr(ord_a + code)
reencoded + = new_char
返回重新编码
def main():
pub_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCjvF5pjuK8gRaw / 2LoRYi37QqRd48B / FeO9yFtT6ueY84z / u0NrJ / xbPFc9OCGBi8RKIblVvcbY0ySGqdmp0QsUr / oXN0b06GL4iB8rMhlO082HhMzrClV8OKRJ + eJNhNBl8viwmtJs3MN0x9ljA4HQLaAPBA9a14IUKLjP0pWuwIDAQAB'
ID_ = build_id(pub_key)
PR int(id_)
if __name__ =='__main__':
main()
欢迎您对现有扩展程序及其ID进行测试。检索其PEM格式的公钥:
- 进入Chrome中现有扩展的列表。获取其中一个的扩展名。
- 查找托管扩展的目录。在我的Windows 7盒子上,它是:C:\ Users \\AppData\Local\Google\Chrome\User Data\Default\Extensions\
- 从key下的manifest.json文件中获取公钥。由于密钥已经准备好进行base64解码,所以您可以跳过该流程的步骤(1)。
公钥在这个例子中来自Chrome Reader扩展。它的扩展ID是lojpenhmoajbiciapkjkiekmobleogjc。
另见:
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 browser?
(I'm answering my own question, below.)
解决方案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:
- This depends on a DER-encoded public key (raw binary), not a PEM-encoded key (nice ASCII generated by base64-encoding the DER key).
- 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:
- 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.
- Base64-decode the public key to render a DER-formatted public-key.
- Generate a SHA256 hex-digest of the DER-formatted key.
- 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 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:
- Go into the list of your existing extensions in Chrome. Grab the extension-ID of one.
- 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\
- 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:
- Google Chrome - Alphanumeric hashes to identify extensions
- http://blog.roomanna.com/12-14-2010/getting-an-extensions-id
这篇关于如何以编程方式计算Chrome扩展程序ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!