openssl signing
[pharext/pharext] / src / pharext / Openssl / PrivateKey.php
1 <?php
2
3 namespace pharext\Openssl;
4
5 class PrivateKey
6 {
7 private $key;
8
9 function __construct($file, $password) {
10 $this->key = openssl_pkey_get_private("file://$file", $password);
11 if (!is_resource($this->key)) {
12 throw new \Exception("Could not load private key");
13 }
14 }
15
16 function sign(\Phar $package) {
17 $package->setSignatureAlgorithm(\Phar::OPENSSL, $this->key);
18 }
19
20 function exportPublicKey($file) {
21 if (!file_put_contents("$file.tmp", openssl_pkey_get_details($this->key)["key"])
22 || !rename("$file.tmp", $file)
23 ) {
24 throw new \Exception(error_get_last()["message"]);
25 }
26 }
27 }