sha1.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. // Adapted from Chris Veness' SHA1 code at
  7. // http://www.movable-type.co.uk/scripts/sha1.html
  8. function f(s, x, y, z) {
  9. switch (s) {
  10. case 0:
  11. return x & y ^ ~x & z;
  12. case 1:
  13. return x ^ y ^ z;
  14. case 2:
  15. return x & y ^ x & z ^ y & z;
  16. case 3:
  17. return x ^ y ^ z;
  18. }
  19. }
  20. function ROTL(x, n) {
  21. return x << n | x >>> 32 - n;
  22. }
  23. function sha1(bytes) {
  24. const K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
  25. const H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
  26. if (typeof bytes === 'string') {
  27. const msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
  28. bytes = [];
  29. for (let i = 0; i < msg.length; ++i) {
  30. bytes.push(msg.charCodeAt(i));
  31. }
  32. } else if (!Array.isArray(bytes)) {
  33. // Convert Array-like to Array
  34. bytes = Array.prototype.slice.call(bytes);
  35. }
  36. bytes.push(0x80);
  37. const l = bytes.length / 4 + 2;
  38. const N = Math.ceil(l / 16);
  39. const M = new Array(N);
  40. for (let i = 0; i < N; ++i) {
  41. const arr = new Uint32Array(16);
  42. for (let j = 0; j < 16; ++j) {
  43. arr[j] = bytes[i * 64 + j * 4] << 24 | bytes[i * 64 + j * 4 + 1] << 16 | bytes[i * 64 + j * 4 + 2] << 8 | bytes[i * 64 + j * 4 + 3];
  44. }
  45. M[i] = arr;
  46. }
  47. M[N - 1][14] = (bytes.length - 1) * 8 / Math.pow(2, 32);
  48. M[N - 1][14] = Math.floor(M[N - 1][14]);
  49. M[N - 1][15] = (bytes.length - 1) * 8 & 0xffffffff;
  50. for (let i = 0; i < N; ++i) {
  51. const W = new Uint32Array(80);
  52. for (let t = 0; t < 16; ++t) {
  53. W[t] = M[i][t];
  54. }
  55. for (let t = 16; t < 80; ++t) {
  56. W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
  57. }
  58. let a = H[0];
  59. let b = H[1];
  60. let c = H[2];
  61. let d = H[3];
  62. let e = H[4];
  63. for (let t = 0; t < 80; ++t) {
  64. const s = Math.floor(t / 20);
  65. const T = ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t] >>> 0;
  66. e = d;
  67. d = c;
  68. c = ROTL(b, 30) >>> 0;
  69. b = a;
  70. a = T;
  71. }
  72. H[0] = H[0] + a >>> 0;
  73. H[1] = H[1] + b >>> 0;
  74. H[2] = H[2] + c >>> 0;
  75. H[3] = H[3] + d >>> 0;
  76. H[4] = H[4] + e >>> 0;
  77. }
  78. return [H[0] >> 24 & 0xff, H[0] >> 16 & 0xff, H[0] >> 8 & 0xff, H[0] & 0xff, H[1] >> 24 & 0xff, H[1] >> 16 & 0xff, H[1] >> 8 & 0xff, H[1] & 0xff, H[2] >> 24 & 0xff, H[2] >> 16 & 0xff, H[2] >> 8 & 0xff, H[2] & 0xff, H[3] >> 24 & 0xff, H[3] >> 16 & 0xff, H[3] >> 8 & 0xff, H[3] & 0xff, H[4] >> 24 & 0xff, H[4] >> 16 & 0xff, H[4] >> 8 & 0xff, H[4] & 0xff];
  79. }
  80. var _default = sha1;
  81. exports.default = _default;