Last active
October 1, 2022 18:34
-
-
Save markuszeller/d1e635d75cd3865d0f2823b672467a96 to your computer and use it in GitHub Desktop.
Timed one time password
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 | |
$digits = 6; | |
$interval = 30; | |
$secret = 'secret_key'; | |
$prevTotp = ''; | |
for ($i = 0; $i < $interval * 4; $i++) { | |
$time = time(); | |
$counter = (string) floor($time / $interval); | |
$hmac = hash_hmac('sha1', $counter, $secret); | |
$offset = hexdec(substr($hmac, -1, 1)); | |
$otp = hexdec(substr($hmac, $offset * 2, 8)); | |
$totp = sprintf('%06d', $otp % 10 ** $digits); | |
if ($totp !== $prevTotp) { | |
$prevTotp = $totp; | |
echo PHP_EOL; | |
} | |
printf("\r\e[2KTime: %d\tCounter: %d\tTOTP: %0{$digits}d", $time, $counter, $totp); | |
sleep(1); | |
} | |
echo PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment