note 39492 added to function.sha1
| From: | mark at dot BANSPAM dot pronexus dot nl | Date: | Fri, 30 Jan 2004 11:28:08 +0000 |
| Subject: | note 39492 added to function.sha1 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64428@lists.php.net to get a copy of this message | ||
Looking for a simple function to implement HMAC-SHA1 but don't want to use the entire PEAR
Message lib?
//Calculate HMAC-SHA1 according to RFC2104
// http://www.ietf.org/rfc/rfc2104.txt
function hmacsha1($key,$data) {
$blocksize=64;
$hashfunc='sha1';
if (strlen($key)>$blocksize)
$key=pack('H*', $hashfunc($key));
$key=str_pad($key,$blocksize,chr(0x00));
$ipad=str_repeat(chr(0x36),$blocksize);
$opad=str_repeat(chr(0x5c),$blocksize);
$hmac = pack(
'H*',$hashfunc(
($key^$opad).pack(
'H*',$hashfunc(
($key^$ipad).$data
)
)
)
);
return bin2hex($hmac);
}
It is very useful for client-authentication. see also http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf
Optionally you can change $hashfunc to 'md5' to make this an HMAC-MD5 function ;-)
If you want raw or base64 output instead of hexadecimal, just change the last return line.
Cheers,
Mark
p.s. the "$hmac =" line used to be 1 line but I had to cut it up in order to fit it here
;)
----
Manual Page -- http://www.php.net/manual/en/function.sha1.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39492
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39492&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39492&report=yes
Search -- http://master.php.net/manage/user-notes.php