@@ 211-226 (lines=16) @@ | ||
208 | * @param bool $throw Whether or not to throw an exception on error. |
|
209 | * @return string Returns the encrypted string. |
|
210 | */ |
|
211 | protected function encrypt($str, $method, $password, $iv = '', $throw = false) { |
|
212 | if ($iv === '') { |
|
213 | $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($method)); |
|
214 | } |
|
215 | // Encrypt the string. |
|
216 | $encrypted = openssl_encrypt($str, $method, $password, OPENSSL_RAW_DATA, $iv); |
|
217 | ||
218 | if ($encrypted === false) { |
|
219 | return $this->exception($throw, "Error encrypting the string.", 400); |
|
220 | } |
|
221 | ||
222 | $str = static::base64urlEncode($encrypted); |
|
223 | $this->pushString($str, static::base64urlEncode($iv)); |
|
224 | ||
225 | return $str; |
|
226 | } |
|
227 | ||
228 | /** |
|
229 | * Decrypt a string with {@link openssl_decrypt()}. |
|
@@ 286-303 (lines=18) @@ | ||
283 | * @param bool $throw Whether or not the throw an exception on error. |
|
284 | * @return string Returns the string with signing information or null on error. |
|
285 | */ |
|
286 | protected function hmac($str, $method, $password, $timestamp = 0, $throw = false) { |
|
287 | if ($timestamp === 0) { |
|
288 | $timestamp = time(); |
|
289 | } |
|
290 | // Add the timestamp to the string. |
|
291 | static::pushString($str, $timestamp); |
|
292 | ||
293 | // Sign the string. |
|
294 | $signature = hash_hmac($method, $str, $password, true); |
|
295 | if ($signature === false) { |
|
296 | return $this->exception($throw, "Invalid hash method $method.", 400); |
|
297 | } |
|
298 | ||
299 | // Add the signature to the string. |
|
300 | static::pushString($str, static::base64urlEncode($signature)); |
|
301 | ||
302 | return $str; |
|
303 | } |
|
304 | ||
305 | /** |
|
306 | * Verify the signature on a secure string. |