Skip to content

Instantly share code, notes, and snippets.

@altmind
Created March 13, 2012 07:57
Show Gist options
  • Save altmind/2027518 to your computer and use it in GitHub Desktop.
Save altmind/2027518 to your computer and use it in GitHub Desktop.
<?php
define('BLOCKSIZE', 64);
function hex2bin($v)
{
return pack("H*" , $v);
}
function sha1bin($text)
{
return hex2bin(sha1($text));
}
function HMAC($key, $text)
{
if (strlen($key) > BLOCKSIZE) {
$key = sha1bin($key);
}
$key = str_pad($key, BLOCKSIZE, chr(0x00));
$ipad = str_repeat(chr(0x36), BLOCKSIZE);
$opad = str_repeat(chr(0x5c), BLOCKSIZE);
$hash1 = sha1bin(($key ^ $ipad) . $text);
$hmac = sha1bin(($key ^ $opad) . $hash1);
return $hmac;
}
$err = 0;
if (bin2hex(HMAC("key","The quick brown fox jumps over the lazy dog"))!="de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9")
{
print("ERROR 1");
$err=1;
}
if (bin2hex(HMAC("keykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykeykey","The quick brown fox jumps over the lazy dog"))!="de647975b3a4e8c38481845dea2177b85dc2eea9")
{
print("ERROR 2");
$err=1;
}
if (bin2hex(HMAC(".","."))!="b2abe377fbdea168617388e371adc07834bec21b")
{
print("ERROR 3");
$err=1;
}
if (bin2hex(HMAC("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111","111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"))!="e211905a427db58fae8635b725b8e1fd7e628cc4")
{
print("ERROR 4");
$err=1;
}
if ($err==1)
{
exit(1);
}
else
{
print "OK";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment