1 | <?php |
||
33 | final class OpensslKey |
||
34 | { |
||
35 | /** |
||
36 | * High entropy key. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $_key; |
||
41 | |||
42 | /** |
||
43 | * Algo string. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $_algo; |
||
48 | |||
49 | /** |
||
50 | * High entropy salt. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $_iv; |
||
55 | |||
56 | /** |
||
57 | * Name of cipher. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $_cipher; |
||
62 | |||
63 | /** |
||
64 | * OpensslKey constructor. |
||
65 | * |
||
66 | * @param string $key Key to use for encryption |
||
67 | * @param string $algo Algo to use for HKDF |
||
68 | * @param string $cipher Name of cipher |
||
69 | * @param string $iv Initialization vector |
||
70 | * |
||
71 | * @throws InvalidKeyException |
||
72 | */ |
||
73 | 48 | public function __construct( |
|
74 | string $key, |
||
75 | string $algo, |
||
76 | string $cipher = '', |
||
77 | string $iv = '' |
||
78 | ) { |
||
79 | // Store the key as what was supplied |
||
80 | 48 | $this->_key = \base64_decode($key, true); |
|
81 | |||
82 | // If key was not proper base64, bail out |
||
83 | 48 | if ($this->_key === false) { |
|
84 | 9 | throw new InvalidKeyException(InvalidKeyException::BASE64ENC); |
|
85 | } |
||
86 | |||
87 | // If key was to short, bail out |
||
88 | 39 | if (Str::strlen($this->_key) < 32) { |
|
89 | throw new InvalidKeyException(InvalidKeyException::KEYLENGTH); |
||
90 | } |
||
91 | |||
92 | // Store algo in object |
||
93 | 39 | $this->_algo = $algo; |
|
94 | |||
95 | // Store init vector in object |
||
96 | 39 | $this->_iv = $iv; |
|
97 | |||
98 | // Store the cipher name |
||
99 | 39 | $this->_cipher = $cipher; |
|
100 | 39 | } |
|
101 | |||
102 | /** |
||
103 | * Generate the authentication key. |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 36 | public function authenticationKey(): string |
|
111 | |||
112 | /** |
||
113 | * Generate the encryption key. |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 37 | public function encryptionKey(): string |
|
121 | |||
122 | /** |
||
123 | * Derive a key with differing info string parameters. |
||
124 | * |
||
125 | * @param string $info Info parameter to provide to hash_hkdf |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | 39 | public function deriveKey(string $info): string |
|
133 | |||
134 | /** |
||
135 | * Calculates a given message HMAC. |
||
136 | * |
||
137 | * @param string $message |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 36 | public function messageChecksum(string $message): string |
|
145 | |||
146 | /** |
||
147 | * Allows secure read only access to private properties. |
||
148 | * |
||
149 | * @param string $name |
||
150 | * |
||
151 | * @throws Exception |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | 37 | public function __get(string $name) |
|
163 | |||
164 | /** |
||
165 | * Generate a new key. |
||
166 | * |
||
167 | * @param int $bytes Size of key in bytes |
||
168 | * |
||
169 | * @throws InvalidKeyException |
||
170 | * @throws Exception |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | 31 | public static function create(int $bytes = 32): string |
|
182 | } |
||
183 |