legacy.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ripemd160 = exports.RIPEMD160 = exports.md5 = exports.MD5 = exports.sha1 = exports.SHA1 = void 0;
  4. /**
  5. SHA1 (RFC 3174), MD5 (RFC 1321) and RIPEMD160 (RFC 2286) legacy, weak hash functions.
  6. Don't use them in a new protocol. What "weak" means:
  7. - Collisions can be made with 2^18 effort in MD5, 2^60 in SHA1, 2^80 in RIPEMD160.
  8. - No practical pre-image attacks (only theoretical, 2^123.4)
  9. - HMAC seems kinda ok: https://datatracker.ietf.org/doc/html/rfc6151
  10. * @module
  11. */
  12. const _md_ts_1 = require("./_md.js");
  13. const utils_ts_1 = require("./utils.js");
  14. /** Initial SHA1 state */
  15. const SHA1_IV = /* @__PURE__ */ Uint32Array.from([
  16. 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
  17. ]);
  18. // Reusable temporary buffer
  19. const SHA1_W = /* @__PURE__ */ new Uint32Array(80);
  20. /** SHA1 legacy hash class. */
  21. class SHA1 extends _md_ts_1.HashMD {
  22. constructor() {
  23. super(64, 20, 8, false);
  24. this.A = SHA1_IV[0] | 0;
  25. this.B = SHA1_IV[1] | 0;
  26. this.C = SHA1_IV[2] | 0;
  27. this.D = SHA1_IV[3] | 0;
  28. this.E = SHA1_IV[4] | 0;
  29. }
  30. get() {
  31. const { A, B, C, D, E } = this;
  32. return [A, B, C, D, E];
  33. }
  34. set(A, B, C, D, E) {
  35. this.A = A | 0;
  36. this.B = B | 0;
  37. this.C = C | 0;
  38. this.D = D | 0;
  39. this.E = E | 0;
  40. }
  41. process(view, offset) {
  42. for (let i = 0; i < 16; i++, offset += 4)
  43. SHA1_W[i] = view.getUint32(offset, false);
  44. for (let i = 16; i < 80; i++)
  45. SHA1_W[i] = (0, utils_ts_1.rotl)(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
  46. // Compression function main loop, 80 rounds
  47. let { A, B, C, D, E } = this;
  48. for (let i = 0; i < 80; i++) {
  49. let F, K;
  50. if (i < 20) {
  51. F = (0, _md_ts_1.Chi)(B, C, D);
  52. K = 0x5a827999;
  53. }
  54. else if (i < 40) {
  55. F = B ^ C ^ D;
  56. K = 0x6ed9eba1;
  57. }
  58. else if (i < 60) {
  59. F = (0, _md_ts_1.Maj)(B, C, D);
  60. K = 0x8f1bbcdc;
  61. }
  62. else {
  63. F = B ^ C ^ D;
  64. K = 0xca62c1d6;
  65. }
  66. const T = ((0, utils_ts_1.rotl)(A, 5) + F + E + K + SHA1_W[i]) | 0;
  67. E = D;
  68. D = C;
  69. C = (0, utils_ts_1.rotl)(B, 30);
  70. B = A;
  71. A = T;
  72. }
  73. // Add the compressed chunk to the current hash value
  74. A = (A + this.A) | 0;
  75. B = (B + this.B) | 0;
  76. C = (C + this.C) | 0;
  77. D = (D + this.D) | 0;
  78. E = (E + this.E) | 0;
  79. this.set(A, B, C, D, E);
  80. }
  81. roundClean() {
  82. (0, utils_ts_1.clean)(SHA1_W);
  83. }
  84. destroy() {
  85. this.set(0, 0, 0, 0, 0);
  86. (0, utils_ts_1.clean)(this.buffer);
  87. }
  88. }
  89. exports.SHA1 = SHA1;
  90. /** SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
  91. exports.sha1 = (0, utils_ts_1.createHasher)(() => new SHA1());
  92. /** Per-round constants */
  93. const p32 = /* @__PURE__ */ Math.pow(2, 32);
  94. const K = /* @__PURE__ */ Array.from({ length: 64 }, (_, i) => Math.floor(p32 * Math.abs(Math.sin(i + 1))));
  95. /** md5 initial state: same as sha1, but 4 u32 instead of 5. */
  96. const MD5_IV = /* @__PURE__ */ SHA1_IV.slice(0, 4);
  97. // Reusable temporary buffer
  98. const MD5_W = /* @__PURE__ */ new Uint32Array(16);
  99. /** MD5 legacy hash class. */
  100. class MD5 extends _md_ts_1.HashMD {
  101. constructor() {
  102. super(64, 16, 8, true);
  103. this.A = MD5_IV[0] | 0;
  104. this.B = MD5_IV[1] | 0;
  105. this.C = MD5_IV[2] | 0;
  106. this.D = MD5_IV[3] | 0;
  107. }
  108. get() {
  109. const { A, B, C, D } = this;
  110. return [A, B, C, D];
  111. }
  112. set(A, B, C, D) {
  113. this.A = A | 0;
  114. this.B = B | 0;
  115. this.C = C | 0;
  116. this.D = D | 0;
  117. }
  118. process(view, offset) {
  119. for (let i = 0; i < 16; i++, offset += 4)
  120. MD5_W[i] = view.getUint32(offset, true);
  121. // Compression function main loop, 64 rounds
  122. let { A, B, C, D } = this;
  123. for (let i = 0; i < 64; i++) {
  124. let F, g, s;
  125. if (i < 16) {
  126. F = (0, _md_ts_1.Chi)(B, C, D);
  127. g = i;
  128. s = [7, 12, 17, 22];
  129. }
  130. else if (i < 32) {
  131. F = (0, _md_ts_1.Chi)(D, B, C);
  132. g = (5 * i + 1) % 16;
  133. s = [5, 9, 14, 20];
  134. }
  135. else if (i < 48) {
  136. F = B ^ C ^ D;
  137. g = (3 * i + 5) % 16;
  138. s = [4, 11, 16, 23];
  139. }
  140. else {
  141. F = C ^ (B | ~D);
  142. g = (7 * i) % 16;
  143. s = [6, 10, 15, 21];
  144. }
  145. F = F + A + K[i] + MD5_W[g];
  146. A = D;
  147. D = C;
  148. C = B;
  149. B = B + (0, utils_ts_1.rotl)(F, s[i % 4]);
  150. }
  151. // Add the compressed chunk to the current hash value
  152. A = (A + this.A) | 0;
  153. B = (B + this.B) | 0;
  154. C = (C + this.C) | 0;
  155. D = (D + this.D) | 0;
  156. this.set(A, B, C, D);
  157. }
  158. roundClean() {
  159. (0, utils_ts_1.clean)(MD5_W);
  160. }
  161. destroy() {
  162. this.set(0, 0, 0, 0);
  163. (0, utils_ts_1.clean)(this.buffer);
  164. }
  165. }
  166. exports.MD5 = MD5;
  167. /**
  168. * MD5 (RFC 1321) legacy hash function. It was cryptographically broken.
  169. * MD5 architecture is similar to SHA1, with some differences:
  170. * - Reduced output length: 16 bytes (128 bit) instead of 20
  171. * - 64 rounds, instead of 80
  172. * - Little-endian: could be faster, but will require more code
  173. * - Non-linear index selection: huge speed-up for unroll
  174. * - Per round constants: more memory accesses, additional speed-up for unroll
  175. */
  176. exports.md5 = (0, utils_ts_1.createHasher)(() => new MD5());
  177. // RIPEMD-160
  178. const Rho160 = /* @__PURE__ */ Uint8Array.from([
  179. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  180. ]);
  181. const Id160 = /* @__PURE__ */ (() => Uint8Array.from(new Array(16).fill(0).map((_, i) => i)))();
  182. const Pi160 = /* @__PURE__ */ (() => Id160.map((i) => (9 * i + 5) % 16))();
  183. const idxLR = /* @__PURE__ */ (() => {
  184. const L = [Id160];
  185. const R = [Pi160];
  186. const res = [L, R];
  187. for (let i = 0; i < 4; i++)
  188. for (let j of res)
  189. j.push(j[i].map((k) => Rho160[k]));
  190. return res;
  191. })();
  192. const idxL = /* @__PURE__ */ (() => idxLR[0])();
  193. const idxR = /* @__PURE__ */ (() => idxLR[1])();
  194. // const [idxL, idxR] = idxLR;
  195. const shifts160 = /* @__PURE__ */ [
  196. [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
  197. [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
  198. [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
  199. [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
  200. [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5],
  201. ].map((i) => Uint8Array.from(i));
  202. const shiftsL160 = /* @__PURE__ */ idxL.map((idx, i) => idx.map((j) => shifts160[i][j]));
  203. const shiftsR160 = /* @__PURE__ */ idxR.map((idx, i) => idx.map((j) => shifts160[i][j]));
  204. const Kl160 = /* @__PURE__ */ Uint32Array.from([
  205. 0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e,
  206. ]);
  207. const Kr160 = /* @__PURE__ */ Uint32Array.from([
  208. 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000,
  209. ]);
  210. // It's called f() in spec.
  211. function ripemd_f(group, x, y, z) {
  212. if (group === 0)
  213. return x ^ y ^ z;
  214. if (group === 1)
  215. return (x & y) | (~x & z);
  216. if (group === 2)
  217. return (x | ~y) ^ z;
  218. if (group === 3)
  219. return (x & z) | (y & ~z);
  220. return x ^ (y | ~z);
  221. }
  222. // Reusable temporary buffer
  223. const BUF_160 = /* @__PURE__ */ new Uint32Array(16);
  224. class RIPEMD160 extends _md_ts_1.HashMD {
  225. constructor() {
  226. super(64, 20, 8, true);
  227. this.h0 = 0x67452301 | 0;
  228. this.h1 = 0xefcdab89 | 0;
  229. this.h2 = 0x98badcfe | 0;
  230. this.h3 = 0x10325476 | 0;
  231. this.h4 = 0xc3d2e1f0 | 0;
  232. }
  233. get() {
  234. const { h0, h1, h2, h3, h4 } = this;
  235. return [h0, h1, h2, h3, h4];
  236. }
  237. set(h0, h1, h2, h3, h4) {
  238. this.h0 = h0 | 0;
  239. this.h1 = h1 | 0;
  240. this.h2 = h2 | 0;
  241. this.h3 = h3 | 0;
  242. this.h4 = h4 | 0;
  243. }
  244. process(view, offset) {
  245. for (let i = 0; i < 16; i++, offset += 4)
  246. BUF_160[i] = view.getUint32(offset, true);
  247. // prettier-ignore
  248. 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;
  249. // Instead of iterating 0 to 80, we split it into 5 groups
  250. // And use the groups in constants, functions, etc. Much simpler
  251. for (let group = 0; group < 5; group++) {
  252. const rGroup = 4 - group;
  253. const hbl = Kl160[group], hbr = Kr160[group]; // prettier-ignore
  254. const rl = idxL[group], rr = idxR[group]; // prettier-ignore
  255. const sl = shiftsL160[group], sr = shiftsR160[group]; // prettier-ignore
  256. for (let i = 0; i < 16; i++) {
  257. const tl = ((0, utils_ts_1.rotl)(al + ripemd_f(group, bl, cl, dl) + BUF_160[rl[i]] + hbl, sl[i]) + el) | 0;
  258. al = el, el = dl, dl = (0, utils_ts_1.rotl)(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
  259. }
  260. // 2 loops are 10% faster
  261. for (let i = 0; i < 16; i++) {
  262. const tr = ((0, utils_ts_1.rotl)(ar + ripemd_f(rGroup, br, cr, dr) + BUF_160[rr[i]] + hbr, sr[i]) + er) | 0;
  263. ar = er, er = dr, dr = (0, utils_ts_1.rotl)(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
  264. }
  265. }
  266. // Add the compressed chunk to the current hash value
  267. 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);
  268. }
  269. roundClean() {
  270. (0, utils_ts_1.clean)(BUF_160);
  271. }
  272. destroy() {
  273. this.destroyed = true;
  274. (0, utils_ts_1.clean)(this.buffer);
  275. this.set(0, 0, 0, 0, 0);
  276. }
  277. }
  278. exports.RIPEMD160 = RIPEMD160;
  279. /**
  280. * RIPEMD-160 - a legacy hash function from 1990s.
  281. * * https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
  282. * * https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
  283. */
  284. exports.ripemd160 = (0, utils_ts_1.createHasher)(() => new RIPEMD160());
  285. //# sourceMappingURL=legacy.js.map