note 27225 added to function.mhash
| From: | lance_rushing at hot* dot php dot net | Date: | Thu, 28 Nov 2002 02:36:25 +0000 |
| Subject: | note 27225 added to function.mhash | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-40058@lists.php.net to get a copy of this message | ||
Want to Create a md5 HMAC, but don't have hmash installed?
Use this:
function hmac ($key, $data)
{
// RFC 2104 HMAC implementation for php.
// Creates an md5 HMAC.
// Eliminates the need to install mhash to compute a HMAC
// Hacked by Lance Rushing
$b = 64; // byte length for md5
if (strlen($key) > $b) {
$key = pack("H*",md5($key));
}
$key = str_pad($key, $b, chr(0x00));
$ipad = str_pad('', $b, chr(0x36));
$opad = str_pad('', $b, chr(0x5c));
$k_ipad = $key ^ $ipad ;
$k_opad = $key ^ $opad;
return md5($k_opad . pack("H*",md5($k_ipad . $data)));
}
-----
To test:
Run this on a server _with_ mhash installed:
$key = 'Jefe';
$data = "what do ya want for nothing?";
echo hmac($key, $data);
echo "<br>\n";
echo bin2hex (mhash(MHASH_MD5, $data, $key));
should produce:
750c783e6ab0b503eaa86e310a5db738
750c783e6ab0b503eaa86e310a5db738
Happy hashing.
--
http://www.php.net/manual/en/function.mhash.php
http://master.php.net/manage/user-notes.php?action=edit+27225
http://master.php.net/manage/user-notes.php?action=delete+27225
http://master.php.net/manage/user-notes.php?action=reject+27225