12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
13 */ |
13 */ |
14 |
14 |
15 function hmac_core($message, $key, $hashfunc) |
15 function hmac_core($message, $key, $hashfunc) |
16 { |
16 { |
17 if ( strlen($key) % 2 == 1 ) |
17 if ( strlen($key) % 2 == 1 ) |
18 $key .= '0'; |
18 $key .= '0'; |
19 |
19 |
20 if ( strlen($key) > 128 ) |
20 if ( strlen($key) > 128 ) |
21 $key = $hashfunc($key); |
21 $key = $hashfunc($key); |
22 |
22 |
23 while ( strlen($key) < 128 ) |
23 while ( strlen($key) < 128 ) |
24 { |
24 { |
25 $key .= '00'; |
25 $key .= '00'; |
26 } |
26 } |
27 $opad = hmac_hexbytearray($key); |
27 $opad = hmac_hexbytearray($key); |
28 $ipad = $opad; |
28 $ipad = $opad; |
29 for ( $i = 0; $i < count($ipad); $i++ ) |
29 for ( $i = 0; $i < count($ipad); $i++ ) |
30 { |
30 { |
31 $opad[$i] = $opad[$i] ^ 0x5c; |
31 $opad[$i] = $opad[$i] ^ 0x5c; |
32 $ipad[$i] = $ipad[$i] ^ 0x36; |
32 $ipad[$i] = $ipad[$i] ^ 0x36; |
33 } |
33 } |
34 $opad = hmac_bytearraytostring($opad); |
34 $opad = hmac_bytearraytostring($opad); |
35 $ipad = hmac_bytearraytostring($ipad); |
35 $ipad = hmac_bytearraytostring($ipad); |
36 return $hashfunc($opad . hexdecode($hashfunc($ipad . $message))); |
36 return $hashfunc($opad . hexdecode($hashfunc($ipad . $message))); |
37 } |
37 } |
38 |
38 |
39 function hmac_hexbytearray($val) |
39 function hmac_hexbytearray($val) |
40 { |
40 { |
41 $val = hexdecode($val); |
41 $val = hexdecode($val); |
42 return hmac_bytearray($val); |
42 return hmac_bytearray($val); |
43 } |
43 } |
44 |
44 |
45 function hmac_bytearray($val) |
45 function hmac_bytearray($val) |
46 { |
46 { |
47 $val = str_split($val, 1); |
47 $val = str_split($val, 1); |
48 foreach ( $val as &$char ) |
48 foreach ( $val as &$char ) |
49 { |
49 { |
50 $char = ord($char); |
50 $char = ord($char); |
51 } |
51 } |
52 return $val; |
52 return $val; |
53 } |
53 } |
54 |
54 |
55 function hmac_bytearraytostring($val) |
55 function hmac_bytearraytostring($val) |
56 { |
56 { |
57 foreach ( $val as &$char ) |
57 foreach ( $val as &$char ) |
58 { |
58 { |
59 $char = chr($char); |
59 $char = chr($char); |
60 } |
60 } |
61 return implode('', $val); |
61 return implode('', $val); |
62 } |
62 } |
63 |
63 |
64 function hmac_md5($message, $key) |
64 function hmac_md5($message, $key) |
65 { |
65 { |
66 return hmac_core($message, $key, 'md5'); |
66 return hmac_core($message, $key, 'md5'); |
67 } |
67 } |
68 |
68 |
69 function hmac_sha1($message, $key) |
69 function hmac_sha1($message, $key) |
70 { |
70 { |
71 return hmac_core($message, $key, 'sha1'); |
71 return hmac_core($message, $key, 'sha1'); |
72 } |
72 } |
73 |
73 |
74 function hmac_sha256($message, $key) |
74 function hmac_sha256($message, $key) |
75 { |
75 { |
76 require_once(ENANO_ROOT . '/includes/math.php'); |
76 require_once(ENANO_ROOT . '/includes/math.php'); |
77 require_once(ENANO_ROOT . '/includes/diffiehellman.php'); |
77 require_once(ENANO_ROOT . '/includes/diffiehellman.php'); |
78 return hmac_core($message, $key, 'sha256'); |
78 return hmac_core($message, $key, 'sha256'); |
79 } |
79 } |
80 |
80 |
81 ?> |
81 ?> |