PageRenderTime 86ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/phpseclib/phpseclib/phpseclib/Crypt/Hash.php

https://github.com/AmaniYunge/greenWeb
PHP | 845 lines | 491 code | 69 blank | 285 comment | 37 complexity | fa8ac79d826596d8e92f1cc5e256a119 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
  4. *
  5. * Uses hash() or mhash() if available and an internal implementation, otherwise. Currently supports the following:
  6. *
  7. * md2, md5, md5-96, sha1, sha1-96, sha256, sha384, and sha512
  8. *
  9. * If {@link Crypt_Hash::setKey() setKey()} is called, {@link Crypt_Hash::hash() hash()} will return the HMAC as opposed to
  10. * the hash. If no valid algorithm is provided, sha1 will be used.
  11. *
  12. * PHP versions 4 and 5
  13. *
  14. * {@internal The variable names are the same as those in
  15. * {@link http://tools.ietf.org/html/rfc2104#section-2 RFC2104}.}}
  16. *
  17. * Here's a short example of how to use this library:
  18. * <code>
  19. * <?php
  20. * include('Crypt/Hash.php');
  21. *
  22. * $hash = new Crypt_Hash('sha1');
  23. *
  24. * $hash->setKey('abcdefg');
  25. *
  26. * echo base64_encode($hash->hash('abcdefg'));
  27. * ?>
  28. * </code>
  29. *
  30. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  31. * of this software and associated documentation files (the "Software"), to deal
  32. * in the Software without restriction, including without limitation the rights
  33. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  34. * copies of the Software, and to permit persons to whom the Software is
  35. * furnished to do so, subject to the following conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be included in
  38. * all copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  41. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  42. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  43. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  44. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  45. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  46. * THE SOFTWARE.
  47. *
  48. * @category Crypt
  49. * @package Crypt_Hash
  50. * @author Jim Wigginton <terrafrost@php.net>
  51. * @copyright MMVII Jim Wigginton
  52. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  53. * @link http://phpseclib.sourceforge.net
  54. */
  55. /**#@+
  56. * @access private
  57. * @see Crypt_Hash::Crypt_Hash()
  58. */
  59. /**
  60. * Toggles the internal implementation
  61. */
  62. define('CRYPT_HASH_MODE_INTERNAL', 1);
  63. /**
  64. * Toggles the mhash() implementation, which has been deprecated on PHP 5.3.0+.
  65. */
  66. define('CRYPT_HASH_MODE_MHASH', 2);
  67. /**
  68. * Toggles the hash() implementation, which works on PHP 5.1.2+.
  69. */
  70. define('CRYPT_HASH_MODE_HASH', 3);
  71. /**#@-*/
  72. /**
  73. * Pure-PHP implementations of keyed-hash message authentication codes (HMACs) and various cryptographic hashing functions.
  74. *
  75. * @package Crypt_Hash
  76. * @author Jim Wigginton <terrafrost@php.net>
  77. * @version 0.1.0
  78. * @access public
  79. */
  80. class Crypt_Hash
  81. {
  82. /**
  83. * Hash Parameter
  84. *
  85. * @see Crypt_Hash::setHash()
  86. * @var Integer
  87. * @access private
  88. */
  89. var $hashParam;
  90. /**
  91. * Byte-length of compression blocks / key (Internal HMAC)
  92. *
  93. * @see Crypt_Hash::setAlgorithm()
  94. * @var Integer
  95. * @access private
  96. */
  97. var $b;
  98. /**
  99. * Byte-length of hash output (Internal HMAC)
  100. *
  101. * @see Crypt_Hash::setHash()
  102. * @var Integer
  103. * @access private
  104. */
  105. var $l = false;
  106. /**
  107. * Hash Algorithm
  108. *
  109. * @see Crypt_Hash::setHash()
  110. * @var String
  111. * @access private
  112. */
  113. var $hash;
  114. /**
  115. * Key
  116. *
  117. * @see Crypt_Hash::setKey()
  118. * @var String
  119. * @access private
  120. */
  121. var $key = false;
  122. /**
  123. * Outer XOR (Internal HMAC)
  124. *
  125. * @see Crypt_Hash::setKey()
  126. * @var String
  127. * @access private
  128. */
  129. var $opad;
  130. /**
  131. * Inner XOR (Internal HMAC)
  132. *
  133. * @see Crypt_Hash::setKey()
  134. * @var String
  135. * @access private
  136. */
  137. var $ipad;
  138. /**
  139. * Default Constructor.
  140. *
  141. * @param optional String $hash
  142. * @return Crypt_Hash
  143. * @access public
  144. */
  145. function Crypt_Hash($hash = 'sha1')
  146. {
  147. if ( !defined('CRYPT_HASH_MODE') ) {
  148. switch (true) {
  149. case extension_loaded('hash'):
  150. define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_HASH);
  151. break;
  152. case extension_loaded('mhash'):
  153. define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_MHASH);
  154. break;
  155. default:
  156. define('CRYPT_HASH_MODE', CRYPT_HASH_MODE_INTERNAL);
  157. }
  158. }
  159. $this->setHash($hash);
  160. }
  161. /**
  162. * Sets the key for HMACs
  163. *
  164. * Keys can be of any length.
  165. *
  166. * @access public
  167. * @param optional String $key
  168. */
  169. function setKey($key = false)
  170. {
  171. $this->key = $key;
  172. }
  173. /**
  174. * Gets the hash function.
  175. *
  176. * As set by the constructor or by the setHash() method.
  177. *
  178. * @access public
  179. * @return String
  180. */
  181. function getHash()
  182. {
  183. return $this->hashParam;
  184. }
  185. /**
  186. * Sets the hash function.
  187. *
  188. * @access public
  189. * @param String $hash
  190. */
  191. function setHash($hash)
  192. {
  193. $this->hashParam = $hash = strtolower($hash);
  194. switch ($hash) {
  195. case 'md5-96':
  196. case 'sha1-96':
  197. $this->l = 12; // 96 / 8 = 12
  198. break;
  199. case 'md2':
  200. case 'md5':
  201. $this->l = 16;
  202. break;
  203. case 'sha1':
  204. $this->l = 20;
  205. break;
  206. case 'sha256':
  207. $this->l = 32;
  208. break;
  209. case 'sha384':
  210. $this->l = 48;
  211. break;
  212. case 'sha512':
  213. $this->l = 64;
  214. }
  215. switch ($hash) {
  216. case 'md2':
  217. $mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_HASH && in_array('md2', hash_algos()) ?
  218. CRYPT_HASH_MODE_HASH : CRYPT_HASH_MODE_INTERNAL;
  219. break;
  220. case 'sha384':
  221. case 'sha512':
  222. $mode = CRYPT_HASH_MODE == CRYPT_HASH_MODE_MHASH ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE;
  223. break;
  224. default:
  225. $mode = CRYPT_HASH_MODE;
  226. }
  227. switch ( $mode ) {
  228. case CRYPT_HASH_MODE_MHASH:
  229. switch ($hash) {
  230. case 'md5':
  231. case 'md5-96':
  232. $this->hash = MHASH_MD5;
  233. break;
  234. case 'sha256':
  235. $this->hash = MHASH_SHA256;
  236. break;
  237. case 'sha1':
  238. case 'sha1-96':
  239. default:
  240. $this->hash = MHASH_SHA1;
  241. }
  242. return;
  243. case CRYPT_HASH_MODE_HASH:
  244. switch ($hash) {
  245. case 'md5':
  246. case 'md5-96':
  247. $this->hash = 'md5';
  248. return;
  249. case 'md2':
  250. case 'sha256':
  251. case 'sha384':
  252. case 'sha512':
  253. $this->hash = $hash;
  254. return;
  255. case 'sha1':
  256. case 'sha1-96':
  257. default:
  258. $this->hash = 'sha1';
  259. }
  260. return;
  261. }
  262. switch ($hash) {
  263. case 'md2':
  264. $this->b = 16;
  265. $this->hash = array($this, '_md2');
  266. break;
  267. case 'md5':
  268. case 'md5-96':
  269. $this->b = 64;
  270. $this->hash = array($this, '_md5');
  271. break;
  272. case 'sha256':
  273. $this->b = 64;
  274. $this->hash = array($this, '_sha256');
  275. break;
  276. case 'sha384':
  277. case 'sha512':
  278. $this->b = 128;
  279. $this->hash = array($this, '_sha512');
  280. break;
  281. case 'sha1':
  282. case 'sha1-96':
  283. default:
  284. $this->b = 64;
  285. $this->hash = array($this, '_sha1');
  286. }
  287. $this->ipad = str_repeat(chr(0x36), $this->b);
  288. $this->opad = str_repeat(chr(0x5C), $this->b);
  289. }
  290. /**
  291. * Compute the HMAC.
  292. *
  293. * @access public
  294. * @param String $text
  295. * @return String
  296. */
  297. function hash($text)
  298. {
  299. $mode = is_array($this->hash) ? CRYPT_HASH_MODE_INTERNAL : CRYPT_HASH_MODE;
  300. if (!empty($this->key) || is_string($this->key)) {
  301. switch ( $mode ) {
  302. case CRYPT_HASH_MODE_MHASH:
  303. $output = mhash($this->hash, $text, $this->key);
  304. break;
  305. case CRYPT_HASH_MODE_HASH:
  306. $output = hash_hmac($this->hash, $text, $this->key, true);
  307. break;
  308. case CRYPT_HASH_MODE_INTERNAL:
  309. /* "Applications that use keys longer than B bytes will first hash the key using H and then use the
  310. resultant L byte string as the actual key to HMAC."
  311. -- http://tools.ietf.org/html/rfc2104#section-2 */
  312. $key = strlen($this->key) > $this->b ? call_user_func($this->hash, $this->key) : $this->key;
  313. $key = str_pad($key, $this->b, chr(0)); // step 1
  314. $temp = $this->ipad ^ $key; // step 2
  315. $temp .= $text; // step 3
  316. $temp = call_user_func($this->hash, $temp); // step 4
  317. $output = $this->opad ^ $key; // step 5
  318. $output.= $temp; // step 6
  319. $output = call_user_func($this->hash, $output); // step 7
  320. }
  321. } else {
  322. switch ( $mode ) {
  323. case CRYPT_HASH_MODE_MHASH:
  324. $output = mhash($this->hash, $text);
  325. break;
  326. case CRYPT_HASH_MODE_HASH:
  327. $output = hash($this->hash, $text, true);
  328. break;
  329. case CRYPT_HASH_MODE_INTERNAL:
  330. $output = call_user_func($this->hash, $text);
  331. }
  332. }
  333. return substr($output, 0, $this->l);
  334. }
  335. /**
  336. * Returns the hash length (in bytes)
  337. *
  338. * @access public
  339. * @return Integer
  340. */
  341. function getLength()
  342. {
  343. return $this->l;
  344. }
  345. /**
  346. * Wrapper for MD5
  347. *
  348. * @access private
  349. * @param String $m
  350. */
  351. function _md5($m)
  352. {
  353. return pack('H*', md5($m));
  354. }
  355. /**
  356. * Wrapper for SHA1
  357. *
  358. * @access private
  359. * @param String $m
  360. */
  361. function _sha1($m)
  362. {
  363. return pack('H*', sha1($m));
  364. }
  365. /**
  366. * Pure-PHP implementation of MD2
  367. *
  368. * See {@link http://tools.ietf.org/html/rfc1319 RFC1319}.
  369. *
  370. * @access private
  371. * @param String $m
  372. */
  373. function _md2($m)
  374. {
  375. static $s = array(
  376. 41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
  377. 19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
  378. 76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
  379. 138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
  380. 245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
  381. 148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
  382. 39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
  383. 181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
  384. 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
  385. 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
  386. 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
  387. 85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
  388. 234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
  389. 129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
  390. 8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
  391. 203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
  392. 166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
  393. 31, 26, 219, 153, 141, 51, 159, 17, 131, 20
  394. );
  395. // Step 1. Append Padding Bytes
  396. $pad = 16 - (strlen($m) & 0xF);
  397. $m.= str_repeat(chr($pad), $pad);
  398. $length = strlen($m);
  399. // Step 2. Append Checksum
  400. $c = str_repeat(chr(0), 16);
  401. $l = chr(0);
  402. for ($i = 0; $i < $length; $i+= 16) {
  403. for ($j = 0; $j < 16; $j++) {
  404. // RFC1319 incorrectly states that C[j] should be set to S[c xor L]
  405. //$c[$j] = chr($s[ord($m[$i + $j] ^ $l)]);
  406. // 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]
  407. $c[$j] = chr($s[ord($m[$i + $j] ^ $l)] ^ ord($c[$j]));
  408. $l = $c[$j];
  409. }
  410. }
  411. $m.= $c;
  412. $length+= 16;
  413. // Step 3. Initialize MD Buffer
  414. $x = str_repeat(chr(0), 48);
  415. // Step 4. Process Message in 16-Byte Blocks
  416. for ($i = 0; $i < $length; $i+= 16) {
  417. for ($j = 0; $j < 16; $j++) {
  418. $x[$j + 16] = $m[$i + $j];
  419. $x[$j + 32] = $x[$j + 16] ^ $x[$j];
  420. }
  421. $t = chr(0);
  422. for ($j = 0; $j < 18; $j++) {
  423. for ($k = 0; $k < 48; $k++) {
  424. $x[$k] = $t = $x[$k] ^ chr($s[ord($t)]);
  425. //$t = $x[$k] = $x[$k] ^ chr($s[ord($t)]);
  426. }
  427. $t = chr(ord($t) + $j);
  428. }
  429. }
  430. // Step 5. Output
  431. return substr($x, 0, 16);
  432. }
  433. /**
  434. * Pure-PHP implementation of SHA256
  435. *
  436. * 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}.
  437. *
  438. * @access private
  439. * @param String $m
  440. */
  441. function _sha256($m)
  442. {
  443. if (extension_loaded('suhosin')) {
  444. return pack('H*', sha256($m));
  445. }
  446. // Initialize variables
  447. $hash = array(
  448. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  449. );
  450. // Initialize table of round constants
  451. // (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
  452. static $k = array(
  453. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  454. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  455. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  456. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  457. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  458. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  459. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  460. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  461. );
  462. // Pre-processing
  463. $length = strlen($m);
  464. // to round to nearest 56 mod 64, we'll add 64 - (length + (64 - 56)) % 64
  465. $m.= str_repeat(chr(0), 64 - (($length + 8) & 0x3F));
  466. $m[$length] = chr(0x80);
  467. // we don't support hashing strings 512MB long
  468. $m.= pack('N2', 0, $length << 3);
  469. // Process the message in successive 512-bit chunks
  470. $chunks = str_split($m, 64);
  471. foreach ($chunks as $chunk) {
  472. $w = array();
  473. for ($i = 0; $i < 16; $i++) {
  474. extract(unpack('Ntemp', $this->_string_shift($chunk, 4)));
  475. $w[] = $temp;
  476. }
  477. // Extend the sixteen 32-bit words into sixty-four 32-bit words
  478. for ($i = 16; $i < 64; $i++) {
  479. $s0 = $this->_rightRotate($w[$i - 15], 7) ^
  480. $this->_rightRotate($w[$i - 15], 18) ^
  481. $this->_rightShift( $w[$i - 15], 3);
  482. $s1 = $this->_rightRotate($w[$i - 2], 17) ^
  483. $this->_rightRotate($w[$i - 2], 19) ^
  484. $this->_rightShift( $w[$i - 2], 10);
  485. $w[$i] = $this->_add($w[$i - 16], $s0, $w[$i - 7], $s1);
  486. }
  487. // Initialize hash value for this chunk
  488. list($a, $b, $c, $d, $e, $f, $g, $h) = $hash;
  489. // Main loop
  490. for ($i = 0; $i < 64; $i++) {
  491. $s0 = $this->_rightRotate($a, 2) ^
  492. $this->_rightRotate($a, 13) ^
  493. $this->_rightRotate($a, 22);
  494. $maj = ($a & $b) ^
  495. ($a & $c) ^
  496. ($b & $c);
  497. $t2 = $this->_add($s0, $maj);
  498. $s1 = $this->_rightRotate($e, 6) ^
  499. $this->_rightRotate($e, 11) ^
  500. $this->_rightRotate($e, 25);
  501. $ch = ($e & $f) ^
  502. ($this->_not($e) & $g);
  503. $t1 = $this->_add($h, $s1, $ch, $k[$i], $w[$i]);
  504. $h = $g;
  505. $g = $f;
  506. $f = $e;
  507. $e = $this->_add($d, $t1);
  508. $d = $c;
  509. $c = $b;
  510. $b = $a;
  511. $a = $this->_add($t1, $t2);
  512. }
  513. // Add this chunk's hash to result so far
  514. $hash = array(
  515. $this->_add($hash[0], $a),
  516. $this->_add($hash[1], $b),
  517. $this->_add($hash[2], $c),
  518. $this->_add($hash[3], $d),
  519. $this->_add($hash[4], $e),
  520. $this->_add($hash[5], $f),
  521. $this->_add($hash[6], $g),
  522. $this->_add($hash[7], $h)
  523. );
  524. }
  525. // Produce the final hash value (big-endian)
  526. return pack('N8', $hash[0], $hash[1], $hash[2], $hash[3], $hash[4], $hash[5], $hash[6], $hash[7]);
  527. }
  528. /**
  529. * Pure-PHP implementation of SHA384 and SHA512
  530. *
  531. * @access private
  532. * @param String $m
  533. */
  534. function _sha512($m)
  535. {
  536. if (!class_exists('Math_BigInteger')) {
  537. include_once 'Math/BigInteger.php';
  538. }
  539. static $init384, $init512, $k;
  540. if (!isset($k)) {
  541. // Initialize variables
  542. $init384 = array( // initial values for SHA384
  543. 'cbbb9d5dc1059ed8', '629a292a367cd507', '9159015a3070dd17', '152fecd8f70e5939',
  544. '67332667ffc00b31', '8eb44a8768581511', 'db0c2e0d64f98fa7', '47b5481dbefa4fa4'
  545. );
  546. $init512 = array( // initial values for SHA512
  547. '6a09e667f3bcc908', 'bb67ae8584caa73b', '3c6ef372fe94f82b', 'a54ff53a5f1d36f1',
  548. '510e527fade682d1', '9b05688c2b3e6c1f', '1f83d9abfb41bd6b', '5be0cd19137e2179'
  549. );
  550. for ($i = 0; $i < 8; $i++) {
  551. $init384[$i] = new Math_BigInteger($init384[$i], 16);
  552. $init384[$i]->setPrecision(64);
  553. $init512[$i] = new Math_BigInteger($init512[$i], 16);
  554. $init512[$i]->setPrecision(64);
  555. }
  556. // Initialize table of round constants
  557. // (first 64 bits of the fractional parts of the cube roots of the first 80 primes 2..409)
  558. $k = array(
  559. '428a2f98d728ae22', '7137449123ef65cd', 'b5c0fbcfec4d3b2f', 'e9b5dba58189dbbc',
  560. '3956c25bf348b538', '59f111f1b605d019', '923f82a4af194f9b', 'ab1c5ed5da6d8118',
  561. 'd807aa98a3030242', '12835b0145706fbe', '243185be4ee4b28c', '550c7dc3d5ffb4e2',
  562. '72be5d74f27b896f', '80deb1fe3b1696b1', '9bdc06a725c71235', 'c19bf174cf692694',
  563. 'e49b69c19ef14ad2', 'efbe4786384f25e3', '0fc19dc68b8cd5b5', '240ca1cc77ac9c65',
  564. '2de92c6f592b0275', '4a7484aa6ea6e483', '5cb0a9dcbd41fbd4', '76f988da831153b5',
  565. '983e5152ee66dfab', 'a831c66d2db43210', 'b00327c898fb213f', 'bf597fc7beef0ee4',
  566. 'c6e00bf33da88fc2', 'd5a79147930aa725', '06ca6351e003826f', '142929670a0e6e70',
  567. '27b70a8546d22ffc', '2e1b21385c26c926', '4d2c6dfc5ac42aed', '53380d139d95b3df',
  568. '650a73548baf63de', '766a0abb3c77b2a8', '81c2c92e47edaee6', '92722c851482353b',
  569. 'a2bfe8a14cf10364', 'a81a664bbc423001', 'c24b8b70d0f89791', 'c76c51a30654be30',
  570. 'd192e819d6ef5218', 'd69906245565a910', 'f40e35855771202a', '106aa07032bbd1b8',
  571. '19a4c116b8d2d0c8', '1e376c085141ab53', '2748774cdf8eeb99', '34b0bcb5e19b48a8',
  572. '391c0cb3c5c95a63', '4ed8aa4ae3418acb', '5b9cca4f7763e373', '682e6ff3d6b2b8a3',
  573. '748f82ee5defb2fc', '78a5636f43172f60', '84c87814a1f0ab72', '8cc702081a6439ec',
  574. '90befffa23631e28', 'a4506cebde82bde9', 'bef9a3f7b2c67915', 'c67178f2e372532b',
  575. 'ca273eceea26619c', 'd186b8c721c0c207', 'eada7dd6cde0eb1e', 'f57d4f7fee6ed178',
  576. '06f067aa72176fba', '0a637dc5a2c898a6', '113f9804bef90dae', '1b710b35131c471b',
  577. '28db77f523047d84', '32caab7b40c72493', '3c9ebe0a15c9bebc', '431d67c49c100d4c',
  578. '4cc5d4becb3e42b6', '597f299cfc657e2a', '5fcb6fab3ad6faec', '6c44198c4a475817'
  579. );
  580. for ($i = 0; $i < 80; $i++) {
  581. $k[$i] = new Math_BigInteger($k[$i], 16);
  582. }
  583. }
  584. $hash = $this->l == 48 ? $init384 : $init512;
  585. // Pre-processing
  586. $length = strlen($m);
  587. // to round to nearest 112 mod 128, we'll add 128 - (length + (128 - 112)) % 128
  588. $m.= str_repeat(chr(0), 128 - (($length + 16) & 0x7F));
  589. $m[$length] = chr(0x80);
  590. // we don't support hashing strings 512MB long
  591. $m.= pack('N4', 0, 0, 0, $length << 3);
  592. // Process the message in successive 1024-bit chunks
  593. $chunks = str_split($m, 128);
  594. foreach ($chunks as $chunk) {
  595. $w = array();
  596. for ($i = 0; $i < 16; $i++) {
  597. $temp = new Math_BigInteger($this->_string_shift($chunk, 8), 256);
  598. $temp->setPrecision(64);
  599. $w[] = $temp;
  600. }
  601. // Extend the sixteen 32-bit words into eighty 32-bit words
  602. for ($i = 16; $i < 80; $i++) {
  603. $temp = array(
  604. $w[$i - 15]->bitwise_rightRotate(1),
  605. $w[$i - 15]->bitwise_rightRotate(8),
  606. $w[$i - 15]->bitwise_rightShift(7)
  607. );
  608. $s0 = $temp[0]->bitwise_xor($temp[1]);
  609. $s0 = $s0->bitwise_xor($temp[2]);
  610. $temp = array(
  611. $w[$i - 2]->bitwise_rightRotate(19),
  612. $w[$i - 2]->bitwise_rightRotate(61),
  613. $w[$i - 2]->bitwise_rightShift(6)
  614. );
  615. $s1 = $temp[0]->bitwise_xor($temp[1]);
  616. $s1 = $s1->bitwise_xor($temp[2]);
  617. $w[$i] = $w[$i - 16]->copy();
  618. $w[$i] = $w[$i]->add($s0);
  619. $w[$i] = $w[$i]->add($w[$i - 7]);
  620. $w[$i] = $w[$i]->add($s1);
  621. }
  622. // Initialize hash value for this chunk
  623. $a = $hash[0]->copy();
  624. $b = $hash[1]->copy();
  625. $c = $hash[2]->copy();
  626. $d = $hash[3]->copy();
  627. $e = $hash[4]->copy();
  628. $f = $hash[5]->copy();
  629. $g = $hash[6]->copy();
  630. $h = $hash[7]->copy();
  631. // Main loop
  632. for ($i = 0; $i < 80; $i++) {
  633. $temp = array(
  634. $a->bitwise_rightRotate(28),
  635. $a->bitwise_rightRotate(34),
  636. $a->bitwise_rightRotate(39)
  637. );
  638. $s0 = $temp[0]->bitwise_xor($temp[1]);
  639. $s0 = $s0->bitwise_xor($temp[2]);
  640. $temp = array(
  641. $a->bitwise_and($b),
  642. $a->bitwise_and($c),
  643. $b->bitwise_and($c)
  644. );
  645. $maj = $temp[0]->bitwise_xor($temp[1]);
  646. $maj = $maj->bitwise_xor($temp[2]);
  647. $t2 = $s0->add($maj);
  648. $temp = array(
  649. $e->bitwise_rightRotate(14),
  650. $e->bitwise_rightRotate(18),
  651. $e->bitwise_rightRotate(41)
  652. );
  653. $s1 = $temp[0]->bitwise_xor($temp[1]);
  654. $s1 = $s1->bitwise_xor($temp[2]);
  655. $temp = array(
  656. $e->bitwise_and($f),
  657. $g->bitwise_and($e->bitwise_not())
  658. );
  659. $ch = $temp[0]->bitwise_xor($temp[1]);
  660. $t1 = $h->add($s1);
  661. $t1 = $t1->add($ch);
  662. $t1 = $t1->add($k[$i]);
  663. $t1 = $t1->add($w[$i]);
  664. $h = $g->copy();
  665. $g = $f->copy();
  666. $f = $e->copy();
  667. $e = $d->add($t1);
  668. $d = $c->copy();
  669. $c = $b->copy();
  670. $b = $a->copy();
  671. $a = $t1->add($t2);
  672. }
  673. // Add this chunk's hash to result so far
  674. $hash = array(
  675. $hash[0]->add($a),
  676. $hash[1]->add($b),
  677. $hash[2]->add($c),
  678. $hash[3]->add($d),
  679. $hash[4]->add($e),
  680. $hash[5]->add($f),
  681. $hash[6]->add($g),
  682. $hash[7]->add($h)
  683. );
  684. }
  685. // Produce the final hash value (big-endian)
  686. // (Crypt_Hash::hash() trims the output for hashes but not for HMACs. as such, we trim the output here)
  687. $temp = $hash[0]->toBytes() . $hash[1]->toBytes() . $hash[2]->toBytes() . $hash[3]->toBytes() .
  688. $hash[4]->toBytes() . $hash[5]->toBytes();
  689. if ($this->l != 48) {
  690. $temp.= $hash[6]->toBytes() . $hash[7]->toBytes();
  691. }
  692. return $temp;
  693. }
  694. /**
  695. * Right Rotate
  696. *
  697. * @access private
  698. * @param Integer $int
  699. * @param Integer $amt
  700. * @see _sha256()
  701. * @return Integer
  702. */
  703. function _rightRotate($int, $amt)
  704. {
  705. $invamt = 32 - $amt;
  706. $mask = (1 << $invamt) - 1;
  707. return (($int << $invamt) & 0xFFFFFFFF) | (($int >> $amt) & $mask);
  708. }
  709. /**
  710. * Right Shift
  711. *
  712. * @access private
  713. * @param Integer $int
  714. * @param Integer $amt
  715. * @see _sha256()
  716. * @return Integer
  717. */
  718. function _rightShift($int, $amt)
  719. {
  720. $mask = (1 << (32 - $amt)) - 1;
  721. return ($int >> $amt) & $mask;
  722. }
  723. /**
  724. * Not
  725. *
  726. * @access private
  727. * @param Integer $int
  728. * @see _sha256()
  729. * @return Integer
  730. */
  731. function _not($int)
  732. {
  733. return ~$int & 0xFFFFFFFF;
  734. }
  735. /**
  736. * Add
  737. *
  738. * _sha256() adds multiple unsigned 32-bit integers. Since PHP doesn't support unsigned integers and since the
  739. * possibility of overflow exists, care has to be taken. Math_BigInteger() could be used but this should be faster.
  740. *
  741. * @param Integer $...
  742. * @return Integer
  743. * @see _sha256()
  744. * @access private
  745. */
  746. function _add()
  747. {
  748. static $mod;
  749. if (!isset($mod)) {
  750. $mod = pow(2, 32);
  751. }
  752. $result = 0;
  753. $arguments = func_get_args();
  754. foreach ($arguments as $argument) {
  755. $result+= $argument < 0 ? ($argument & 0x7FFFFFFF) + 0x80000000 : $argument;
  756. }
  757. return fmod($result, $mod);
  758. }
  759. /**
  760. * String Shift
  761. *
  762. * Inspired by array_shift
  763. *
  764. * @param String $string
  765. * @param optional Integer $index
  766. * @return String
  767. * @access private
  768. */
  769. function _string_shift(&$string, $index = 1)
  770. {
  771. $substr = substr($string, 0, $index);
  772. $string = substr($string, $index);
  773. return $substr;
  774. }
  775. }