ripemd160.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ripemd160 = exports.RIPEMD160 = void 0;
  4. const _md_js_1 = require("./_md.js");
  5. const utils_js_1 = require("./utils.js");
  6. // https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
  7. // https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
  8. const Rho = /* @__PURE__ */ new Uint8Array([7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8]);
  9. const Id = /* @__PURE__ */ new Uint8Array(new Array(16).fill(0).map((_, i) => i));
  10. const Pi = /* @__PURE__ */ Id.map((i) => (9 * i + 5) % 16);
  11. let idxL = [Id];
  12. let idxR = [Pi];
  13. for (let i = 0; i < 4; i++)
  14. for (let j of [idxL, idxR])
  15. j.push(j[i].map((k) => Rho[k]));
  16. const shifts = /* @__PURE__ */ [
  17. [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
  18. [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
  19. [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
  20. [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
  21. [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5],
  22. ].map((i) => new Uint8Array(i));
  23. const shiftsL = /* @__PURE__ */ idxL.map((idx, i) => idx.map((j) => shifts[i][j]));
  24. const shiftsR = /* @__PURE__ */ idxR.map((idx, i) => idx.map((j) => shifts[i][j]));
  25. const Kl = /* @__PURE__ */ new Uint32Array([
  26. 0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e,
  27. ]);
  28. const Kr = /* @__PURE__ */ new Uint32Array([
  29. 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000,
  30. ]);
  31. // It's called f() in spec.
  32. function f(group, x, y, z) {
  33. if (group === 0)
  34. return x ^ y ^ z;
  35. else if (group === 1)
  36. return (x & y) | (~x & z);
  37. else if (group === 2)
  38. return (x | ~y) ^ z;
  39. else if (group === 3)
  40. return (x & z) | (y & ~z);
  41. else
  42. return x ^ (y | ~z);
  43. }
  44. // Temporary buffer, not used to store anything between runs
  45. const R_BUF = /* @__PURE__ */ new Uint32Array(16);
  46. class RIPEMD160 extends _md_js_1.HashMD {
  47. constructor() {
  48. super(64, 20, 8, true);
  49. this.h0 = 0x67452301 | 0;
  50. this.h1 = 0xefcdab89 | 0;
  51. this.h2 = 0x98badcfe | 0;
  52. this.h3 = 0x10325476 | 0;
  53. this.h4 = 0xc3d2e1f0 | 0;
  54. }
  55. get() {
  56. const { h0, h1, h2, h3, h4 } = this;
  57. return [h0, h1, h2, h3, h4];
  58. }
  59. set(h0, h1, h2, h3, h4) {
  60. this.h0 = h0 | 0;
  61. this.h1 = h1 | 0;
  62. this.h2 = h2 | 0;
  63. this.h3 = h3 | 0;
  64. this.h4 = h4 | 0;
  65. }
  66. process(view, offset) {
  67. for (let i = 0; i < 16; i++, offset += 4)
  68. R_BUF[i] = view.getUint32(offset, true);
  69. // prettier-ignore
  70. let al = this.h0 | 0, ar = al, bl = this.h1 | 0, br = bl, cl = this.h2 | 0, cr = cl, dl = this.h3 | 0, dr = dl, el = this.h4 | 0, er = el;
  71. // Instead of iterating 0 to 80, we split it into 5 groups
  72. // And use the groups in constants, functions, etc. Much simpler
  73. for (let group = 0; group < 5; group++) {
  74. const rGroup = 4 - group;
  75. const hbl = Kl[group], hbr = Kr[group]; // prettier-ignore
  76. const rl = idxL[group], rr = idxR[group]; // prettier-ignore
  77. const sl = shiftsL[group], sr = shiftsR[group]; // prettier-ignore
  78. for (let i = 0; i < 16; i++) {
  79. const tl = ((0, utils_js_1.rotl)(al + f(group, bl, cl, dl) + R_BUF[rl[i]] + hbl, sl[i]) + el) | 0;
  80. al = el, el = dl, dl = (0, utils_js_1.rotl)(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
  81. }
  82. // 2 loops are 10% faster
  83. for (let i = 0; i < 16; i++) {
  84. const tr = ((0, utils_js_1.rotl)(ar + f(rGroup, br, cr, dr) + R_BUF[rr[i]] + hbr, sr[i]) + er) | 0;
  85. ar = er, er = dr, dr = (0, utils_js_1.rotl)(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
  86. }
  87. }
  88. // Add the compressed chunk to the current hash value
  89. this.set((this.h1 + cl + dr) | 0, (this.h2 + dl + er) | 0, (this.h3 + el + ar) | 0, (this.h4 + al + br) | 0, (this.h0 + bl + cr) | 0);
  90. }
  91. roundClean() {
  92. R_BUF.fill(0);
  93. }
  94. destroy() {
  95. this.destroyed = true;
  96. this.buffer.fill(0);
  97. this.set(0, 0, 0, 0, 0);
  98. }
  99. }
  100. exports.RIPEMD160 = RIPEMD160;
  101. /**
  102. * RIPEMD-160 - a hash function from 1990s.
  103. * @param message - msg that would be hashed
  104. */
  105. exports.ripemd160 = (0, utils_js_1.wrapConstructor)(() => new RIPEMD160());
  106. //# sourceMappingURL=ripemd160.js.map