-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenotp.js
634 lines (550 loc) · 18.1 KB
/
genotp.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
const RFC4648 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
const RFC4648_HEX = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
const CROCKFORD = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
// https://github.com/jbt/tiny-hashes/
md5=function(){for(var m=[],l=0;64>l;)m[l]=0|4294967296*Math.abs(Math.sin(++l));return function(c){var e,g,f,a,h=[];c=unescape(encodeURI(c));for(var b=c.length,k=[e=1732584193,g=-271733879,~e,~g],d=0;d<=b;)h[d>>2]|=(c.charCodeAt(d)||128)<<8*(d++%4);h[c=16*(b+8>>6)+14]=8*b;for(d=0;d<c;d+=16){b=k;for(a=0;64>a;)b=[f=b[3],(e=b[1]|0)+((f=b[0]+[e&(g=b[2])|~e&f,f&e|~f&g,e^g^f,g^(e|~f)][b=a>>4]+(m[a]+(h[[a,5*a+1,3*a+5,7*a][b]%16+d]|0)))<<(b=[7,12,17,22,5,9,14,20,4,11,16,23,6,10,15,21][4*b+a++%4])|f>>>32-b),e,g];for(a=4;a;)k[--a]=k[a]+b[a]}for(c="";32>a;)c+=(k[a>>3]>>4*(1^a++&7)&15).toString(16);return c}}();
function hex2bin(r){for(var n=[],t=0;t<r.length-1;t+=2)n.push(parseInt(r.substr(t,2),16));return String.fromCharCode.apply(String,n)}
function bin2hex(s) {var i, l, o = "", n; s += ""; for (i = 0, l = s.length; i < l; i++) {n = s.charCodeAt(i).toString(16); o += n.length < 2 ? "0" + n : n;} return o;}
function bin2Uint8Array(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function uint8Array2bin(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function readChar (alphabet, char) {
var idx = alphabet.indexOf(char);
if (idx === -1) {
throw new Error('Invalid character found: ' + char)
}
return idx
}
var base32Decode = function base32Decode (input, variant) {
var alphabet;
switch (variant) {
case 'RFC3548':
case 'RFC4648':
alphabet = RFC4648;
input = input.replace(/=+$/, '');
break
case 'RFC4648-HEX':
alphabet = RFC4648_HEX;
input = input.replace(/=+$/, '');
break
case 'Crockford':
alphabet = CROCKFORD;
input = input.toUpperCase().replace(/O/g, '0').replace(/[IL]/g, '1');
break
default:
throw new Error('Unknown base32 variant: ' + variant)
}
var length = input.length;
var bits = 0;
var value = 0;
var index = 0;
var output = new Uint8Array((length * 5 / 8) | 0);
for (var i = 0; i < length; i++) {
value = (value << 5) | readChar(alphabet, input[i]);
bits += 5;
if (bits >= 8) {
output[index++] = (value >>> (bits - 8)) & 255;
bits -= 8;
}
}
return output.buffer
};
function toDataView (data) {
if (data instanceof Int8Array || data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
return new DataView(data.buffer, data.byteOffset, data.byteLength)
}
if (data instanceof ArrayBuffer) {
return new DataView(data)
}
throw new TypeError('Expected `data` to be an ArrayBuffer, Buffer, Int8Array, Uint8Array or Uint8ClampedArray')
}
function base32Encode (data, variant, options) {
options = options || {};
let alphabet, defaultPadding;
switch (variant) {
case 'RFC3548':
case 'RFC4648':
alphabet = RFC4648;
defaultPadding = true;
break
case 'RFC4648-HEX':
alphabet = RFC4648_HEX;
defaultPadding = true;
break
case 'Crockford':
alphabet = CROCKFORD;
defaultPadding = false;
break
default:
throw new Error('Unknown base32 variant: ' + variant)
}
const padding = (options.padding !== undefined ? options.padding : defaultPadding);
const view = toDataView(data);
let bits = 0;
let value = 0;
let output = '';
for (let i = 0; i < view.byteLength; i++) {
value = (value << 8) | view.getUint8(i);
bits += 8;
while (bits >= 5) {
output += alphabet[(value >>> (bits - 5)) & 31];
bits -= 5;
}
}
if (bits > 0) {
output += alphabet[(value << (5 - bits)) & 31];
}
if (padding) {
while ((output.length % 8) !== 0) {
output += '=';
}
}
return output
}
function createCommonjsModule(fn, basedir, module) {
return module = {
path: basedir,
exports: {},
require: function (path, base) {
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
}
}, fn(module, module.exports), module.exports;
}
function commonjsRequire () {
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
}
var crypt = createCommonjsModule(function (module) {
(function() {
var base64map
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
crypt = {
// Bit-wise rotation left
rotl: function(n, b) {
return (n << b) | (n >>> (32 - b));
},
// Bit-wise rotation right
rotr: function(n, b) {
return (n << (32 - b)) | (n >>> b);
},
// Swap big-endian to little-endian and vice versa
endian: function(n) {
// If number given, swap endian
if (n.constructor == Number) {
return crypt.rotl(n, 8) & 0x00FF00FF | crypt.rotl(n, 24) & 0xFF00FF00;
}
// Else, assume array and swap all items
for (var i = 0; i < n.length; i++)
n[i] = crypt.endian(n[i]);
return n;
},
// Generate an array of any length of random bytes
randomBytes: function(n) {
for (var bytes = []; n > 0; n--)
bytes.push(Math.floor(Math.random() * 256));
return bytes;
},
// Convert a byte array to big-endian 32-bit words
bytesToWords: function(bytes) {
for (var words = [], i = 0, b = 0; i < bytes.length; i++, b += 8)
words[b >>> 5] |= bytes[i] << (24 - b % 32);
return words;
},
// Convert big-endian 32-bit words to a byte array
wordsToBytes: function(words) {
for (var bytes = [], b = 0; b < words.length * 32; b += 8)
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
return bytes;
},
// Convert a byte array to a hex string
bytesToHex: function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join('');
},
// Convert a hex string to a byte array
hexToBytes: function(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
},
// Convert a byte array to a base-64 string
bytesToBase64: function(bytes) {
for (var base64 = [], i = 0; i < bytes.length; i += 3) {
var triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
for (var j = 0; j < 4; j++)
if (i * 8 + j * 6 <= bytes.length * 8)
base64.push(base64map.charAt((triplet >>> 6 * (3 - j)) & 0x3F));
else
base64.push('=');
}
return base64.join('');
},
// Convert a base-64 string to a byte array
base64ToBytes: function(base64) {
// Remove non-base-64 characters
base64 = base64.replace(/[^A-Z0-9+\/]/ig, '');
for (var bytes = [], i = 0, imod4 = 0; i < base64.length;
imod4 = ++i % 4) {
if (imod4 == 0) continue;
bytes.push(((base64map.indexOf(base64.charAt(i - 1))
& (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2))
| (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)));
}
return bytes;
}
};
module.exports = crypt;
})();
});
var charenc = {
// UTF-8 encoding
utf8: {
// Convert a string to a byte array
stringToBytes: function(str) {
return charenc.bin.stringToBytes(unescape(encodeURIComponent(str)));
},
// Convert a byte array to a string
bytesToString: function(bytes) {
return decodeURIComponent(escape(charenc.bin.bytesToString(bytes)));
}
},
// Binary encoding
bin: {
// Convert a string to a byte array
stringToBytes: function(str) {
for (var bytes = [], i = 0; i < str.length; i++)
bytes.push(str.charCodeAt(i) & 0xFF);
return bytes;
},
// Convert a byte array to a string
bytesToString: function(bytes) {
for (var str = [], i = 0; i < bytes.length; i++)
str.push(String.fromCharCode(bytes[i]));
return str.join('');
}
}
};
var charenc_1 = charenc;
var sha1 = createCommonjsModule(function (module) {
(function() {
var crypt$1 = crypt,
utf8 = charenc_1.utf8,
bin = charenc_1.bin,
// The core
sha1 = function (message) {
// Convert to byte array
if (message.constructor == String)
message = utf8.stringToBytes(message);
else if (typeof Buffer !== 'undefined' && typeof Buffer.isBuffer == 'function' && Buffer.isBuffer(message))
message = Array.prototype.slice.call(message, 0);
else if (!Array.isArray(message))
message = message.toString();
// otherwise assume byte array
var m = crypt$1.bytesToWords(message),
l = message.length * 8,
w = [],
H0 = 1732584193,
H1 = -271733879,
H2 = -1732584194,
H3 = 271733878,
H4 = -1009589776;
// Padding
m[l >> 5] |= 0x80 << (24 - l % 32);
m[((l + 64 >>> 9) << 4) + 15] = l;
for (var i = 0; i < m.length; i += 16) {
var a = H0,
b = H1,
c = H2,
d = H3,
e = H4;
for (var j = 0; j < 80; j++) {
if (j < 16)
w[j] = m[i + j];
else {
var n = w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16];
w[j] = (n << 1) | (n >>> 31);
}
var t = ((H0 << 5) | (H0 >>> 27)) + H4 + (w[j] >>> 0) + (
j < 20 ? (H1 & H2 | ~H1 & H3) + 1518500249 :
j < 40 ? (H1 ^ H2 ^ H3) + 1859775393 :
j < 60 ? (H1 & H2 | H1 & H3 | H2 & H3) - 1894007588 :
(H1 ^ H2 ^ H3) - 899497514);
H4 = H3;
H3 = H2;
H2 = (H1 << 30) | (H1 >>> 2);
H1 = H0;
H0 = t;
}
H0 += a;
H1 += b;
H2 += c;
H3 += d;
H4 += e;
}
return [H0, H1, H2, H3, H4];
},
// Public API
api = function (message, options) {
var digestbytes = crypt$1.wordsToBytes(sha1(message));
return options && options.asBytes ? digestbytes :
options && options.asString ? bin.bytesToString(digestbytes) :
crypt$1.bytesToHex(digestbytes);
};
api._blocksize = 16;
api._digestsize = 20;
module.exports = api;
})();
});
class Hmac {
constructor(key) {
this.input = [];
const blockSize = 64;
if (key.length > blockSize) {
key = new Uint8Array(sha1(key, {
asBytes: true,
}));
}
this.ipad = new Uint8Array(blockSize);
this.ipad.set(key);
for (let i = 0; i < this.ipad.length; i++) {
this.ipad[i] = this.ipad[i] ^ 0x36;
}
this.opad = new Uint8Array(blockSize);
this.opad.set(key);
for (let i = 0; i < this.opad.length; i++) {
this.opad[i] = this.opad[i] ^ 0x5c;
}
this.write(this.ipad);
}
write(input) {
this.input.push(...input);
}
sum() {
const innerSum = sha1(this.input, {
asBytes: true,
});
let outer = [
...this.opad,
...innerSum
];
return sha1(outer, { asBytes: true });
}
}
const Base32 = {
encode: (value) => {
return base32Encode(value, "RFC4648");
},
decode: (value) => {
return base32Decode(value, "RFC4648");
}
};
function hotp(options) {
let key = new Uint8Array(bin2Uint8Array(options.secret));
const hmac = new Hmac(key);
hmac.write(numberToU64Buffer(options.counter));
const sum = hmac.sum();
// "Dynamic truncation" in RFC 4226
// http://tools.ietf.org/html/rfc4226#section-5.4
let offset = sum[sum.length - 1] & 0xf;
let value = ((sum[offset] & 0x7f) << 24) | ((sum[offset + 1] & 0xff) << 16) | ((sum[offset + 2] & 0xff) << 8) | sum[offset + 3] & 0xff;
const mod = Math.pow(10, options.codeLength);
value = value % mod;
return value.toString().padStart(options.codeLength, "0").substring(0, options.codeLength);
}
function numberToU64Buffer(num) {
// adapted to work without Buffer or BigInt b/c those aren't available in the standard react native setup.
// original code from: https://github.com/feross/buffer/blob/795bbb5bda1b39f1370ebd784bea6107b087e3a7/index.js#L1516
const buf = new Uint8Array(8);
const offset = 0;
let lo = Number(num & 0xffffffff);
buf[offset + 7] = lo;
lo = lo >> 8;
buf[offset + 6] = lo;
lo = lo >> 8;
buf[offset + 5] = lo;
lo = lo >> 8;
buf[offset + 4] = lo;
if (num > 0xffffffff) {
throw new Error("number out of range: " + num.toString());
}
// high bytes are always zero b/c the biggest number we support is
// 32-bit. And we can't easily deal with 64-bit numbers without BigInt (see comment above)
return buf;
}
// Constructor
function OTP(options = []) {
if (typeof options.algorithm == "undefined") {
this.algorithm = "sha1";
} else {
this.algorithm = options.algorithm.toLowerCase();
}
if (typeof options.bias == "undefined") {
this.bias = 0;
} else {
this.bias = parseInt(options.bias, 10);
}
if (typeof options.counter == "undefined") {
this.counter = 0;
} else {
this.counter = parseInt(options.counter, 10);
}
if (typeof options.digits == "undefined") {
this.digits = 6;
} else {
this.digits = parseInt(options.digits, 10);
}
if (typeof options.period == "undefined") {
this.period = 30;
} else {
this.period = parseInt(options.period, 10);
}
if (typeof options.pincode == "undefined") {
this.pincode = "";
} else {
this.pincode = options.pincode;
}
if (typeof options.secret == "undefined") {
this.secret = "3132333435363738393031323334353637383930";
} else {
this.secret = options.secret;
}
if (typeof options.seedtype == "undefined") {
this.seedtype = "hex"; // "hex" or "base32" or "bin"
} else {
this.seedtype = options.seedtype.toLowerCase();
}
if (typeof options.type == "undefined") {
this.type = "totp";
} else {
this.type = options.type.toLowerCase();
}
if (typeof options.values == "undefined") {
this.values = 1;
} else {
this.values = parseInt(options.values, 10);
}
}
OTP.prototype = {
base32toHex: function(value) {
return bin2hex(uint8Array2bin(Base32.decode(value)));
},
hextoBase32: function(value) {
return Base32.encode(bin2Uint8Array(hex2bin(value)));
},
generate: function(options) {
if (typeof options != "undefined") {
if (!isNaN(options)) {
this.values = parseInt(options);
} else {
if (typeof options.algorithm != "undefined") {
this.algorithm = options.algorithm.toLowerCase();
}
if (typeof options.bias != "undefined") {
this.bias = parseInt(options.bias, 10);
}
if (typeof options.counter != "undefined") {
this.counter = parseInt(options.counter, 10);
}
if (typeof options.digits != "undefined") {
this.digits = parseInt(options.digits, 10);
}
if (typeof options.period != "undefined") {
this.period = parseInt(options.period, 10);
}
if (typeof options.pincode != "undefined") {
this.pincode = options.pincode;
}
if (typeof options.secret != "undefined") {
this.secret = options.secret;
}
if (typeof options.seedtype != "undefined") {
this.seedtype = options.seedtype.toLowerCase();
}
if (typeof options.type != "undefined") {
this.type = options.type.toLowerCase();
}
if (typeof options.values != "undefined") {
this.values = parseInt(options.values, 10);
}
}
}
if (typeof this.algorithm == "undefined") {
this.algorithm = "sha1";
}
if (isNaN(this.bias)) {
this.bias = 0;
}
if (isNaN(this.counter)) {
this.counter = 0;
}
if (isNaN(this.digits)) {
this.digits = 6;
}
if (isNaN(this.period)) {
this.period = 30;
}
if (typeof this.pincode == "undefined") {
this.pincode = "";
}
if (typeof this.secret == "undefined") {
this.secret = "3132333435363738393031323334353637383930";
}
if (typeof this.seedtype == "undefined") {
this.seedtype = "hex";
}
if (typeof this.type == "undefined") {
this.type = "totp";
}
if (isNaN(this.values)) {
this.values = 1;
}
if ('motp' == this.type) {
this.period = 10;
}
let _rawsecret = this.secret;
let _hex = this.secret;
if ("base32" == this.seedtype) {
_rawsecret = uint8Array2bin(Base32.decode(this.secret));
_hex = bin2hex(_rawsecret);
} else if ("hex" == this.seedtype) {
_rawsecret = hex2bin(_hex);
} else {
_hex = bin2hex(_rawsecret);
}
const _result = [];
if ("motp" == this.type) {
let epochTimeTen = (Math.floor((Date.now() / 1000 - this.bias) / this.period));
for (let i = 0; i < this.values; i++) {
_result.push(md5(String(epochTimeTen + i) + String(this.secret) + String(this.pincode)).substring(0, this.digits));
}
} else {
if ("totp" == this.type) {
this.counter = (Math.floor((Date.now() / 1000 - this.bias) / this.period));
}
for (let i = 0; i < this.values; i++) {
_result.push(hotp({
hmacAlgorithm: this.algorithm,
counter: this.counter + i,
codeLength: this.digits,
secret: _rawsecret,
}));
}
}
if (this.values < 2) {
return _result[0];
} else {
return _result;
}
},
}
module.exports = OTP;