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