PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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