+function hmac_md5($data, $key='') {
+ // Creates a HMAC digest that can be used for auth purposes
+ // See RFCs 2104, 2617, 2831
+ // Uses mhash() extension if available
+ if (extension_loaded('mhash')) {
+ if ($key== '') {
+ $mhash=mhash(MHASH_MD5,$data);
+ } else {
+ $mhash=mhash(MHASH_MD5,$data,$key);
+ }
+ return $mhash;
+ }
+ if (!$key) {
+ return pack('H*',md5($data));
+ }
+ $key = str_pad($key,64,chr(0x00));
+ if (strlen($key) > 64) {
+ $key = pack("H*",md5($key));
+ }
+ $k_ipad = $key ^ str_repeat(chr(0x36), 64) ;
+ $k_opad = $key ^ str_repeat(chr(0x5c), 64) ;
+ /* Heh, let's get recursive. */
+ $hmac=hmac_md5($k_opad . pack("H*",md5($k_ipad . $data)) );
+ return $hmac;
+}
+