首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从PHP生成Chrome .crx

从PHP生成Chrome .crx
EN

Stack Overflow用户
提问于 2012-12-16 14:08:56
回答 1查看 1.1K关注 0票数 6

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

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

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

我如何生成一个键盘并使用它来用PHP签署一个铬.crx包?

此代码失败(CRX_SIGNATURE_VERIFICATION_INITALIZATION_FAILED):

代码语言:javascript
复制
// 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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-17 12:30:50

你用错了openssl_pkey_export而且你还没有删除

代码语言:javascript
复制
-----BEGIN PUBLIC KEY-----
...
-----END PUBLIC KEY-----

在解码前从公钥字符串中删除。我通过查看公钥和签名的长度就知道了这一点。第一个应该是161个,第二个应该是128个字节长(来源):

代码语言:javascript
复制
A2 00 00 00   # 162 -- length of public key in bytes
80 00 00 00   # 128 -- length of signature in bytes

以下是固定代码(PHP 5.4):

代码语言:javascript
复制
$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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13902082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档