ripemd160.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /** @preserve
  2. (c) 2012 by Cédric Mesnil. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted
  4. provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this list of
  6. conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice, this list
  8. of conditions and the following disclaimer in the documentation and/or other materials
  9. provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  11. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  12. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  13. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  14. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  15. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  16. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  17. WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. */
  19. import {
  20. WordArray,
  21. Hasher,
  22. } from './core.js';
  23. // Constants table
  24. const _zl = WordArray.create([
  25. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  26. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  27. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  28. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  29. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]);
  30. const _zr = WordArray.create([
  31. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  32. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  33. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  34. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  35. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]);
  36. const _sl = WordArray.create([
  37. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  38. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  39. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  40. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  41. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6]);
  42. const _sr = WordArray.create([
  43. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  44. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  45. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  46. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  47. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11]);
  48. const _hl = WordArray.create([0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]);
  49. const _hr = WordArray.create([0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]);
  50. const f1 = (x, y, z) => (x) ^ (y) ^ (z);
  51. const f2 = (x, y, z) => ((x) & (y)) | ((~x) & (z));
  52. const f3 = (x, y, z) => ((x) | (~(y))) ^ (z);
  53. const f4 = (x, y, z) => ((x) & (z)) | ((y) & (~(z)));
  54. const f5 = (x, y, z) => (x) ^ ((y) | (~(z)));
  55. const rotl = (x, n) => (x << n) | (x >>> (32 - n));
  56. /**
  57. * RIPEMD160 hash algorithm.
  58. */
  59. export class RIPEMD160Algo extends Hasher {
  60. _doReset() {
  61. this._hash = WordArray.create([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]);
  62. }
  63. _doProcessBlock(M, offset) {
  64. const _M = M;
  65. // Swap endian
  66. for (let i = 0; i < 16; i += 1) {
  67. // Shortcuts
  68. const offset_i = offset + i;
  69. const M_offset_i = _M[offset_i];
  70. // Swap
  71. _M[offset_i] = (
  72. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff)
  73. | (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  74. );
  75. }
  76. // Shortcut
  77. const H = this._hash.words;
  78. const hl = _hl.words;
  79. const hr = _hr.words;
  80. const zl = _zl.words;
  81. const zr = _zr.words;
  82. const sl = _sl.words;
  83. const sr = _sr.words;
  84. // Working variables
  85. let al = H[0];
  86. let bl = H[1];
  87. let cl = H[2];
  88. let dl = H[3];
  89. let el = H[4];
  90. let ar = H[0];
  91. let br = H[1];
  92. let cr = H[2];
  93. let dr = H[3];
  94. let er = H[4];
  95. // Computation
  96. let t;
  97. for (let i = 0; i < 80; i += 1) {
  98. t = (al + _M[offset + zl[i]]) | 0;
  99. if (i < 16) {
  100. t += f1(bl, cl, dl) + hl[0];
  101. } else if (i < 32) {
  102. t += f2(bl, cl, dl) + hl[1];
  103. } else if (i < 48) {
  104. t += f3(bl, cl, dl) + hl[2];
  105. } else if (i < 64) {
  106. t += f4(bl, cl, dl) + hl[3];
  107. } else { // if (i<80) {
  108. t += f5(bl, cl, dl) + hl[4];
  109. }
  110. t |= 0;
  111. t = rotl(t, sl[i]);
  112. t = (t + el) | 0;
  113. al = el;
  114. el = dl;
  115. dl = rotl(cl, 10);
  116. cl = bl;
  117. bl = t;
  118. t = (ar + _M[offset + zr[i]]) | 0;
  119. if (i < 16) {
  120. t += f5(br, cr, dr) + hr[0];
  121. } else if (i < 32) {
  122. t += f4(br, cr, dr) + hr[1];
  123. } else if (i < 48) {
  124. t += f3(br, cr, dr) + hr[2];
  125. } else if (i < 64) {
  126. t += f2(br, cr, dr) + hr[3];
  127. } else { // if (i<80) {
  128. t += f1(br, cr, dr) + hr[4];
  129. }
  130. t |= 0;
  131. t = rotl(t, sr[i]);
  132. t = (t + er) | 0;
  133. ar = er;
  134. er = dr;
  135. dr = rotl(cr, 10);
  136. cr = br;
  137. br = t;
  138. }
  139. // Intermediate hash value
  140. t = (H[1] + cl + dr) | 0;
  141. H[1] = (H[2] + dl + er) | 0;
  142. H[2] = (H[3] + el + ar) | 0;
  143. H[3] = (H[4] + al + br) | 0;
  144. H[4] = (H[0] + bl + cr) | 0;
  145. H[0] = t;
  146. }
  147. _doFinalize() {
  148. // Shortcuts
  149. const data = this._data;
  150. const dataWords = data.words;
  151. const nBitsTotal = this._nDataBytes * 8;
  152. const nBitsLeft = data.sigBytes * 8;
  153. // Add padding
  154. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - (nBitsLeft % 32));
  155. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  156. (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff)
  157. | (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
  158. );
  159. data.sigBytes = (dataWords.length + 1) * 4;
  160. // Hash final blocks
  161. this._process();
  162. // Shortcuts
  163. const hash = this._hash;
  164. const H = hash.words;
  165. // Swap endian
  166. for (let i = 0; i < 5; i += 1) {
  167. // Shortcut
  168. const H_i = H[i];
  169. // Swap
  170. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff)
  171. | (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
  172. }
  173. // Return final computed hash
  174. return hash;
  175. }
  176. clone() {
  177. const clone = super.clone.call(this);
  178. clone._hash = this._hash.clone();
  179. return clone;
  180. }
  181. }
  182. /**
  183. * Shortcut function to the hasher's object interface.
  184. *
  185. * @param {WordArray|string} message The message to hash.
  186. *
  187. * @return {WordArray} The hash.
  188. *
  189. * @static
  190. *
  191. * @example
  192. *
  193. * var hash = CryptoJS.RIPEMD160('message');
  194. * var hash = CryptoJS.RIPEMD160(wordArray);
  195. */
  196. export const RIPEMD160 = Hasher._createHelper(RIPEMD160Algo);
  197. /**
  198. * Shortcut function to the HMAC's object interface.
  199. *
  200. * @param {WordArray|string} message The message to hash.
  201. * @param {WordArray|string} key The secret key.
  202. *
  203. * @return {WordArray} The HMAC.
  204. *
  205. * @static
  206. *
  207. * @example
  208. *
  209. * var hmac = CryptoJS.HmacRIPEMD160(message, key);
  210. */
  211. export const HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160Algo);