3 namespace pharext\Openssl
;
24 * @param string $password
25 * @throws \pharext\Exception
27 function __construct($file, $password) {
28 /* there appears to be a bug with refcount handling of this
29 * resource; when the resource is stored as property, it cannot be
30 * "coerced to a private key" on openssl_sign() later in another method
32 $key = openssl_pkey_get_private("file://$file", $password);
33 if (!is_resource($key)) {
34 throw new Exception("Could not load private key");
36 openssl_pkey_export($key, $this->key
);
37 $this->pub
= openssl_pkey_get_details($key)["key"];
42 * @param \Phar $package
44 function sign(\Phar
$package) {
45 $package->setSignatureAlgorithm(\Phar
::OPENSSL
, $this->key
);
49 * Export the public key to a file
51 * @throws \pharext\Exception
53 function exportPublicKey($file) {
54 if (!file_put_contents("$file.tmp", $this->pub
) ||
!rename("$file.tmp", $file)) {