sha1.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.sha1 = void 0;
  4. const _md_js_1 = require("./_md.js");
  5. const utils_js_1 = require("./utils.js");
  6. // SHA1 (RFC 3174) was cryptographically broken. It's still used. Don't use it for a new protocol.
  7. // Initial state
  8. const SHA1_IV = /* @__PURE__ */ new Uint32Array([
  9. 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
  10. ]);
  11. // Temporary buffer, not used to store anything between runs
  12. // Named this way because it matches specification.
  13. const SHA1_W = /* @__PURE__ */ new Uint32Array(80);
  14. class SHA1 extends _md_js_1.HashMD {
  15. constructor() {
  16. super(64, 20, 8, false);
  17. this.A = SHA1_IV[0] | 0;
  18. this.B = SHA1_IV[1] | 0;
  19. this.C = SHA1_IV[2] | 0;
  20. this.D = SHA1_IV[3] | 0;
  21. this.E = SHA1_IV[4] | 0;
  22. }
  23. get() {
  24. const { A, B, C, D, E } = this;
  25. return [A, B, C, D, E];
  26. }
  27. set(A, B, C, D, E) {
  28. this.A = A | 0;
  29. this.B = B | 0;
  30. this.C = C | 0;
  31. this.D = D | 0;
  32. this.E = E | 0;
  33. }
  34. process(view, offset) {
  35. for (let i = 0; i < 16; i++, offset += 4)
  36. SHA1_W[i] = view.getUint32(offset, false);
  37. for (let i = 16; i < 80; i++)
  38. SHA1_W[i] = (0, utils_js_1.rotl)(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
  39. // Compression function main loop, 80 rounds
  40. let { A, B, C, D, E } = this;
  41. for (let i = 0; i < 80; i++) {
  42. let F, K;
  43. if (i < 20) {
  44. F = (0, _md_js_1.Chi)(B, C, D);
  45. K = 0x5a827999;
  46. }
  47. else if (i < 40) {
  48. F = B ^ C ^ D;
  49. K = 0x6ed9eba1;
  50. }
  51. else if (i < 60) {
  52. F = (0, _md_js_1.Maj)(B, C, D);
  53. K = 0x8f1bbcdc;
  54. }
  55. else {
  56. F = B ^ C ^ D;
  57. K = 0xca62c1d6;
  58. }
  59. const T = ((0, utils_js_1.rotl)(A, 5) + F + E + K + SHA1_W[i]) | 0;
  60. E = D;
  61. D = C;
  62. C = (0, utils_js_1.rotl)(B, 30);
  63. B = A;
  64. A = T;
  65. }
  66. // Add the compressed chunk to the current hash value
  67. A = (A + this.A) | 0;
  68. B = (B + this.B) | 0;
  69. C = (C + this.C) | 0;
  70. D = (D + this.D) | 0;
  71. E = (E + this.E) | 0;
  72. this.set(A, B, C, D, E);
  73. }
  74. roundClean() {
  75. SHA1_W.fill(0);
  76. }
  77. destroy() {
  78. this.set(0, 0, 0, 0, 0);
  79. this.buffer.fill(0);
  80. }
  81. }
  82. exports.sha1 = (0, utils_js_1.wrapConstructor)(() => new SHA1());
  83. //# sourceMappingURL=sha1.js.map