diff options
Diffstat (limited to 'laravel/serializable-closure/src/Signers/Hmac.php')
-rw-r--r-- | laravel/serializable-closure/src/Signers/Hmac.php | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/laravel/serializable-closure/src/Signers/Hmac.php b/laravel/serializable-closure/src/Signers/Hmac.php new file mode 100644 index 00000000..d94b0a2a --- /dev/null +++ b/laravel/serializable-closure/src/Signers/Hmac.php @@ -0,0 +1,53 @@ +<?php + +namespace Laravel\SerializableClosure\Signers; + +use Laravel\SerializableClosure\Contracts\Signer; + +class Hmac implements Signer +{ + /** + * The secret key. + * + * @var string + */ + protected $secret; + + /** + * Creates a new signer instance. + * + * @param string $secret + * @return void + */ + public function __construct($secret) + { + $this->secret = $secret; + } + + /** + * Sign the given serializable. + * + * @param string $serialized + * @return array + */ + public function sign($serialized) + { + return [ + 'serializable' => $serialized, + 'hash' => base64_encode(hash_hmac('sha256', $serialized, $this->secret, true)), + ]; + } + + /** + * Verify the given signature. + * + * @param array $signature + * @return bool + */ + public function verify($signature) + { + return hash_equals(base64_encode( + hash_hmac('sha256', $signature['serializable'], $this->secret, true) + ), $signature['hash']); + } +} |