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