1 | <?php |
||
35 | final class OpensslKey |
||
36 | { |
||
37 | /** |
||
38 | * High entropy key. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $_key; |
||
43 | |||
44 | /** |
||
45 | * Algo string. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $_algo; |
||
50 | |||
51 | /** |
||
52 | * High entropy salt. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $_iv; |
||
57 | |||
58 | /** |
||
59 | * Name of cipher. |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $_cipher; |
||
64 | |||
65 | /** |
||
66 | * OpensslKey constructor. |
||
67 | * |
||
68 | * @param string $key Key to use for encryption |
||
69 | * @param string $algo Algo to use for HKDF |
||
70 | * @param string $cipher Name of cipher |
||
71 | * @param string $iv Initialization vector |
||
72 | * |
||
73 | * @throws InvalidKeyLengthException |
||
74 | * @throws InvalidKeyEncodingException |
||
75 | */ |
||
76 | 49 | public function __construct( |
|
104 | |||
105 | /** |
||
106 | * Generate the authentication key. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | 36 | public function authenticationKey(): string |
|
114 | |||
115 | /** |
||
116 | * Generate the encryption key. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 37 | public function encryptionKey(): string |
|
124 | |||
125 | /** |
||
126 | * Derive a key with differing info string parameters. |
||
127 | * |
||
128 | * @param string $info Info parameter to provide to hash_hkdf |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 39 | public function deriveKey(string $info): string |
|
136 | |||
137 | /** |
||
138 | * Calculates a given message HMAC. |
||
139 | * |
||
140 | * @param string $message |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | 36 | public function messageChecksum(string $message): string |
|
148 | |||
149 | /** |
||
150 | * Allows secure read only access to private properties. |
||
151 | * |
||
152 | * @param string $name |
||
153 | * |
||
154 | * @throws Exception |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | 38 | public function __get(string $name): string |
|
166 | |||
167 | /** |
||
168 | * Generate a new key. |
||
169 | * |
||
170 | * @param int $bytes Size of key in bytes |
||
171 | * |
||
172 | * @throws Exception |
||
173 | * @throws InvalidKeyLengthException |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | 32 | public static function create(int $bytes = 32): string |
|
185 | } |
||
186 |