Bar*_*aan 6 php openssl public-key-encryption google-chrome-extension
我想从PHP生成Chrome扩展程序(Chrome主题).我的PHP脚本生成一个zip文件(download.zip).要将其转换为.crx包,需要添加标头,包括公钥和签名.
我看到了这个答案,但你需要一个生成.pub文件的.pem文件.我在共享主机上,因此exec()将无法工作(将.pem转换为.pub).不需要.pem文件,只需在下载后使用它(无需更新).
然后我看到这条评论说明你可以生成私钥和公钥.组合这两个脚本将不起作用(请参阅代码).
如何生成密钥对并使用它来签署带有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);
Run Code Online (Sandbox Code Playgroud)
你用openssl_pkey_export错了而且还没有删除
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----
Run Code Online (Sandbox Code Playgroud)
在解码之前从公钥字符串中获取。我通过查看公钥和签名的长度发现了这一点。第一个应该是 161,第二个应该是 128 字节长(来源):
A2 00 00 00 # 162 -- length of public key in bytes
80 00 00 00 # 128 -- length of signature in bytes
Run Code Online (Sandbox Code Playgroud)
这是固定代码(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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |