* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
* Uses hash() or mhash() if available and an internal implementation, otherwise. Currently supports the following:
* md2, md5, md5-96, sha1, sha1-96, sha256, sha384, and sha512
* If {@link Crypt_Hash::setKey() setKey()} is called, {@link Crypt_Hash::hash() hash()} will return the HMAC as opposed to
* the hash. If no valid algorithm is provided, sha1 will be used.
* {@internal The variable names are the same as those in
* {@link http://tools.ietf.org/html/rfc2104#section-2 RFC2104}.}}
* Here's a short example of how to use this library:
* include 'Crypt/Hash.php';
* $hash = new Crypt_Hash('sha1');
* $hash->setKey('abcdefg');
* echo base64_encode($hash->hash('abcdefg'));
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVII Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
* @see Crypt_Hash::Crypt_Hash()
* Toggles the internal implementation
define('CRYPT_HASH_MODE_INTERNAL', 1);
* Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+.
define('CRYPT_HASH_MODE_MHASH', 2);
* Toggles the hash() implementation, which works on PHP 5.1.2+.
define('CRYPT_HASH_MODE_HASH', 3);
* Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
* @author Jim Wigginton <terrafrost@php.net>
* @see Crypt_Hash::setHash()
* Byte-length of compression blocks / key (Internal HMAC)
* @see Crypt_Hash::setAlgorithm()
* Byte-length of hash output (Internal HMAC)
* @see Crypt_Hash::setHash()
* @see Crypt_Hash::setHash()
* @see Crypt_Hash::setKey()
* Outer XOR (Internal HMAC)
* @see Crypt_Hash::setKey()
* Inner XOR (Internal HMAC)
* @see Crypt_Hash::setKey()
* @param optional String $hash
public function __construct($hash = 'sha1')
if (!defined('CRYPT_HASH_MODE')) {
case extension_loaded('hash'):
define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_HASH);
case extension_loaded('mhash'):
define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_MHASH);
define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL);
* Keys can be of any length.
* @param optional String $key
public function setKey($key = false)
* Gets the hash function.
* As set by the constructor or by the setHash() method.
public function getHash()
* Sets the hash function.
public function setHash($hash)
$this->hashParam = $hash = strtolower($hash);
$this->l = 12; // 96 / 8 = 12
$mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_HASH && in_array('md2', hash_algos()) ?
CRYPT_HASH_MODE_HASH : CRYPT_HASH_MODE_INTERNAL;
$mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_MHASH ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE;
case CRYPT_HASH_MODE_MHASH:
$this->hash = MHASH_SHA256;
$this->hash = MHASH_SHA1;
case CRYPT_HASH_MODE_HASH:
$this->hash = array($this, '_md2');
$this->hash = array($this, '_md5');
$this->hash = array($this, '_sha256');
$this->hash = array($this, '_sha512');
$this->hash = array($this, '_sha1');
$this->ipad = str_repeat(chr(0x36), $this->b);
$this->opad = str_repeat(chr(0x5C), $this->b);
public function hash($text)
$mode = is_array($this->hash) ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE;
if (!empty($this->key) || is_string($this->key)) {
case CRYPT_HASH_MODE_MHASH:
$output = mhash($this->hash, $text, $this->key);
case CRYPT_HASH_MODE_HASH:
$output = hash_hmac($this->hash, $text, $this->key, true);
case CRYPT_HASH_MODE_INTERNAL:
/* "Applications that use keys longer than B bytes will first hash the key using H and then use the
resultant L byte string as the actual key to HMAC."
-- http://tools.ietf.org/html/rfc2104#section-2 */
$key = strlen($this->key) > $this->b ? call_user_func($this->hash, $this->key) : $this->key;
$key = str_pad($key, $this->b, chr(0)); // step 1
$temp = $this->ipad ^ $key; // step 2
$temp .= $text; // step 3
$temp = call_user_func($this->hash, $temp); // step 4
$output = $this->opad ^ $key; // step 5
$output .= $temp; // step 6
$output = call_user_func($this->hash, $output); // step 7
case CRYPT_HASH_MODE_MHASH:
$output = mhash($this->hash, $text);
case CRYPT_HASH_MODE_HASH:
$output = hash($this->hash, $text, true);
case CRYPT_HASH_MODE_INTERNAL:
$output = call_user_func($this->hash, $text);
return substr($output, 0, $this->l);
* Returns the hash length (in bytes)
public function getLength()
return pack('H*', md5($m));
public function _sha1($m)
return pack('H*', sha1($m));
* Pure-PHP implementation of MD2
* See {@link http://tools.ietf.org/html/rfc1319 RFC1319}.
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
31, 26, 219, 153, 141, 51, 159, 17, 131, 20,
// Step 1. Append Padding Bytes
$pad = 16 - (strlen($m) & 0xF);
$m .= str_repeat(chr($pad), $pad);
// Step 2. Append Checksum
$c = str_repeat(chr(0), 16);
for ($i = 0; $i < $length; $i += 16) {
for ($j = 0; $j < 16; $j++) {
// RFC1319 incorrectly states that C[j] should be set to S[c xor L]
//$c[$j] = chr($s[ord($m[$i + $j] ^ $l)]);
// per <http://www.rfc-editor.org/errata_search.php?rfc=1319>, however, C[j] should be set to S[c xor L] xor C[j]
$c[$j] = chr($s[ord($m[$i + $j] ^ $l)] ^ ord($c[$j]));
// Step 3. Initialize MD Buffer
$x = str_repeat(chr(0), 48);
// Step 4. Process Message in 16-Byte Blocks
for ($i = 0; $i < $length; $i += 16) {
for ($j = 0; $j < 16; $j++) {
$x[$j + 16] = $m[$i + $j];
$x[$j + 32] = $x[$j + 16] ^ $x[$j];
for ($j = 0; $j < 18; $j++) {
for ($k = 0; $k < 48; $k++) {
$x[$k] = $t = $x[$k] ^ chr($s[ord($t)]);
//$t = $x[$k] = $x[$k] ^ chr($s[ord($t)]);
return substr($x, 0, 16);
* Pure-PHP implementation of SHA256
* See {@link http://en.wikipedia.org/wiki/SHA_hash_functions#SHA-256_.28a_SHA-2_variant.29_pseudocode SHA-256 (a SHA-2 variant) pseudocode - Wikipedia}.
public function _sha256($m)
if (extension_loaded('suhosin')) {
return pack('H*', sha256($m));
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
// Initialize table of round constants
// (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)