1 | <?php declare(strict_types=1); |
||
28 | final class OpensslKey |
||
29 | { |
||
30 | /** |
||
31 | * High entropy key |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $_key; |
||
36 | |||
37 | /** |
||
38 | * Algo string |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $_algo; |
||
43 | |||
44 | /** |
||
45 | * High entropy salt |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $_ivr; |
||
50 | |||
51 | /** |
||
52 | * OpensslKey constructor. |
||
53 | * |
||
54 | * @param string $algo Algo to use for HKDF |
||
55 | * @param string $key Key |
||
56 | * @param string $ivr Initialization vector |
||
57 | * @param bool $testKey Validate the key |
||
58 | * |
||
59 | * @throws InvalidKeyException |
||
60 | */ |
||
61 | 44 | public function __construct( |
|
88 | |||
89 | /** |
||
90 | * Generate the authentication key |
||
91 | * |
||
92 | * @param string $info The extra info parameter for hash_hkdf |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 33 | public function authenticationKey(string $info): string |
|
100 | |||
101 | /** |
||
102 | * Generate the encryption key |
||
103 | * |
||
104 | * @param string $info The extra info parameter for hash_hkdf |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 34 | public function encryptionKey(string $info): string |
|
112 | |||
113 | /** |
||
114 | * Derive a key with differing info string parameters |
||
115 | * |
||
116 | * @param string $info Info parameter to provide to hash_hkdf |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 36 | public function deriveKey(string $info): string |
|
124 | |||
125 | /** |
||
126 | * Generate a new key that meets requirements for dcrypt |
||
127 | * |
||
128 | * @param int $bytes Size of key in bytes |
||
129 | * |
||
130 | * @return string |
||
131 | * @throws InvalidKeyException |
||
132 | */ |
||
133 | 29 | public static function create(int $bytes = 2048): string |
|
141 | |||
142 | /** |
||
143 | * Returns true if key has enough entropy |
||
144 | * |
||
145 | * @param string $key Key string to test |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | 28 | private static function _testKeyEntropy(string $key): bool |
|
153 | } |