PageRenderTime 50ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/fuel/core/vendor/phpseclib/Crypt/Hash.php

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