-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
277 lines (234 loc) · 6.28 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*! https://litejs.com/MIT-LICENSE.txt */
// node v15.7.0 introduced base64url encoding.
!function(exports) {
"use strict";
var crypto = exports.crypto || (exports.crypto = {})
function intToHex(arr) {
for (var i = arr.length; i--;) arr[i] = ("0000000" + (arr[i] >>> 0).toString(16)).slice(-8)
return arr.join("")
}
function strToInt(str) {
str = unescape(encodeURIComponent(str))
var i = 0
, arr = []
, len = arr.len = str.length
for (; i < len;) {
arr[i>>2] = str.charCodeAt(i++)<<24 |
str.charCodeAt(i++)<<16 |
str.charCodeAt(i++)<<8 |
str.charCodeAt(i++)
}
return arr
}
function rotL(val, count) {
return (val << count) | (val >>> (32 - count))
}
//** HMAC
function hmac(hasher, _key, _txt) {
hasher = hasher == "sha256" ? sha256 : sha1
var i = 0
, ipad = []
, opad = []
, key = (_key.length > 64 ? hasher : strToInt)(_key)
, txt = typeof _txt == "string" ? strToInt(_txt) : _txt
, len = txt.len || txt.length * 4
for (; i < 16;) {
ipad[i] = key[i]^0x36363636
opad[i] = key[i++]^0x5c5c5c5c
}
return hasher(opad.concat(hasher(ipad.concat(txt), 64 + len)))
}
crypto.hmac = function(digest, key, message) {
return intToHex(hmac(digest, key, message))
}
//*/
/**
* A minimum iteration count of 1,000 is recommended.
* For especially critical keys,
* or for very powerful systems
* or systems where user-perceived performance is not critical,
* an iteration count of 10,000,000 may be appropriate.
*
* PBKDF2 is always used with HMAC,
* which is itself a construction which is built over
* an underlying hash function.
* So when we say "PBKDF2 with SHA-1",
* we actually mean "PBKDF2 with HMAC with SHA-1".
*/
//** PBKDF2
// pbkdf2(sha256, this, salt, count, length || 32)
// crypto.pbkdf2('secret', 'salt', 4096, 512, 'sha256', function(err, key) {
// $PBKDF2$HMACSHA1:1000:akrvug==$Zi+c82tnjpcrRmUAHRd8h4ZRR5M=
crypto.pbkdf2 = pbkdf2
// crypto.pbkdf2('secret', 'salt', 4096, 512, 'sha256', function(err, key) {
function pbkdf2(secret, salt, count, length, digest) {
var u, ui, i, j
, k = 1
, out = []
, wlen = length>>2 || 5
for (; out.length < wlen; k++) {
u = ui = hmac(digest, secret, salt+String.fromCharCode(k >> 24 & 0xF, k >> 16 & 0xF, k >> 8 & 0xF, k & 0xF))
for (i = count || 1000; --i;) {
ui = hmac(digest, secret, ui)
for (j = ui.length; j--;) u[j] ^= ui[j]
}
//out = out.concat(u)
out.push.apply(out, u)
}
return intToHex(out).slice(0, length*2 || 40)
}
//*/
//** HOTP
crypto.hotp = hotp
function hotp(key, opts) {
opts = Object.assign({
counter: typeof opts === "number" ? opts : 0,
digits: 6,
algo: "sha1"
}, opts)
var arr = hmac(
opts.algo,
opts.enc == "base32" ? base32Decode(key) : key,
[0, opts.counter]
)
, offset = arr[arr.length-1]&15
return ("0000000" + (0x7FFFFFFF & parseInt(intToHex(arr).substr(2*offset, 8), 16))).slice(-opts.digits)
}
//*/
//** TOTP
crypto.totp = totp
function totp(key, opts) {
opts = Object.assign({
time: typeof opts === "number" ? opts : Math.floor(Date.now()/1000),
t0: 0,
step: 30
}, opts)
opts.counter = Math.floor((opts.time - opts.t0)/opts.step)
return hotp(key, opts)
}
// RFC 4648 Base32
function base32Decode(str) {
return str.replace(/./g, function(c) {
c = c.charCodeAt()
return (c - (c < 48 ? c : c < 58 ? -8 : c < 65 ? c : c < 97 ? 33 : 65)).toString(2).slice(-5)
})
.replace(/.{1,8}/g, function(c) {
return String.fromCharCode(parseInt(c, 2))
})
}
//*/
function shaInit(bin, len) {
if (typeof bin == "string") {
bin = strToInt(bin)
len = bin.len
} else len = len || bin.length<<2
bin[len>>2] |= 0x80 << (24 - (31 & (len<<=3)))
bin[((len + 64 >> 9) << 4) + 15] = len
return bin
}
//** sha1
function sha1(data, _len) {
var a, b, c, d, e, t, j
, i = 0
, w = []
, A = 0x67452301
, B = 0xefcdab89
, C = 0x98badcfe
, D = 0x10325476
, E = 0xc3d2e1f0
, bin = shaInit(data, _len)
, len = bin.length
for (; i < len; i+=16, A+=a, B+=b, C+=c, D+=d, E+=e) {
for (j=0, a=A, b=B, c=C, d=D, e=E; j < 80;) {
w[j] = j < 16 ? bin[i+j] : rotL(w[j-3]^w[j-8]^w[j-14]^w[j-16], 1)
t = (
j < 20 ? ((b&c)|(~b&d)) + 0x5A827999 :
j < 40 ? (b^c^d) + 0x6ED9EBA1 :
j < 60 ? ((b&c)|(b&d)|(c&d)) + 0x8F1BBCDC :
(b^c^d) + 0xCA62C1D6
) + rotL(a, 5) + e + (w[j++]|0)
e = d
d = c
c = rotL(b,30)
b = a
a = t|0
}
}
return [A, B, C, D, E]
}
crypto.sha1 = function(data) {
return intToHex(sha1(data))
}
//*/
//** sha256
var initial_map = []
, constants_map = []
function buildMaps() {
// getFractionalBits
function powFraction(c, e) {
c = Math.pow(c, e)
return (c - (c>>>0)) * 0x100000000 | 0
}
outer: for (var b = 0, c = 2, d; b < 64; c++) {
// isPrime
for (d = 2; d * d <= c; d++) if (c % d === 0) continue outer;
if (b < 8) initial_map[b] = powFraction(c, 0.5)
constants_map[b++] = powFraction(c, 1 / 3)
}
}
function sha256(data, _len, is224) {
if (!initial_map[0]) buildMaps()
var a, b, c, d, e, f, g, h, t1, t2, j
, i = 0
, w = []
, A = initial_map[0]
, B = initial_map[1]
, C = initial_map[2]
, D = initial_map[3]
, E = initial_map[4]
, F = initial_map[5]
, G = initial_map[6]
, H = initial_map[7]
, bin = shaInit(data, _len)
, len = bin.length
, K = constants_map
if (is224) {
A = 0xc1059ed8
B = 0x367cd507
C = 0x3070dd17
D = 0xf70e5939
E = 0xffc00b31
F = 0x68581511
G = 0x64f98fa7
H = 0xbefa4fa4
}
for (; i < len; i+=16, A+=a, B+=b, C+=c, D+=d, E+=e, F+=f, G+=g, H+=h) {
for (j=0, a=A, b=B, c=C, d=D, e=E, f=F, g=G, h=H; j < 64; ) {
if (j < 16) w[j] = bin[i+j]
else {
t1 = w[j-2]
t2 = w[j-15]
w[j] = (rotL(t1, 15)^rotL(t1, 13)^t1>>>10) + (w[j-7]|0) + (rotL(t2, 25)^rotL(t2, 14)^t2>>>3) + (w[j-16]|0)
}
t1 = (w[j]|0) + h + (rotL(e, 26)^rotL(e, 21)^rotL(e, 7)) + ((e&f)^((~e)&g)) + K[j++]
t2 = (rotL(a, 30)^rotL(a, 19)^rotL(a, 10)) + ((a&b)^(a&c)^(b&c))
h = g
g = f
f = e
e = (d + t1)|0
d = c
c = b
b = a
a = (t1 + t2)|0
}
}
return [A, B, C, D, E, F, G, H]
}
crypto.sha224 = function(data) {
return intToHex(sha256(data, 0, 1)).slice(0, -8)
}
crypto.sha256 = function(data) {
return intToHex(sha256(data))
}
//*/
}(this) // jshint ignore:line