6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Code from http://phpseclib.sourceforge.net/
22 * Modified by phpBB Group to meet our coding standards
23 * and being able to integrate into phpBB
25 * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
27 * Copyright 2007-2009 TerraFrost <terrafrost@php.net>
28 * Copyright 2009+ phpBB
31 * @author TerraFrost <terrafrost@php.net>
36 * @see hash::__construct()
39 * Toggles the internal implementation
41 define('CRYPT_HASH_MODE_INTERNAL', 1);
43 * Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+.
45 define('CRYPT_HASH_MODE_MHASH', 2);
47 * Toggles the hash() implementation, which works on PHP 5.1.2+.
49 define('CRYPT_HASH_MODE_HASH', 3);
53 * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
55 * @author Jim Wigginton <terrafrost@php.net>
63 * Byte-length of compression blocks / key (Internal HMAC)
65 * @see hash::set_algorithm()
72 * Byte-length of hash output (Internal HMAC)
74 * @see hash::set_hash()
83 * @see hash::set_hash()
99 * Outer XOR (Internal HMAC)
101 * @see hash::setKey()
108 * Inner XOR (Internal HMAC)
110 * @see hash::setKey()
117 * Default Constructor.
119 * @param optional String $hash
123 function __construct($hash = 'sha1')
125 if ( !defined('CRYPT_HASH_MODE') )
129 case extension_loaded('hash'):
130 define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_HASH
);
132 case extension_loaded('mhash'):
133 define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_MHASH
);
136 define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL
);
140 $this->set_hash($hash);
144 * Sets the key for HMACs
146 * Keys can be of any length.
151 function set_key($key)
157 * Sets the hash function.
160 * @param String $hash
162 function set_hash($hash)
168 $this->l
= 12; // 96 / 8 = 12
177 switch (CRYPT_HASH_MODE
)
179 case CRYPT_HASH_MODE_MHASH
:
184 $this->hash
= MHASH_MD5
;
189 $this->hash
= MHASH_SHA1
;
192 case CRYPT_HASH_MODE_HASH
:
202 $this->hash
= 'sha1';
218 $this->hash
= 'sha1';
221 $this->ipad
= str_repeat(chr(0x36), $this->b
);
222 $this->opad
= str_repeat(chr(0x5C), $this->b
);
229 * @param String $text
231 function create($text)
233 if (!empty($this->key
))
235 switch (CRYPT_HASH_MODE
)
237 case CRYPT_HASH_MODE_MHASH
:
238 $output = mhash($this->hash
, $text, $this->key
);
240 case CRYPT_HASH_MODE_HASH
:
241 $output = hash_hmac($this->hash
, $text, $this->key
, true);
243 case CRYPT_HASH_MODE_INTERNAL
:
245 /* "Applications that use keys longer than B bytes will first hash the key using H and then use the
246 resultant L byte string as the actual key to HMAC."
248 -- http://tools.ietf.org/html/rfc2104#section-2 */
249 $key = strlen($this->key
) > $this->b ?
$hash($this->key
) : $this->key
;
251 $key = str_pad($key, $this->b
, chr(0));// step 1
252 $temp = $this->ipad ^
$key; // step 2
253 $temp .= $text; // step 3
254 $temp = pack('H*', $hash($temp)); // step 4
255 $output = $this->opad ^
$key; // step 5
256 $output.= $temp; // step 6
257 $output = pack('H*', $hash($output)); // step 7
262 switch (CRYPT_HASH_MODE
)
264 case CRYPT_HASH_MODE_MHASH
:
265 $output = mhash($this->hash
, $text);
267 case CRYPT_HASH_MODE_MHASH
:
268 $output = hash($this->hash
, $text, true);
270 case CRYPT_HASH_MODE_INTERNAL
:
272 $output = pack('H*', $hash($output));
276 return substr($output, 0, $this->l
);