| | 34 | |
| | 35 | if ( !function_exists('hash_hmac') ): |
| | 36 | function hash_hmac($algo, $data, $key, $raw_output = false) { |
| | 37 | return _hash_hmac($algo, $data, $key, $raw_output); |
| | 38 | } |
| | 39 | endif; |
| | 40 | |
| | 41 | function _hash_hmac($algo, $data, $key, $raw_output = false) { |
| | 42 | $packs = array('md5' => 'H32', 'sha1' => 'H40'); |
| | 43 | |
| | 44 | if ( !isset($packs[$algo]) ) |
| | 45 | return false; |
| | 46 | |
| | 47 | $pack = $packs[$algo]; |
| | 48 | |
| | 49 | if (strlen($key) > 64) |
| | 50 | $key = pack($pack, $algo($key)); |
| | 51 | |
| | 52 | $key = str_pad($key, 64, chr(0)); |
| | 53 | |
| | 54 | $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64)); |
| | 55 | $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64)); |
| | 56 | |
| | 57 | $hmac = $algo($opad . pack($pack, $algo($ipad . $data))); |
| | 58 | |
| | 59 | if ( $raw_output ) |
| | 60 | return pack( $pack, $hmac ); |
| | 61 | return $hmac; |
| | 62 | } |