chinmay.sahoo
New member
The Crypt_HMAC class implements the algorithm as described in RFC 2104 and can be installed with pear install crypt_hmac. Let’s look at it:
class Crypt_HMAC {
/**
* Constructor
* Pass method as first parameter
*
* @param string method - Hash function used for the calculation
* @return void
* @access public
*/
function Crypt_HMAC($key, $method = 'md5')
{
if (!in_array($method, array('sha1', 'md5'))) {
die("Unsupported hash function '$method'.");
}
$this->_func = $method;
/* Pad the key as the RFC wishes (step 1) */
if (strlen($key) > 64) {
$key = pack('H32', $method($key));
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
/* Calculate the padded keys and save them (step 2 & 3) */
$this->_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36),
➥64);
$this->_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C),
➥64);
}