[79a0317] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | function f(s, x, y, z) {
|
---|
| 4 | switch (s) {
|
---|
| 5 | case 0:
|
---|
| 6 | return (x & y) ^ (~x & z);
|
---|
| 7 | case 1:
|
---|
| 8 | return x ^ y ^ z;
|
---|
| 9 | case 2:
|
---|
| 10 | return (x & y) ^ (x & z) ^ (y & z);
|
---|
| 11 | case 3:
|
---|
| 12 | return x ^ y ^ z;
|
---|
| 13 | }
|
---|
| 14 | }
|
---|
| 15 | function ROTL(x, n) {
|
---|
| 16 | return (x << n) | (x >>> (32 - n));
|
---|
| 17 | }
|
---|
| 18 | function sha1(bytes) {
|
---|
| 19 | const K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];
|
---|
| 20 | const H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
|
---|
| 21 | const newBytes = new Uint8Array(bytes.length + 1);
|
---|
| 22 | newBytes.set(bytes);
|
---|
| 23 | newBytes[bytes.length] = 0x80;
|
---|
| 24 | bytes = newBytes;
|
---|
| 25 | const l = bytes.length / 4 + 2;
|
---|
| 26 | const N = Math.ceil(l / 16);
|
---|
| 27 | const M = new Array(N);
|
---|
| 28 | for (let i = 0; i < N; ++i) {
|
---|
| 29 | const arr = new Uint32Array(16);
|
---|
| 30 | for (let j = 0; j < 16; ++j) {
|
---|
| 31 | arr[j] =
|
---|
| 32 | (bytes[i * 64 + j * 4] << 24) |
|
---|
| 33 | (bytes[i * 64 + j * 4 + 1] << 16) |
|
---|
| 34 | (bytes[i * 64 + j * 4 + 2] << 8) |
|
---|
| 35 | bytes[i * 64 + j * 4 + 3];
|
---|
| 36 | }
|
---|
| 37 | M[i] = arr;
|
---|
| 38 | }
|
---|
| 39 | M[N - 1][14] = ((bytes.length - 1) * 8) / Math.pow(2, 32);
|
---|
| 40 | M[N - 1][14] = Math.floor(M[N - 1][14]);
|
---|
| 41 | M[N - 1][15] = ((bytes.length - 1) * 8) & 0xffffffff;
|
---|
| 42 | for (let i = 0; i < N; ++i) {
|
---|
| 43 | const W = new Uint32Array(80);
|
---|
| 44 | for (let t = 0; t < 16; ++t) {
|
---|
| 45 | W[t] = M[i][t];
|
---|
| 46 | }
|
---|
| 47 | for (let t = 16; t < 80; ++t) {
|
---|
| 48 | W[t] = ROTL(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
|
---|
| 49 | }
|
---|
| 50 | let a = H[0];
|
---|
| 51 | let b = H[1];
|
---|
| 52 | let c = H[2];
|
---|
| 53 | let d = H[3];
|
---|
| 54 | let e = H[4];
|
---|
| 55 | for (let t = 0; t < 80; ++t) {
|
---|
| 56 | const s = Math.floor(t / 20);
|
---|
| 57 | const T = (ROTL(a, 5) + f(s, b, c, d) + e + K[s] + W[t]) >>> 0;
|
---|
| 58 | e = d;
|
---|
| 59 | d = c;
|
---|
| 60 | c = ROTL(b, 30) >>> 0;
|
---|
| 61 | b = a;
|
---|
| 62 | a = T;
|
---|
| 63 | }
|
---|
| 64 | H[0] = (H[0] + a) >>> 0;
|
---|
| 65 | H[1] = (H[1] + b) >>> 0;
|
---|
| 66 | H[2] = (H[2] + c) >>> 0;
|
---|
| 67 | H[3] = (H[3] + d) >>> 0;
|
---|
| 68 | H[4] = (H[4] + e) >>> 0;
|
---|
| 69 | }
|
---|
| 70 | return Uint8Array.of(H[0] >> 24, H[0] >> 16, H[0] >> 8, H[0], H[1] >> 24, H[1] >> 16, H[1] >> 8, H[1], H[2] >> 24, H[2] >> 16, H[2] >> 8, H[2], H[3] >> 24, H[3] >> 16, H[3] >> 8, H[3], H[4] >> 24, H[4] >> 16, H[4] >> 8, H[4]);
|
---|
| 71 | }
|
---|
| 72 | exports.default = sha1;
|
---|