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