openssl signing
[pharext/pharext] / src / pharext / Openssl / PrivateKey.php
diff --git a/src/pharext/Openssl/PrivateKey.php b/src/pharext/Openssl/PrivateKey.php
new file mode 100644 (file)
index 0000000..1d3aed1
--- /dev/null
@@ -0,0 +1,27 @@
+<?php
+
+namespace pharext\Openssl;
+
+class PrivateKey
+{
+       private $key;
+       
+       function __construct($file, $password) {
+               $this->key = openssl_pkey_get_private("file://$file", $password);
+               if (!is_resource($this->key)) {
+                       throw new \Exception("Could not load private key");
+               }
+       }
+       
+       function sign(\Phar $package) {
+               $package->setSignatureAlgorithm(\Phar::OPENSSL, $this->key);
+       }
+       
+       function exportPublicKey($file) {
+               if (!file_put_contents("$file.tmp", openssl_pkey_get_details($this->key)["key"])
+               ||      !rename("$file.tmp", $file)
+               ) {
+                       throw new \Exception(error_get_last()["message"]);
+               }
+       }
+}