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.