Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

815 lines
28 KiB

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