该页面翻译自 Google Chrome Extensions 与 Google Chrome Apps。除非特别说明,该页面的内容遵循 Creative Commons Attribution 3.0 License,代码示例遵循 BSD License。
CRX 文件是具有特殊头信息的 ZIP 文件,并且文件扩展名为 .crx。
头信息包含作者的公共密钥和扩展程序的签名,签名以
SHA-1 算法使用作者的私有密钥从 ZIP
文件生成。头信息要求字节顺序为小端序并以 4
字节对齐。下表按顺序描述 .crx的头信息:
| 字段 | 类型 | 长度 | 值 | 描述 |
|---|---|---|---|---|
| magic number | char[] | 32 位 | Cr24 |
Chrome 要求每一个 .crx 包的开头包含此常量。
|
| version | unsigned int | 32 位 | 2 | *.crx 文件格式的版本(当前为2)。 |
| public key length | unsigned int | 32 位 | pubkey.length | RSA 公共密钥的长度,以字节为单位。 |
| signature length | unsigned int | 32 位 | sig.length | 签名的长度,以字节为单位 |
| public key | byte[] | pubkey.length | pubkey.contents | 作者的 RSA 公共密钥内容,以 X509 SubjectPublicKeyInfo 块的格式表示。 |
| signature | byte[] | sig.length | sig.contents | ZIP 内容使用作者私有密钥的签名,该签名使用 RSA 算法以及 SHA-1 散列函数创建。 |
扩展程序的 ZIP 文件附加到 *.crx
包的头信息之后,这应该和生成头信息中的签名使用同一个 ZIP 文件。
以下是一个 .crx 文件开头内容的十六进制表示:
43 72 32 34 # "Cr24" -- the magic number 02 00 00 00 # 2 -- the crx format version number A2 00 00 00 # 162 -- length of public key in bytes 80 00 00 00 # 128 -- length of signature in bytes ........... # the contents of the public key ........... # the contents of the signature ........... # the contents of the zip file
社区成员编写了如下脚本来产生 .crx 文件。
github: crxmake
#!/bin/bash -e
#
# Purpose: Pack a Chromium extension directory into crx format
if test $# -ne 2; then
echo "Usage: crxmake.sh <extension dir> <pem path>"
exit 1
fi
dir=$1
key=$2
name=$(basename "$dir")
crx="$name.crx"
pub="$name.pub"
sig="$name.sig"
zip="$name.zip"
trap 'rm -f "$pub" "$sig" "$zip"' EXIT
# zip up the crx dir
cwd=$(pwd -P)
(cd "$dir" && zip -qr -9 -X "$cwd/$zip" .)
# signature
openssl sha1 -sha1 -binary -sign "$key" < "$zip" > "$sig"
# public key
openssl rsa -pubout -outform DER < "$key" > "$pub" 2>/dev/null
byte_swap () {
# Take "abcdefgh" and return it as "ghefcdab"
echo "${1:6:2}${1:4:2}${1:2:2}${1:0:2}"
}
crmagic_hex="4372 3234" # Cr24
version_hex="0200 0000" # 2
pub_len_hex=$(byte_swap $(printf '%08x\n' $(ls -l "$pub" | awk '{print $5}')))
sig_len_hex=$(byte_swap $(printf '%08x\n' $(ls -l "$sig" | awk '{print $5}')))
(
echo "$crmagic_hex $version_hex $pub_len_hex $sig_len_hex" | xxd -r -p
cat "$pub" "$sig" "$zip"
) > "$crx"
echo "Wrote $crx"