Hash.php 30 KB

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