gpt4 book ai didi

php - 从 PHP 生成 Chrome .crx

转载 作者:可可西里 更新时间:2023-11-01 13:28:27 25 4
gpt4 key购买 nike

我想从 PHP 生成一个 Chrome 扩展(Chrome 主题)。我的 PHP 脚本生成一个 zip 文件 (download.zip)。要将其转换为 .crx 包,需要添加 header ,包括公钥和签名。

我看到了this答案,但您需要一个生成 .pub 文件的 .pem 文件。我在共享主机上,所以 exec() 将不起作用(将 .pem 转换为 .pub)。无需 .pem 文件,只需下载一次即可使用(无需更新)。

然后我看到this说明您可以生成私钥和公钥的注释。组合这两个脚本是行不通的(请参阅代码)。

我如何生成一个 key 对并使用它通过 PHP 对 chrome .crx 包进行签名?

此代码失败 (CRX_SIGNATURE_VERIFICATION_INITALIZATION_FAILED):

// Create the keypair
$res=openssl_pkey_new();

// Get private key
openssl_pkey_export($res, $pk);

// Get public key
$key=openssl_pkey_get_details($res);
$key=$key["key"];

# make a SHA1 signature using our private key
openssl_sign(file_get_contents('download.zip'), $signature, $pk, 'sha1');

# decode the public key
$key = base64_decode($key);

# .crx package format:
#
# magic number char(4)
# crx format ver byte(4)
# pub key lenth byte(4)
# signature length byte(4)
# public key string
# signature string
# package contents, zipped string
#
# see http://code.google.com/chrome/extensions/crx.html
#
$fh = fopen('extension.crx', 'wb');
fwrite($fh, 'Cr24'); // extension file magic number
fwrite($fh, pack('V', 2)); // crx format version
fwrite($fh, pack('V', strlen($key))); // public key length
fwrite($fh, pack('V', strlen($signature))); // signature length
fwrite($fh, $key); // public key
fwrite($fh, $signature); // signature
fwrite($fh, file_get_contents('download.zip')); // package contents, zipped
fclose($fh);

最佳答案

您使用的 openssl_pkey_export 有误,您还没有删除

-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----

在解码之前从公钥字符串中获取。我通过查看公钥和签名的长度来解决这个问题。第一个应该是 161,第二个应该是 128 字节长(source):

A2 00 00 00   # 162 -- length of public key in bytes
80 00 00 00 # 128 -- length of signature in bytes

这是固定代码(PHP 5.4):

$pk=file_get_contents('pk.pem');

$priv = openssl_pkey_get_private($pk);
$pub = openssl_pkey_get_details($priv)['key'];

# make a SHA1 signature using our private key
openssl_sign(file_get_contents('download.zip'), $signature, $priv, OPENSSL_ALGO_SHA1);

# geting rid of -----BEGIN/END PUBLIC KEY-----
# you can probably do it better using preg_match_all / explode(PHP_EOL, $pub) etc.
$pub = trim(explode('-----',$pub)[2]);

# decode the public key
$pub = base64_decode($pub);

# .crx package format:
#
# magic number char(4)
# crx format ver byte(4)
# pub key lenth byte(4)
# signature length byte(4)
# public key string
# signature string
# package contents, zipped string
#
# see http://code.google.com/chrome/extensions/crx.html
#
$fh = fopen('extension.crx', 'wb');
fwrite($fh, 'Cr24'); // extension file magic number
fwrite($fh, pack('V', 2)); // crx format version
fwrite($fh, pack('V', strlen($pub))); // public key length
fwrite($fh, pack('V', strlen($signature))); // signature length
fwrite($fh, $pub); // public key
fwrite($fh, $signature); // signature
fwrite($fh, file_get_contents('download.zip')); // package contents, zipped
fclose($fh);

关于php - 从 PHP 生成 Chrome .crx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com