1 | <?php |
||
32 | final class OpensslKey |
||
33 | { |
||
34 | /** |
||
35 | * High entropy key. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $_key; |
||
40 | |||
41 | /** |
||
42 | * Algo string. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $_algo; |
||
47 | |||
48 | /** |
||
49 | * High entropy salt. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $_ivr; |
||
54 | |||
55 | /** |
||
56 | * OpensslKey constructor. |
||
57 | * |
||
58 | * @param string $algo Algo to use for HKDF |
||
59 | * @param string $key Key |
||
60 | * @param string $ivr Initialization vector |
||
61 | * @param bool $testKey Validate the key |
||
62 | * |
||
63 | * @throws InvalidKeyException |
||
64 | */ |
||
65 | 44 | public function __construct( |
|
92 | |||
93 | /** |
||
94 | * Generate the authentication key. |
||
95 | * |
||
96 | * @param string $info The extra info parameter for hash_hkdf |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 33 | public function authenticationKey(string $info): string |
|
104 | |||
105 | /** |
||
106 | * Generate the encryption key. |
||
107 | * |
||
108 | * @param string $info The extra info parameter for hash_hkdf |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 34 | public function encryptionKey(string $info): string |
|
116 | |||
117 | /** |
||
118 | * Derive a key with differing info string parameters. |
||
119 | * |
||
120 | * @param string $info Info parameter to provide to hash_hkdf |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 36 | public function deriveKey(string $info): string |
|
128 | |||
129 | /** |
||
130 | * Generate a new key that meets requirements for dcrypt. |
||
131 | * |
||
132 | * @param int $bytes Size of key in bytes |
||
133 | * |
||
134 | * @throws InvalidKeyException |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 29 | public static function create(int $bytes = 2048): string |
|
146 | |||
147 | /** |
||
148 | * Returns true if key has enough entropy. |
||
149 | * |
||
150 | * @param string $key Key string to test |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | 28 | private static function _testKeyEntropy(string $key): bool |
|
158 | } |
||
159 |