note 93440 added to function.hash-hmac
| From: | KCCloyd at osu1 dot php dot net | Date: | Thu, 10 Sep 2009 06:16:30 +0000 |
| Subject: | note 93440 added to function.hash-hmac | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160396@lists.php.net to get a copy of this message | ||
Sometimes a hosting provider doesn't provide access to the Hash extension. Here is a clone of
the hash_hmac function you can use in the event you need an HMAC generator and Hash is not
available. It's only usable with MD5 and SHA1 encryption algorithms, but its output is
identical to the official hash_hmac function (so far at least).
<?php
function custom_hmac($algo, $data, $key, $raw_output = false)
{
$algo = strtolower($algo);
$pack = 'H'.strlen($algo('test'));
$size = 64;
$opad = str_repeat(chr(0x5C), $size);
$ipad = str_repeat(chr(0x36), $size);
if (strlen($key) > $size) {
$key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));
} else {
$key = str_pad($key, $size, chr(0x00));
}
for ($i = 0; $i < strlen($key) - 1; $i++) {
$opad[$i] = $opad[$i] ^ $key[$i];
$ipad[$i] = $ipad[$i] ^ $key[$i];
}
$output = $algo($opad.pack($pack, $algo($ipad.$data)));
return ($raw_output) ? pack($pack, $output) : $output;
}
?>
Example Use:
<?php
custom_hmac('sha1', 'Hello, world!', 'secret', true);
?>
----
Server IP: 208.69.120.35
Probable Submitter: 76.115.133.105
----
Manual Page -- http://www.php.net/manual/en/function.hash-hmac.php
Edit -- https://master.php.net/note/edit/93440
Del: integrated -- https://master.php.net/note/delete/93440/integrated
Del: useless -- https://master.php.net/note/delete/93440/useless
Del: bad code -- https://master.php.net/note/delete/93440/bad+code
Del: spam -- https://master.php.net/note/delete/93440/spam
Del: non-english -- https://master.php.net/note/delete/93440/non-english
Del: in docs -- https://master.php.net/note/delete/93440/in+docs
Del: other reasons-- https://master.php.net/note/delete/93440
Reject -- https://master.php.net/note/reject/93440
Search -- https://master.php.net/manage/user-notes.php