11 * |
11 * |
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
14 */ |
14 */ |
15 |
15 |
16 function hmac_gen_padding($val, $len = 32) |
|
17 { |
|
18 $ret = array(); |
|
19 for ( $i = 0; $i < $len; $i++ ) |
|
20 { |
|
21 $ret[] = $val; |
|
22 } |
|
23 return $ret; |
|
24 } |
|
25 |
|
26 function hmac_core($message, $key, $hashfunc) |
16 function hmac_core($message, $key, $hashfunc) |
27 { |
17 { |
28 static $block_sizes = array(); |
18 if ( strlen($key) % 2 == 1 ) |
29 if ( !isset($block_sizes[$hashfunc]) ) |
19 $key .= '0'; |
|
20 |
|
21 if ( strlen($key) > 128 ) |
|
22 $key = $hashfunc($key); |
|
23 |
|
24 while ( strlen($key) < 128 ) |
30 { |
25 { |
31 $block_sizes[$hashfunc] = strlen($hashfunc(''))/2; |
26 $key .= '00'; |
32 } |
27 } |
33 $blocksize = $block_sizes[$hashfunc]; |
28 $opad = hmac_hexbytearray($key); |
34 $ipad = hmac_gen_padding(0x5c, $blocksize); |
29 $ipad = $opad; |
35 $opad = hmac_gen_padding(0x36, $blocksize); |
30 for ( $i = 0; $i < count($ipad); $i++ ) |
36 if ( strlen($key) != ( $blocksize * 2 ) ) |
|
37 $key = $hashfunc($key); |
|
38 $key = hmac_hexbytearray($key); |
|
39 for ( $i = 0; $i < count($key); $i++ ) |
|
40 { |
31 { |
41 $ipad[$i] = $ipad[$i] ^ $key[$i]; |
32 $opad[$i] = $opad[$i] ^ 0x5c; |
42 $opad[$i] = $opad[$i] ^ $key[$i]; |
33 $ipad[$i] = $ipad[$i] ^ 0x36; |
43 } |
34 } |
44 return $hashfunc(hmac_bytearraytostring($opad) . $hashfunc(hmac_bytearraytostring($ipad) . $message)); |
35 $opad = hmac_bytearraytostring($opad); |
|
36 $ipad = hmac_bytearraytostring($ipad); |
|
37 return $hashfunc($opad . hexdecode($hashfunc($ipad . $message))); |
45 } |
38 } |
46 |
39 |
47 function hmac_hexbytearray($val) |
40 function hmac_hexbytearray($val) |
48 { |
41 { |
49 $val = hexdecode($val); |
42 $val = hexdecode($val); |