Create Google Chrome Crx file with PHP

馋奶兔 提交于 2019-11-28 19:56:56
Jonathon Hill

This ruby code was helpful.

Your public key must be in DER format, and unfortunately PHP's OpenSSL extension can't do that, so far as I can tell. I had to generate it from my private key at the command line:

openssl rsa -pubout -outform DER < extension_private_key.pem > extension_public_key.pub

UPDATE: there is a PHP der2pem() function available here, thanks to tutuDajuju for pointing it out.

Once that's done, building the .crx file is quite easy:

# make a SHA1 signature using our private key
$pk = openssl_pkey_get_private(file_get_contents('extension_private_key.pem'));
openssl_sign(file_get_contents('extension.zip'), $signature, $pk, 'sha1');
openssl_free_key($pk);

# decode the public key
$key = base64_decode(file_get_contents('extension_public_key.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($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('extension.zip')); // package contents, zipped
fclose($fh);

The CRX format is described in detail on the documentation page: http://code.google.com/chrome/extensions/crx.html

There are examples on the end of that file for Ruby and Bash. Follow the format in your language (PHP).

You can use the working PHP solution: https://github.com/andyps/crxbuild There are a PHP class that you can include in your project and command line script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!