你使用的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);