簡體   English   中英

從PHP生成Chrome .crx

[英]Generate Chrome .crx from PHP

我想從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);

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM