note 89769 added to function.hash-hmac
| From: | torben dot egmose at gmail dot com | Date: | Sun, 22 Mar 2009 19:40:43 +0000 |
| Subject: | note 89769 added to function.hash-hmac | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152306@lists.php.net to get a copy of this message | ||
HOTP Algorithm that works according to the RCF http://tools.ietf.org/html/draft-mraihi-oath-hmac-otp-04
The test cases from the RCF document the ASCII string as "123456787901234567890".
But the hex decoded to a string is "12345678901234567890".
Secret="12345678901234567890";
Count:
0 755224
1 287082
<?php
function oath_hotp($key,$counter) {
// Convert to padded binary string
$data = pack ('C*', $counter);
$data = str_pad($data,8,chr(0),STR_PAD_LEFT);
// HMAC
return hash_hmac('sha1',$data,$key);
}
function oath_truncate($hash, $length = 6) {
// Convert to dec
foreach(str_split($hash,2) as $hex) {
$hmac_result[]=hexdec($hex);
}
// Find offset
$offset = $hmac_result[19] & 0xf;
// Algorithm from RFC
return (
(($hmac_result[$offset+0] & 0x7f) << 24 ) |
(($hmac_result[$offset+1] & 0xff) << 16 ) |
(($hmac_result[$offset+2] & 0xff) << 8 ) |
($hmac_result[$offset+3] & 0xff)
) % pow(10,$length);
}
print "<pre>";
print "Compare results with:"
print " http://tools.ietf.org/html/draft-mraihi-oath-hmac-otp-04\n";
print "Count\tHash\t\t\t\t\t\tPin\n";
for($i=0;$i<10;$i++)
print $i."\t".($a=oath_hotp("12345678901234567890",$i))
print "\t".oath_truncate($a)."\n";
----
Server IP: 195.184.96.15
Probable Submitter: 83.93.59.34
----
Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php
Edit -- https://master.php.net/note/edit/89769
Del: integrated -- https://master.php.net/note/delete/89769/integrated
Del: useless -- https://master.php.net/note/delete/89769/useless
Del: bad code -- https://master.php.net/note/delete/89769/bad+code
Del: spam -- https://master.php.net/note/delete/89769/spam
Del: non-english -- https://master.php.net/note/delete/89769/non-english
Del: in docs -- https://master.php.net/note/delete/89769/in+docs
Del: other reasons-- https://master.php.net/note/delete/89769
Reject -- https://master.php.net/note/reject/89769
Search -- https://master.php.net/manage/user-notes.php