note 110288 added to function.hash-hmac
| From: | pete dot walker at NOSPAM dot me dot com | Date: | Mon, 08 Oct 2012 14:13:03 +0000 |
| Subject: | note 110288 added to function.hash-hmac | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191186@lists.php.net to get a copy of this message | ||
A function implementing the algorithm outlined in RFC 6238 (http://tools.ietf.org/html/rfc6238)
<?php
/**
* This function implements the algorithm outlined
* in RFC 6238 for Time-Based One-Time Passwords
*
* @link http://tools.ietf.org/html/rfc6238
* @param string $key the string to use for the HMAC key
* @param mixed $time a value that reflects a time (unix
* time in the example)
* @param int $digits the desired length of the OTP
* @param string $crypto the desired HMAC crypto algorithm
* @return string the generated OTP
*/
function oauth_totp($key, $time, $digits=8, $crypto='sha256')
{
$digits = intval($digits);
$result = null;
// Convert counter to binary (64-bit)
$data = pack('NNC*', $time >> 32, $time & 0xFFFFFFFF);
// Pad to 8 chars (if necessary)
if (strlen ($data) < 8) {
$data = str_pad($data, 8, chr(0), STR_PAD_LEFT);
}
// Get the hash
$hash = hash_hmac($crypto, $data, $key);
// Grab the offset
$offset = 2 * hexdec(substr($hash, strlen($hash) - 1, 1));
// Grab the portion we're interested in
$binary = hexdec(substr($hash, $offset, 8)) & 0x7fffffff;
// Modulus
$result = $binary % pow(10, $digits);
// Pad (if necessary)
$result = str_pad($result, $digits, "0", STR_PAD_LEFT);
return $result;
}
?>
----
Server IP: 94.76.225.145
Probable Submitter: 188.92.232.203
----
Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php
Edit -- https://master.php.net/note/edit/110288
Del: integrated -- https://master.php.net/note/delete/110288/integrated
Del: useless -- https://master.php.net/note/delete/110288/useless
Del: bad code -- https://master.php.net/note/delete/110288/bad+code
Del: spam -- https://master.php.net/note/delete/110288/spam
Del: non-english -- https://master.php.net/note/delete/110288/non-english
Del: in docs -- https://master.php.net/note/delete/110288/in+docs
Del: other reasons-- https://master.php.net/note/delete/110288
Reject -- https://master.php.net/note/reject/110288
Search -- https://master.php.net/manage/user-notes.php