-
-
Save Zegnat/c22fea3c368217df2620654d90b7ca30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function create_signed_code($key, $message, $ttl = 31536000, $appended_data = '') | |
{ | |
$expires = time() + $ttl; | |
$body = $message . $expires . $appended_data; | |
$signature = hash_hmac('sha256', $body, $key); | |
return dechex($expires) . ':' . $signature . ':' . $appended_data; | |
} | |
function verify_signed_code($key, $message, $code) | |
{ | |
$code_parts = explode(':', $code, 3); | |
if (count($code_parts) !== 3) return false; | |
$expires = hexdec($code_parts[0]); | |
if (time() > $expires) return false; | |
$body = $message . $expires . $code_parts[2]; | |
$signature = hash_hmac('sha256', $body, $key); | |
return $signature === $code_parts[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment