我想从PHP生成一个Chrome扩展(Chrome主题)。我的PHP脚本生成一个zip文件(download.zip)。要将其转换为.crx包,它需要添加头部,包括公钥和签名。
我看到了这的答案,但您需要一个生成.pub文件的.pem文件。我在共享主机上,所以exec()不能工作(将.pem转换为.pub)。没有必要有一个.pem文件,它只需要下载一次就可以使用它(不需要更新)。
然后,我看到了这评论,说明您可以生成私钥和公钥。组合这两个脚本是行不通的(请参阅代码)。
我如何生成一个键盘并使用它来用PHP签署一个铬.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);发布于 2012-12-17 12:30:50
你用错了openssl_pkey_export而且你还没有删除
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----在解码前从公钥字符串中删除。我通过查看公钥和签名的长度就知道了这一点。第一个应该是161个,第二个应该是128个字节长(来源):
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);https://stackoverflow.com/questions/13902082
复制相似问题