142 if (!defined(
'CRYPT_HASH_MODE')) {
144 case extension_loaded(
'hash'):
145 define(
'CRYPT_HASH_MODE', self::MODE_HASH);
147 case extension_loaded(
'mhash'):
148 define(
'CRYPT_HASH_MODE', self::MODE_MHASH);
151 define(
'CRYPT_HASH_MODE', self::MODE_INTERNAL);
192 $this->hashParam = $hash = strtolower($hash);
198 $hash = substr($hash, 0, -3);
220 $mode = CRYPT_HASH_MODE == self::MODE_HASH && in_array(
'md2', hash_algos()) ?
221 self::MODE_HASH : self::MODE_INTERNAL;
225 $mode = CRYPT_HASH_MODE == self::MODE_MHASH ? self::MODE_INTERNAL : CRYPT_HASH_MODE;
228 $mode = CRYPT_HASH_MODE;
232 case self::MODE_MHASH:
235 $this->
hash = MHASH_MD5;
238 $this->
hash = MHASH_SHA256;
242 $this->
hash = MHASH_SHA1;
245 case self::MODE_HASH:
258 $this->
hash =
'sha1';
266 $this->
hash = array($this,
'_md2');
270 $this->
hash = array($this,
'_md5');
274 $this->
hash = array($this,
'_sha256');
279 $this->
hash = array($this,
'_sha512');
284 $this->
hash = array($this,
'_sha1');
287 $this->ipad = str_repeat(chr(0x36), $this->b);
288 $this->opad = str_repeat(chr(0x5C), $this->b);
300 $mode = is_array($this->
hash) ? self::MODE_INTERNAL : CRYPT_HASH_MODE;
302 if (!empty($this->key) || is_string($this->key)) {
304 case self::MODE_MHASH:
307 case self::MODE_HASH:
310 case self::MODE_INTERNAL:
315 $key = strlen($this->key) > $this->b ? call_user_func($this->
hash, $this->key) :
$this->key;
317 $key = str_pad($key, $this->b, chr(0));
318 $temp = $this->ipad ^
$key;
320 $temp = call_user_func($this->
hash, $temp);
327 case self::MODE_MHASH:
330 case self::MODE_HASH:
333 case self::MODE_INTERNAL:
338 return substr(
$output, 0, $this->l);
360 return pack(
'H*', md5(
$m));
371 return pack(
'H*', sha1(
$m));
385 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
386 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
387 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
388 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
389 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
390 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
391 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
392 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
393 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
394 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
395 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
396 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
397 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
398 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
399 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
400 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
401 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
402 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
406 $pad = 16 - (strlen(
$m) & 0xF);
407 $m.= str_repeat(chr($pad), $pad);
409 $length = strlen(
$m);
412 $c = str_repeat(chr(0), 16);
414 for (
$i = 0;
$i < $length;
$i+= 16) {
415 for ($j = 0; $j < 16; $j++) {
419 $c[$j] = chr(
$s[ord(
$m[
$i + $j] ^ $l)] ^ ord(
$c[$j]));
428 $x = str_repeat(chr(0), 48);
431 for (
$i = 0;
$i < $length;
$i+= 16) {
432 for ($j = 0; $j < 16; $j++) {
433 $x[$j + 16] =
$m[
$i + $j];
434 $x[$j + 32] =
$x[$j + 16] ^
$x[$j];
437 for ($j = 0; $j < 18; $j++) {
438 for ($k = 0; $k < 48; $k++) {
442 $t = chr(ord(
$t) + $j);
447 return substr(
$x, 0, 16);
460 if (extension_loaded(
'suhosin')) {
461 return pack(
'H*', sha256(
$m));
466 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
471 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
472 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
473 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
474 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
475 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
476 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
477 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
478 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
482 $length = strlen(
$m);
484 $m.= str_repeat(chr(0), 64 - (($length + 8) & 0x3F));
485 $m[$length] = chr(0x80);
487 $m.= pack(
'N2', 0, $length << 3);
490 $chunks = str_split(
$m, 64);
491 foreach ($chunks as $chunk) {
493 for (
$i = 0;
$i < 16;
$i++) {
499 for (
$i = 16;
$i < 64;
$i++) {
516 for (
$i = 0;
$i < 64;
$i++) {
523 $t2 = $this->
_add($s0, $maj);
529 ($this->
_not($e) & $g);
530 $t1 = $this->
_add(
$h, $s1, $ch, $k[
$i],
$w[$i]);
535 $e = $this->
_add(
$d, $t1);
539 $a = $this->
_add($t1, $t2);
544 $this->
_add($hash[0], $a),
545 $this->
_add($hash[1], $b),
546 $this->
_add($hash[2],
$c),
547 $this->
_add($hash[3],
$d),
548 $this->
_add($hash[4], $e),
549 $this->
_add($hash[5],
$f),
550 $this->
_add($hash[6], $g),
556 return pack(
'N8', $hash[0], $hash[1], $hash[2], $hash[3], $hash[4], $hash[5], $hash[6], $hash[7]);
567 static $init384, $init512, $k;
572 'cbbb9d5dc1059ed8',
'629a292a367cd507',
'9159015a3070dd17',
'152fecd8f70e5939',
573 '67332667ffc00b31',
'8eb44a8768581511',
'db0c2e0d64f98fa7',
'47b5481dbefa4fa4' 576 '6a09e667f3bcc908',
'bb67ae8584caa73b',
'3c6ef372fe94f82b',
'a54ff53a5f1d36f1',
577 '510e527fade682d1',
'9b05688c2b3e6c1f',
'1f83d9abfb41bd6b',
'5be0cd19137e2179' 580 for (
$i = 0;
$i < 8;
$i++) {
582 $init384[
$i]->setPrecision(64);
584 $init512[
$i]->setPrecision(64);
590 '428a2f98d728ae22',
'7137449123ef65cd',
'b5c0fbcfec4d3b2f',
'e9b5dba58189dbbc',
591 '3956c25bf348b538',
'59f111f1b605d019',
'923f82a4af194f9b',
'ab1c5ed5da6d8118',
592 'd807aa98a3030242',
'12835b0145706fbe',
'243185be4ee4b28c',
'550c7dc3d5ffb4e2',
593 '72be5d74f27b896f',
'80deb1fe3b1696b1',
'9bdc06a725c71235',
'c19bf174cf692694',
594 'e49b69c19ef14ad2',
'efbe4786384f25e3',
'0fc19dc68b8cd5b5',
'240ca1cc77ac9c65',
595 '2de92c6f592b0275',
'4a7484aa6ea6e483',
'5cb0a9dcbd41fbd4',
'76f988da831153b5',
596 '983e5152ee66dfab',
'a831c66d2db43210',
'b00327c898fb213f',
'bf597fc7beef0ee4',
597 'c6e00bf33da88fc2',
'd5a79147930aa725',
'06ca6351e003826f',
'142929670a0e6e70',
598 '27b70a8546d22ffc',
'2e1b21385c26c926',
'4d2c6dfc5ac42aed',
'53380d139d95b3df',
599 '650a73548baf63de',
'766a0abb3c77b2a8',
'81c2c92e47edaee6',
'92722c851482353b',
600 'a2bfe8a14cf10364',
'a81a664bbc423001',
'c24b8b70d0f89791',
'c76c51a30654be30',
601 'd192e819d6ef5218',
'd69906245565a910',
'f40e35855771202a',
'106aa07032bbd1b8',
602 '19a4c116b8d2d0c8',
'1e376c085141ab53',
'2748774cdf8eeb99',
'34b0bcb5e19b48a8',
603 '391c0cb3c5c95a63',
'4ed8aa4ae3418acb',
'5b9cca4f7763e373',
'682e6ff3d6b2b8a3',
604 '748f82ee5defb2fc',
'78a5636f43172f60',
'84c87814a1f0ab72',
'8cc702081a6439ec',
605 '90befffa23631e28',
'a4506cebde82bde9',
'bef9a3f7b2c67915',
'c67178f2e372532b',
606 'ca273eceea26619c',
'd186b8c721c0c207',
'eada7dd6cde0eb1e',
'f57d4f7fee6ed178',
607 '06f067aa72176fba',
'0a637dc5a2c898a6',
'113f9804bef90dae',
'1b710b35131c471b',
608 '28db77f523047d84',
'32caab7b40c72493',
'3c9ebe0a15c9bebc',
'431d67c49c100d4c',
609 '4cc5d4becb3e42b6',
'597f299cfc657e2a',
'5fcb6fab3ad6faec',
'6c44198c4a475817' 612 for (
$i = 0;
$i < 80;
$i++) {
617 $hash = $this->l == 48 ? $init384 : $init512;
620 $length = strlen(
$m);
622 $m.= str_repeat(chr(0), 128 - (($length + 16) & 0x7F));
623 $m[$length] = chr(0x80);
625 $m.= pack(
'N4', 0, 0, 0, $length << 3);
628 $chunks = str_split(
$m, 128);
629 foreach ($chunks as $chunk) {
631 for (
$i = 0;
$i < 16;
$i++) {
633 $temp->setPrecision(64);
638 for (
$i = 16;
$i < 80;
$i++) {
640 $w[
$i - 15]->bitwise_rightRotate(1),
641 $w[
$i - 15]->bitwise_rightRotate(8),
642 $w[
$i - 15]->bitwise_rightShift(7)
644 $s0 = $temp[0]->bitwise_xor($temp[1]);
645 $s0 = $s0->bitwise_xor($temp[2]);
647 $w[
$i - 2]->bitwise_rightRotate(19),
648 $w[
$i - 2]->bitwise_rightRotate(61),
649 $w[
$i - 2]->bitwise_rightShift(6)
651 $s1 = $temp[0]->bitwise_xor($temp[1]);
652 $s1 = $s1->bitwise_xor($temp[2]);
660 $a = $hash[0]->copy();
661 $b = $hash[1]->copy();
662 $c = $hash[2]->copy();
663 $d = $hash[3]->copy();
664 $e = $hash[4]->copy();
665 $f = $hash[5]->copy();
666 $g = $hash[6]->copy();
667 $h = $hash[7]->copy();
670 for (
$i = 0;
$i < 80;
$i++) {
672 $a->bitwise_rightRotate(28),
673 $a->bitwise_rightRotate(34),
674 $a->bitwise_rightRotate(39)
676 $s0 = $temp[0]->bitwise_xor($temp[1]);
677 $s0 = $s0->bitwise_xor($temp[2]);
683 $maj = $temp[0]->bitwise_xor($temp[1]);
684 $maj = $maj->bitwise_xor($temp[2]);
685 $t2 = $s0->add($maj);
688 $e->bitwise_rightRotate(14),
689 $e->bitwise_rightRotate(18),
690 $e->bitwise_rightRotate(41)
692 $s1 = $temp[0]->bitwise_xor($temp[1]);
693 $s1 = $s1->bitwise_xor($temp[2]);
696 $g->bitwise_and($e->bitwise_not())
698 $ch = $temp[0]->bitwise_xor($temp[1]);
701 $t1 = $t1->add($k[
$i]);
702 $t1 = $t1->add(
$w[$i]);
729 $temp = $hash[0]->toBytes() . $hash[1]->toBytes() . $hash[2]->toBytes() . $hash[3]->toBytes() .
730 $hash[4]->toBytes() . $hash[5]->toBytes();
731 if ($this->l != 48) {
732 $temp.= $hash[6]->toBytes() . $hash[7]->toBytes();
750 $mask = (1 << $invamt) - 1;
751 return ((
$int << $invamt) & 0xFFFFFFFF) | ((
$int >> $amt) &
$mask);
765 $mask = (1 << (32 - $amt)) - 1;
779 return ~
$int & 0xFFFFFFFF;
801 $arguments = func_get_args();
802 foreach ($arguments as $argument) {
803 $result+= $argument < 0 ? ($argument & 0x7FFFFFFF) + 0x80000000 : $argument;
821 $substr = substr($string, 0,
$index);
822 $string = substr($string,
$index);
getHash()
Gets the hash function.
hash($text)
Compute the HMAC.
const MODE_INTERNAL
#+ private
const MODE_MHASH
Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+.
_string_shift(&$string, $index=1)
String Shift.
_rightRotate($int, $amt)
Right Rotate.
Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic...
_sha256($m)
Pure-PHP implementation of SHA256.
_sha512($m)
Pure-PHP implementation of SHA384 and SHA512.
const MODE_HASH
Toggles the hash() implementation, which works on PHP 5.1.2+.
setHash($hash)
Sets the hash function.
Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic...
_rightShift($int, $amt)
Right Shift.
setKey($key=false)
Sets the key for HMACs.
Pure-PHP arbitrary precision integer arithmetic library.
_sha1($m)
Wrapper for SHA1.
__construct($hash='sha1')
Default Constructor.
_md2($m)
Pure-PHP implementation of MD2.
getLength()
Returns the hash length (in bytes)
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d