ripemd160.ts 4.1 KB

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