note 84223 added to function.hash-hmac
| From: | (caverett*@*corecodec,net) | Date: | Thu, 03 Jul 2008 22:54:52 +0000 |
| Subject: | note 84223 added to function.hash-hmac | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141154@lists.php.net to get a copy of this message | ||
Generating OATH-compliant OTP (one time passwords) results in PHP:
<?php
$otp = oath_truncate (oath_hotp ($key, $counter), $length);
function oath_hotp ($key, $counter) {
// Counter
$bin_counter = pack ('C*', $counter);
// Pad to 8 chars
if (strlen ($bin_counter) < 8) {
$bin_counter = str_repeat (chr(0), 8 - strlen ($bin_counter)) . $bin_counter;
}
// HMAC
$hash = hash_hmac ('sha1', $bin_counter, $key);
return $hash;
}
function oath_truncate ($hash, $length = 6) {
// The last byte is used as an offset
$offset = hexdec (substr ($hash, 38)) & 0xf;
// Extract the relevant part, and clear the first bit
$hex_truncated = substr ($hash, $offset * 2, 8);
$bin_truncated = decbin (hexdec ($hex_truncated));
$bin_truncated[0] = '0';
$dec_truncated = bindec ($bin_truncated);
return substr ($dec_truncated, 0 - $length);
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 140.247.241.185
----
Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php
Edit -- https://master.php.net/note/edit/84223
Del: integrated -- https://master.php.net/note/delete/84223/integrated
Del: useless -- https://master.php.net/note/delete/84223/useless
Del: bad code -- https://master.php.net/note/delete/84223/bad+code
Del: spam -- https://master.php.net/note/delete/84223/spam
Del: non-english -- https://master.php.net/note/delete/84223/non-english
Del: in docs -- https://master.php.net/note/delete/84223/in+docs
Del: other reasons-- https://master.php.net/note/delete/84223
Reject -- https://master.php.net/note/reject/84223
Search -- https://master.php.net/manage/user-notes.php