-
Notifications
You must be signed in to change notification settings - Fork 5
/
PrivateKey.class.php
76 lines (68 loc) · 2.52 KB
/
PrivateKey.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
class PrivateKey {
private $keyResource = null;
/**
* Holds a private key so you can sign or decrypt stuff with it, must be cleartext,
* since we need the binary format as well.
* @param string $privateKey
*/
public function __construct($privateKey, $passphrase = '') {
if(!extension_loaded('openssl'))
throw new OpenSSLExtensionNotLoadedException('The openssl module is not loaded.');
$this->keyResource = openssl_pkey_get_private($privateKey, $passphrase);
if($this->keyResource === false)
throw new PrivateKeyDecryptionFailedException(
'Could not decrypt the private key, the passphrase is incorrect, '.
'its contents are mangled or it is not a valid private key.');
}
/**
* Initialize the private key from a file.
* @param string $privatekeyLocation
* @throws FileNotFoundException
* @throws FileNotReadableException
*/
public static function initFromFile($privatekeyLocation, $passphrase) {
if(!file_exists($privatekeyLocation))
throw new FileNotFoundException("The private key file '$privatekeyLocation' does not exist.");
if(!is_readable($privatekeyLocation))
throw new FileNotReadableException("The private key file '$privatekeyLocation' is not readable.");
return new self(file_get_contents($privatekeyLocation), $passphrase);
}
/**
* Signs the data passed in the argument, returns the signature in binary format.
* @param mixed $data The data to be signed
* @param string $algoritm Which algorithm to use for signing
* @return binary
* @throws InvalidMessageDigestAlgorithmException
*/
public function sign($data, $algorithm = 'RSA-SHA256') {
if(!in_array($algorithm, openssl_get_md_methods(true)))
throw new InvalidMessageDigestAlgorithmException(
"The digest algorithm '$algorithm' is not supported by this openssl implementation.");
openssl_sign($data, $signature, $this->keyResource, $algorithm);
return $signature;
}
/**
* Decrypts $data using this private key.
* @param mixed $data
* @return string
* @throws DecryptionFailedException
*/
public function decrypt($data) {
if(!openssl_private_decrypt($data, $decrypted, $this->keyResource))
throw new DecryptionFailedException('Failed decrypting the data with this private key.');
return $decrypted;
}
/**
* Frees the resource associated with this private key.
* This is automatically done on destruct.
*/
private function free() {
if($this->keyResource)
openssl_pkey_free($this->keyResource);
$this->keyResource = null;
}
public function __destruct() {
$this->free();
}
}