1export const sha1 = (str) => {
2
3 var _rotLeft = function (n, s) {
4 var t4 = (n << s) | (n >>> (32 - s))
5 return t4
6 }
7
8 var _cvtHex = function (val) {
9 var str = ''
10 var i
11 var v
12
13 for (i = 7; i >= 0; i--) {
14 v = (val >>> (i * 4)) & 0x0f
15 str += v.toString(16)
16 }
17 return str
18 }
19
20 var blockstart
21 var i, j
22 var W = new Array(80)
23 var H0 = 0x67452301
24 var H1 = 0xEFCDAB89
25 var H2 = 0x98BADCFE
26 var H3 = 0x10325476
27 var H4 = 0xC3D2E1F0
28 var A, B, C, D, E
29 var temp
30
31
32 str = unescape(encodeURIComponent(str))
33 var strLen = str.length
34
35 var wordArray = []
36 for (i = 0; i < strLen - 3; i += 4) {
37 j = str.charCodeAt(i) << 24 |
38 str.charCodeAt(i + 1) << 16 |
39 str.charCodeAt(i + 2) << 8 |
40 str.charCodeAt(i + 3)
41 wordArray.push(j)
42 }
43
44 switch (strLen % 4) {
45 case 0:
46 i = 0x080000000
47 break
48 case 1:
49 i = str.charCodeAt(strLen - 1) << 24 | 0x0800000
50 break
51 case 2:
52 i = str.charCodeAt(strLen - 2) << 24 | str.charCodeAt(strLen - 1) << 16 | 0x08000
53 break
54 case 3:
55 i = str.charCodeAt(strLen - 3) << 24 |
56 str.charCodeAt(strLen - 2) << 16 |
57 str.charCodeAt(strLen - 1) <<
58 8 | 0x80
59 break
60 }
61
62 wordArray.push(i)
63
64 while ((wordArray.length % 16) !== 14) {
65 wordArray.push(0)
66 }
67
68 wordArray.push(strLen >>> 29)
69 wordArray.push((strLen << 3) & 0x0ffffffff)
70
71 for (blockstart = 0; blockstart < wordArray.length; blockstart += 16) {
72 for (i = 0; i < 16; i++) {
73 W[i] = wordArray[blockstart + i]
74 }
75 for (i = 16; i <= 79; i++) {
76 W[i] = _rotLeft(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1)
77 }
78
79 A = H0
80 B = H1
81 C = H2
82 D = H3
83 E = H4
84
85 for (i = 0; i <= 19; i++) {
86 temp = (_rotLeft(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff
87 E = D
88 D = C
89 C = _rotLeft(B, 30)
90 B = A
91 A = temp
92 }
93
94 for (i = 20; i <= 39; i++) {
95 temp = (_rotLeft(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff
96 E = D
97 D = C
98 C = _rotLeft(B, 30)
99 B = A
100 A = temp
101 }
102
103 for (i = 40; i <= 59; i++) {
104 temp = (_rotLeft(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff
105 E = D
106 D = C
107 C = _rotLeft(B, 30)
108 B = A
109 A = temp
110 }
111
112 for (i = 60; i <= 79; i++) {
113 temp = (_rotLeft(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff
114 E = D
115 D = C
116 C = _rotLeft(B, 30)
117 B = A
118 A = temp
119 }
120
121 H0 = (H0 + A) & 0x0ffffffff
122 H1 = (H1 + B) & 0x0ffffffff
123 H2 = (H2 + C) & 0x0ffffffff
124 H3 = (H3 + D) & 0x0ffffffff
125 H4 = (H4 + E) & 0x0ffffffff
126 }
127
128 temp = _cvtHex(H0) + _cvtHex(H1) + _cvtHex(H2) + _cvtHex(H3) + _cvtHex(H4)
129 return temp.toLowerCase()
130 }