@ray-js/wechat-rsa 0.0.1-beta-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.esm.js ADDED
@@ -0,0 +1,3704 @@
1
+ const BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
2
+ function int2char(n) {
3
+ return BI_RM.charAt(n);
4
+ } //#region BIT_OPERATIONS
5
+ // (public) this & a
6
+
7
+ function op_and(x, y) {
8
+ return x & y;
9
+ } // (public) this | a
10
+
11
+ function op_or(x, y) {
12
+ return x | y;
13
+ } // (public) this ^ a
14
+
15
+ function op_xor(x, y) {
16
+ return x ^ y;
17
+ } // (public) this & ~a
18
+
19
+ function op_andnot(x, y) {
20
+ return x & ~y;
21
+ } // return index of lowest 1-bit in x, x < 2^31
22
+
23
+ function lbit(x) {
24
+ if (x == 0) {
25
+ return -1;
26
+ }
27
+
28
+ let r = 0;
29
+
30
+ if ((x & 0xffff) == 0) {
31
+ x >>= 16;
32
+ r += 16;
33
+ }
34
+
35
+ if ((x & 0xff) == 0) {
36
+ x >>= 8;
37
+ r += 8;
38
+ }
39
+
40
+ if ((x & 0xf) == 0) {
41
+ x >>= 4;
42
+ r += 4;
43
+ }
44
+
45
+ if ((x & 3) == 0) {
46
+ x >>= 2;
47
+ r += 2;
48
+ }
49
+
50
+ if ((x & 1) == 0) {
51
+ ++r;
52
+ }
53
+
54
+ return r;
55
+ } // return number of 1 bits in x
56
+
57
+ function cbit(x) {
58
+ let r = 0;
59
+
60
+ while (x != 0) {
61
+ x &= x - 1;
62
+ ++r;
63
+ }
64
+
65
+ return r;
66
+ } //#endregion BIT_OPERATIONS
67
+
68
+ const b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
69
+ const b64pad = "=";
70
+ function hex2b64(h) {
71
+ let i;
72
+ let c;
73
+ let ret = "";
74
+
75
+ for (i = 0; i + 3 <= h.length; i += 3) {
76
+ c = parseInt(h.substring(i, i + 3), 16);
77
+ ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
78
+ }
79
+
80
+ if (i + 1 == h.length) {
81
+ c = parseInt(h.substring(i, i + 1), 16);
82
+ ret += b64map.charAt(c << 2);
83
+ } else if (i + 2 == h.length) {
84
+ c = parseInt(h.substring(i, i + 2), 16);
85
+ ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);
86
+ }
87
+
88
+ while ((ret.length & 3) > 0) {
89
+ ret += b64pad;
90
+ }
91
+
92
+ return ret;
93
+ } // convert a base64 string to hex
94
+
95
+ // Hex JavaScript decoder
96
+ // Copyright (c) 2008-2013 Lapo Luchini <lapo@lapo.it>
97
+ // Permission to use, copy, modify, and/or distribute this software for any
98
+ // purpose with or without fee is hereby granted, provided that the above
99
+ // copyright notice and this permission notice appear in all copies.
100
+ //
101
+ // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
102
+ // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
103
+ // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
104
+ // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
105
+ // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
106
+ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
107
+ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
108
+
109
+ /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false */
110
+ let decoder$1;
111
+ const Hex = {
112
+ decode(a) {
113
+ let i;
114
+
115
+ if (decoder$1 === undefined) {
116
+ let hex = "0123456789ABCDEF";
117
+ const ignore = " \f\n\r\t\u00A0\u2028\u2029";
118
+ decoder$1 = {};
119
+
120
+ for (i = 0; i < 16; ++i) {
121
+ decoder$1[hex.charAt(i)] = i;
122
+ }
123
+
124
+ hex = hex.toLowerCase();
125
+
126
+ for (i = 10; i < 16; ++i) {
127
+ decoder$1[hex.charAt(i)] = i;
128
+ }
129
+
130
+ for (i = 0; i < ignore.length; ++i) {
131
+ decoder$1[ignore.charAt(i)] = -1;
132
+ }
133
+ }
134
+
135
+ const out = [];
136
+ let bits = 0;
137
+ let char_count = 0;
138
+
139
+ for (i = 0; i < a.length; ++i) {
140
+ let c = a.charAt(i);
141
+
142
+ if (c == "=") {
143
+ break;
144
+ }
145
+
146
+ c = decoder$1[c];
147
+
148
+ if (c == -1) {
149
+ continue;
150
+ }
151
+
152
+ if (c === undefined) {
153
+ throw new Error("Illegal character at offset " + i);
154
+ }
155
+
156
+ bits |= c;
157
+
158
+ if (++char_count >= 2) {
159
+ out[out.length] = bits;
160
+ bits = 0;
161
+ char_count = 0;
162
+ } else {
163
+ bits <<= 4;
164
+ }
165
+ }
166
+
167
+ if (char_count) {
168
+ throw new Error("Hex encoding incomplete: 4 bits missing");
169
+ }
170
+
171
+ return out;
172
+ }
173
+
174
+ };
175
+
176
+ // Base64 JavaScript decoder
177
+ // Copyright (c) 2008-2013 Lapo Luchini <lapo@lapo.it>
178
+ // Permission to use, copy, modify, and/or distribute this software for any
179
+ // purpose with or without fee is hereby granted, provided that the above
180
+ // copyright notice and this permission notice appear in all copies.
181
+ //
182
+ // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
183
+ // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
184
+ // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
185
+ // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
186
+ // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
187
+ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
188
+ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
189
+
190
+ /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false */
191
+ let decoder;
192
+ const Base64 = {
193
+ decode(a) {
194
+ let i;
195
+
196
+ if (decoder === undefined) {
197
+ const b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
198
+ const ignore = "= \f\n\r\t\u00A0\u2028\u2029";
199
+ decoder = Object.create(null);
200
+
201
+ for (i = 0; i < 64; ++i) {
202
+ decoder[b64.charAt(i)] = i;
203
+ }
204
+
205
+ decoder['-'] = 62; //+
206
+
207
+ decoder['_'] = 63; //-
208
+
209
+ for (i = 0; i < ignore.length; ++i) {
210
+ decoder[ignore.charAt(i)] = -1;
211
+ }
212
+ }
213
+
214
+ const out = [];
215
+ let bits = 0;
216
+ let char_count = 0;
217
+
218
+ for (i = 0; i < a.length; ++i) {
219
+ let c = a.charAt(i);
220
+
221
+ if (c == "=") {
222
+ break;
223
+ }
224
+
225
+ c = decoder[c];
226
+
227
+ if (c == -1) {
228
+ continue;
229
+ }
230
+
231
+ if (c === undefined) {
232
+ throw new Error("Illegal character at offset " + i);
233
+ }
234
+
235
+ bits |= c;
236
+
237
+ if (++char_count >= 4) {
238
+ out[out.length] = bits >> 16;
239
+ out[out.length] = bits >> 8 & 0xFF;
240
+ out[out.length] = bits & 0xFF;
241
+ bits = 0;
242
+ char_count = 0;
243
+ } else {
244
+ bits <<= 6;
245
+ }
246
+ }
247
+
248
+ switch (char_count) {
249
+ case 1:
250
+ throw new Error("Base64 encoding incomplete: at least 2 bits missing");
251
+
252
+ case 2:
253
+ out[out.length] = bits >> 10;
254
+ break;
255
+
256
+ case 3:
257
+ out[out.length] = bits >> 16;
258
+ out[out.length] = bits >> 8 & 0xFF;
259
+ break;
260
+ }
261
+
262
+ return out;
263
+ },
264
+
265
+ // eslint-disable-next-line no-useless-escape
266
+ re: /-----BEGIN [^-]+-----([A-Za-z0-9+\/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/,
267
+
268
+ unarmor(a) {
269
+ const m = Base64.re.exec(a);
270
+
271
+ if (m) {
272
+ if (m[1]) {
273
+ a = m[1];
274
+ } else if (m[2]) {
275
+ a = m[2];
276
+ } else {
277
+ throw new Error("RegExp out of sync");
278
+ }
279
+ }
280
+
281
+ return Base64.decode(a);
282
+ }
283
+
284
+ };
285
+
286
+ // Big integer base-10 printing library
287
+ // Copyright (c) 2014 Lapo Luchini <lapo@lapo.it>
288
+ // Permission to use, copy, modify, and/or distribute this software for any
289
+ // purpose with or without fee is hereby granted, provided that the above
290
+ // copyright notice and this permission notice appear in all copies.
291
+ //
292
+ // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
293
+ // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
294
+ // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
295
+ // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
296
+ // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
297
+ // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
298
+ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
299
+
300
+ /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false */
301
+ const max = 10000000000000; // biggest integer that can still fit 2^53 when multiplied by 256
302
+
303
+ class Int10 {
304
+ constructor(value) {
305
+ this.buf = [value ? +value : 0];
306
+ }
307
+
308
+ mulAdd(m, c) {
309
+ // assert(m <= 256)
310
+ const b = this.buf;
311
+ const l = b.length;
312
+ let i;
313
+ let t;
314
+
315
+ for (i = 0; i < l; ++i) {
316
+ t = b[i] * m + c;
317
+
318
+ if (t < max) {
319
+ c = 0;
320
+ } else {
321
+ c = 0 | t / max;
322
+ t -= c * max;
323
+ }
324
+
325
+ b[i] = t;
326
+ }
327
+
328
+ if (c > 0) {
329
+ b[i] = c;
330
+ }
331
+ }
332
+
333
+ sub(c) {
334
+ // assert(m <= 256)
335
+ const b = this.buf;
336
+ const l = b.length;
337
+ let i;
338
+ let t;
339
+
340
+ for (i = 0; i < l; ++i) {
341
+ t = b[i] - c;
342
+
343
+ if (t < 0) {
344
+ t += max;
345
+ c = 1;
346
+ } else {
347
+ c = 0;
348
+ }
349
+
350
+ b[i] = t;
351
+ }
352
+
353
+ while (b[b.length - 1] === 0) {
354
+ b.pop();
355
+ }
356
+ }
357
+
358
+ toString(base) {
359
+ if ((base || 10) != 10) {
360
+ throw new Error('only base 10 is supported');
361
+ }
362
+
363
+ const b = this.buf;
364
+ let s = b[b.length - 1].toString();
365
+
366
+ for (let i = b.length - 2; i >= 0; --i) {
367
+ s += (max + b[i]).toString().substring(1);
368
+ }
369
+
370
+ return s;
371
+ }
372
+
373
+ valueOf() {
374
+ const b = this.buf;
375
+ let v = 0;
376
+
377
+ for (let i = b.length - 1; i >= 0; --i) {
378
+ v = v * max + b[i];
379
+ }
380
+
381
+ return v;
382
+ }
383
+
384
+ simplify() {
385
+ const b = this.buf;
386
+ return b.length == 1 ? b[0] : this;
387
+ }
388
+
389
+ }
390
+
391
+ // ASN.1 JavaScript decoder
392
+ const ellipsis = '\u2026';
393
+ const reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|[-+](?:[0]\d|1[0-2])([0-5]\d)?)?$/;
394
+ const reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|[-+](?:[0]\d|1[0-2])([0-5]\d)?)?$/;
395
+
396
+ function stringCut(str, len) {
397
+ if (str.length > len) {
398
+ str = str.substring(0, len) + ellipsis;
399
+ }
400
+
401
+ return str;
402
+ }
403
+
404
+ class Stream {
405
+ constructor(enc, pos) {
406
+ this.hexDigits = '0123456789ABCDEF';
407
+
408
+ if (enc instanceof Stream) {
409
+ this.enc = enc.enc;
410
+ this.pos = enc.pos;
411
+ } else {
412
+ // enc should be an array or a binary string
413
+ this.enc = enc;
414
+ this.pos = pos || 0;
415
+ }
416
+ }
417
+
418
+ get(pos) {
419
+ if (pos === undefined) {
420
+ pos = this.pos++;
421
+ }
422
+
423
+ if (pos >= this.enc.length) {
424
+ throw new Error(`Requesting byte offset ${pos} on a stream of length ${this.enc.length}`);
425
+ }
426
+
427
+ return 'string' === typeof this.enc ? this.enc.charCodeAt(pos) : this.enc[pos];
428
+ }
429
+
430
+ hexByte(b) {
431
+ return this.hexDigits.charAt(b >> 4 & 0xf) + this.hexDigits.charAt(b & 0xf);
432
+ }
433
+
434
+ hexDump(start, end, raw) {
435
+ let s = '';
436
+
437
+ for (let i = start; i < end; ++i) {
438
+ s += this.hexByte(this.get(i));
439
+
440
+ if (raw !== true) {
441
+ switch (i & 0xf) {
442
+ case 0x7:
443
+ s += ' ';
444
+ break;
445
+
446
+ case 0xf:
447
+ s += '\n';
448
+ break;
449
+
450
+ default:
451
+ s += ' ';
452
+ }
453
+ }
454
+ }
455
+
456
+ return s;
457
+ }
458
+
459
+ isASCII(start, end) {
460
+ for (let i = start; i < end; ++i) {
461
+ const c = this.get(i);
462
+
463
+ if (c < 32 || c > 176) {
464
+ return false;
465
+ }
466
+ }
467
+
468
+ return true;
469
+ }
470
+
471
+ parseStringISO(start, end) {
472
+ let s = '';
473
+
474
+ for (let i = start; i < end; ++i) {
475
+ s += String.fromCharCode(this.get(i));
476
+ }
477
+
478
+ return s;
479
+ }
480
+
481
+ parseStringUTF(start, end) {
482
+ let s = '';
483
+
484
+ for (let i = start; i < end;) {
485
+ const c = this.get(i++);
486
+
487
+ if (c < 128) {
488
+ s += String.fromCharCode(c);
489
+ } else if (c > 191 && c < 224) {
490
+ s += String.fromCharCode((c & 0x1f) << 6 | this.get(i++) & 0x3f);
491
+ } else {
492
+ s += String.fromCharCode((c & 0x0f) << 12 | (this.get(i++) & 0x3f) << 6 | this.get(i++) & 0x3f);
493
+ }
494
+ }
495
+
496
+ return s;
497
+ }
498
+
499
+ parseStringBMP(start, end) {
500
+ let str = '';
501
+ let hi;
502
+ let lo;
503
+
504
+ for (let i = start; i < end;) {
505
+ hi = this.get(i++);
506
+ lo = this.get(i++);
507
+ str += String.fromCharCode(hi << 8 | lo);
508
+ }
509
+
510
+ return str;
511
+ }
512
+
513
+ parseTime(start, end, shortYear) {
514
+ let s = this.parseStringISO(start, end);
515
+ const m = (shortYear ? reTimeS : reTimeL).exec(s);
516
+
517
+ if (!m) {
518
+ return 'Unrecognized time: ' + s;
519
+ }
520
+
521
+ if (shortYear) {
522
+ // to avoid querying the timer, use the fixed range [1970, 2069]
523
+ // it will conform with ITU X.400 [-10, +40] sliding window until 2030
524
+ m[1] = +m[1];
525
+ m[1] += +m[1] < 70 ? 2000 : 1900;
526
+ }
527
+
528
+ s = m[1] + '-' + m[2] + '-' + m[3] + ' ' + m[4];
529
+
530
+ if (m[5]) {
531
+ s += ':' + m[5];
532
+
533
+ if (m[6]) {
534
+ s += ':' + m[6];
535
+
536
+ if (m[7]) {
537
+ s += '.' + m[7];
538
+ }
539
+ }
540
+ }
541
+
542
+ if (m[8]) {
543
+ s += ' UTC';
544
+
545
+ if (m[8] != 'Z') {
546
+ s += m[8];
547
+
548
+ if (m[9]) {
549
+ s += ':' + m[9];
550
+ }
551
+ }
552
+ }
553
+
554
+ return s;
555
+ }
556
+
557
+ parseInteger(start, end) {
558
+ let v = this.get(start);
559
+ const neg = v > 127;
560
+ const pad = neg ? 255 : 0;
561
+ let len;
562
+ let s = ''; // skip unuseful bits (not allowed in DER)
563
+
564
+ while (v == pad && ++start < end) {
565
+ v = this.get(start);
566
+ }
567
+
568
+ len = end - start;
569
+
570
+ if (len === 0) {
571
+ return neg ? -1 : 0;
572
+ } // show bit length of huge integers
573
+
574
+
575
+ if (len > 4) {
576
+ s = v;
577
+ len <<= 3;
578
+
579
+ while (((+s ^ pad) & 0x80) == 0) {
580
+ s = +s << 1;
581
+ --len;
582
+ }
583
+
584
+ s = '(' + len + ' bit)\n';
585
+ } // decode the integer
586
+
587
+
588
+ if (neg) {
589
+ v = v - 256;
590
+ }
591
+
592
+ const n = new Int10(v);
593
+
594
+ for (let i = start + 1; i < end; ++i) {
595
+ n.mulAdd(256, this.get(i));
596
+ }
597
+
598
+ return s + n.toString();
599
+ }
600
+
601
+ parseBitString(start, end, maxLength) {
602
+ const unusedBit = this.get(start);
603
+ const lenBit = (end - start - 1 << 3) - unusedBit;
604
+ const intro = '(' + lenBit + ' bit)\n';
605
+ let s = '';
606
+
607
+ for (let i = start + 1; i < end; ++i) {
608
+ const b = this.get(i);
609
+ const skip = i == end - 1 ? unusedBit : 0;
610
+
611
+ for (let j = 7; j >= skip; --j) {
612
+ s += b >> j & 1 ? '1' : '0';
613
+ }
614
+
615
+ if (s.length > maxLength) {
616
+ return intro + stringCut(s, maxLength);
617
+ }
618
+ }
619
+
620
+ return intro + s;
621
+ }
622
+
623
+ parseOctetString(start, end, maxLength) {
624
+ if (this.isASCII(start, end)) {
625
+ return stringCut(this.parseStringISO(start, end), maxLength);
626
+ }
627
+
628
+ const len = end - start;
629
+ let s = '(' + len + ' byte)\n';
630
+ maxLength /= 2; // we work in bytes
631
+
632
+ if (len > maxLength) {
633
+ end = start + maxLength;
634
+ }
635
+
636
+ for (let i = start; i < end; ++i) {
637
+ s += this.hexByte(this.get(i));
638
+ }
639
+
640
+ if (len > maxLength) {
641
+ s += ellipsis;
642
+ }
643
+
644
+ return s;
645
+ }
646
+
647
+ parseOID(start, end, maxLength) {
648
+ let s = '';
649
+ let n = new Int10();
650
+ let bits = 0;
651
+
652
+ for (let i = start; i < end; ++i) {
653
+ const v = this.get(i);
654
+ n.mulAdd(128, v & 0x7f);
655
+ bits += 7;
656
+
657
+ if (!(v & 0x80)) {
658
+ // finished
659
+ if (s === '') {
660
+ n = n.simplify();
661
+
662
+ if (n instanceof Int10) {
663
+ n.sub(80);
664
+ s = '2.' + n.toString();
665
+ } else {
666
+ const m = n < 80 ? n < 40 ? 0 : 1 : 2;
667
+ s = m + '.' + (n - m * 40);
668
+ }
669
+ } else {
670
+ s += '.' + n.toString();
671
+ }
672
+
673
+ if (s.length > maxLength) {
674
+ return stringCut(s, maxLength);
675
+ }
676
+
677
+ n = new Int10();
678
+ bits = 0;
679
+ }
680
+ }
681
+
682
+ if (bits > 0) {
683
+ s += '.incomplete';
684
+ }
685
+
686
+ return s;
687
+ }
688
+
689
+ }
690
+ class ASN1 {
691
+ constructor(stream, header, length, tag, sub) {
692
+ if (!(tag instanceof ASN1Tag)) {
693
+ throw new Error('Invalid tag value.');
694
+ }
695
+
696
+ this.stream = stream;
697
+ this.header = header;
698
+ this.length = length;
699
+ this.tag = tag;
700
+ this.sub = sub;
701
+ }
702
+
703
+ typeName() {
704
+ switch (this.tag.tagClass) {
705
+ case 0:
706
+ // universal
707
+ switch (this.tag.tagNumber) {
708
+ case 0x00:
709
+ return 'EOC';
710
+
711
+ case 0x01:
712
+ return 'BOOLEAN';
713
+
714
+ case 0x02:
715
+ return 'INTEGER';
716
+
717
+ case 0x03:
718
+ return 'BIT_STRING';
719
+
720
+ case 0x04:
721
+ return 'OCTET_STRING';
722
+
723
+ case 0x05:
724
+ return 'NULL';
725
+
726
+ case 0x06:
727
+ return 'OBJECT_IDENTIFIER';
728
+
729
+ case 0x07:
730
+ return 'ObjectDescriptor';
731
+
732
+ case 0x08:
733
+ return 'EXTERNAL';
734
+
735
+ case 0x09:
736
+ return 'REAL';
737
+
738
+ case 0x0a:
739
+ return 'ENUMERATED';
740
+
741
+ case 0x0b:
742
+ return 'EMBEDDED_PDV';
743
+
744
+ case 0x0c:
745
+ return 'UTF8String';
746
+
747
+ case 0x10:
748
+ return 'SEQUENCE';
749
+
750
+ case 0x11:
751
+ return 'SET';
752
+
753
+ case 0x12:
754
+ return 'NumericString';
755
+
756
+ case 0x13:
757
+ return 'PrintableString';
758
+ // ASCII subset
759
+
760
+ case 0x14:
761
+ return 'TeletexString';
762
+ // aka T61String
763
+
764
+ case 0x15:
765
+ return 'VideotexString';
766
+
767
+ case 0x16:
768
+ return 'IA5String';
769
+ // ASCII
770
+
771
+ case 0x17:
772
+ return 'UTCTime';
773
+
774
+ case 0x18:
775
+ return 'GeneralizedTime';
776
+
777
+ case 0x19:
778
+ return 'GraphicString';
779
+
780
+ case 0x1a:
781
+ return 'VisibleString';
782
+ // ASCII subset
783
+
784
+ case 0x1b:
785
+ return 'GeneralString';
786
+
787
+ case 0x1c:
788
+ return 'UniversalString';
789
+
790
+ case 0x1e:
791
+ return 'BMPString';
792
+ }
793
+
794
+ return 'Universal_' + this.tag.tagNumber.toString();
795
+
796
+ case 1:
797
+ return 'Application_' + this.tag.tagNumber.toString();
798
+
799
+ case 2:
800
+ return '[' + this.tag.tagNumber.toString() + ']';
801
+ // Context
802
+
803
+ case 3:
804
+ return 'Private_' + this.tag.tagNumber.toString();
805
+
806
+ default:
807
+ return;
808
+ }
809
+ }
810
+
811
+ content(maxLength) {
812
+ // a preview of the content (intended for humans)
813
+ if (this.tag === undefined) {
814
+ return null;
815
+ }
816
+
817
+ if (maxLength === undefined) {
818
+ maxLength = Infinity;
819
+ }
820
+
821
+ const content = this.posContent();
822
+ const len = Math.abs(this.length);
823
+
824
+ if (!this.tag.isUniversal()) {
825
+ if (this.sub !== null) {
826
+ return '(' + this.sub.length + ' elem)';
827
+ }
828
+
829
+ return this.stream.parseOctetString(content, content + len, maxLength);
830
+ }
831
+
832
+ switch (this.tag.tagNumber) {
833
+ case 0x01:
834
+ // BOOLEAN
835
+ return this.stream.get(content) === 0 ? 'false' : 'true';
836
+
837
+ case 0x02:
838
+ // INTEGER
839
+ return this.stream.parseInteger(content, content + len);
840
+
841
+ case 0x03:
842
+ // BIT_STRING
843
+ return this.sub ? '(' + this.sub.length + ' elem)' : this.stream.parseBitString(content, content + len, maxLength);
844
+
845
+ case 0x04:
846
+ // OCTET_STRING
847
+ return this.sub ? '(' + this.sub.length + ' elem)' : this.stream.parseOctetString(content, content + len, maxLength);
848
+ // case 0x05: // NULL
849
+
850
+ case 0x06:
851
+ // OBJECT_IDENTIFIER
852
+ return this.stream.parseOID(content, content + len, maxLength);
853
+ // case 0x07: // ObjectDescriptor
854
+ // case 0x08: // EXTERNAL
855
+ // case 0x09: // REAL
856
+ // case 0x0A: // ENUMERATED
857
+ // case 0x0B: // EMBEDDED_PDV
858
+
859
+ case 0x10: // SEQUENCE
860
+
861
+ case 0x11:
862
+ // SET
863
+ if (this.sub !== null) {
864
+ return '(' + this.sub.length + ' elem)';
865
+ } else {
866
+ return '(no elem)';
867
+ }
868
+
869
+ case 0x0c:
870
+ // UTF8String
871
+ return stringCut(this.stream.parseStringUTF(content, content + len), maxLength);
872
+
873
+ case 0x12: // NumericString
874
+
875
+ case 0x13: // PrintableString
876
+
877
+ case 0x14: // TeletexString
878
+
879
+ case 0x15: // VideotexString
880
+
881
+ case 0x16: // IA5String
882
+ // case 0x19: // GraphicString
883
+ // eslint-disable-next-line no-fallthrough
884
+
885
+ case 0x1a:
886
+ // VisibleString
887
+ // case 0x1B: // GeneralString
888
+ // case 0x1C: // UniversalString
889
+ return stringCut(this.stream.parseStringISO(content, content + len), maxLength);
890
+
891
+ case 0x1e:
892
+ // BMPString
893
+ return stringCut(this.stream.parseStringBMP(content, content + len), maxLength);
894
+
895
+ case 0x17: // UTCTime
896
+
897
+ case 0x18:
898
+ // GeneralizedTime
899
+ return this.stream.parseTime(content, content + len, this.tag.tagNumber == 0x17);
900
+ }
901
+
902
+ return null;
903
+ }
904
+
905
+ toString() {
906
+ return this.typeName() + '@' + this.stream.pos + '[header:' + this.header + ',length:' + this.length + ',sub:' + (this.sub === null ? 'null' : this.sub.length) + ']';
907
+ }
908
+
909
+ toPrettyString(indent) {
910
+ if (indent === undefined) {
911
+ indent = '';
912
+ }
913
+
914
+ let s = indent + this.typeName() + ' @' + this.stream.pos;
915
+
916
+ if (this.length >= 0) {
917
+ s += '+';
918
+ }
919
+
920
+ s += this.length;
921
+
922
+ if (this.tag.tagConstructed) {
923
+ s += ' (constructed)';
924
+ } else if (this.tag.isUniversal() && (this.tag.tagNumber == 0x03 || this.tag.tagNumber == 0x04) && this.sub !== null) {
925
+ s += ' (encapsulates)';
926
+ }
927
+
928
+ s += '\n';
929
+
930
+ if (this.sub !== null) {
931
+ indent += ' ';
932
+
933
+ for (let i = 0, max = this.sub.length; i < max; ++i) {
934
+ s += this.sub[i].toPrettyString(indent);
935
+ }
936
+ }
937
+
938
+ return s;
939
+ }
940
+
941
+ posStart() {
942
+ return this.stream.pos;
943
+ }
944
+
945
+ posContent() {
946
+ return this.stream.pos + this.header;
947
+ }
948
+
949
+ posEnd() {
950
+ return this.stream.pos + this.header + Math.abs(this.length);
951
+ }
952
+
953
+ toHexString() {
954
+ return this.stream.hexDump(this.posStart(), this.posEnd(), true);
955
+ }
956
+
957
+ static decodeLength(stream) {
958
+ let buf = stream.get();
959
+ const len = buf & 0x7f;
960
+
961
+ if (len == buf) {
962
+ return len;
963
+ } // no reason to use Int10, as it would be a huge buffer anyways
964
+
965
+
966
+ if (len > 6) {
967
+ throw new Error('Length over 48 bits not supported at position ' + (stream.pos - 1));
968
+ }
969
+
970
+ if (len === 0) {
971
+ return null;
972
+ } // undefined
973
+
974
+
975
+ buf = 0;
976
+
977
+ for (let i = 0; i < len; ++i) {
978
+ buf = buf * 256 + stream.get();
979
+ }
980
+
981
+ return buf;
982
+ }
983
+ /**
984
+ * Retrieve the hexadecimal value (as a string) of the current ASN.1 element
985
+ * @returns {string}
986
+ * @public
987
+ */
988
+
989
+
990
+ getHexStringValue() {
991
+ const hexString = this.toHexString();
992
+ const offset = this.header * 2;
993
+ const length = this.length * 2;
994
+ return hexString.substr(offset, length);
995
+ }
996
+
997
+ static decode(str) {
998
+ let stream;
999
+
1000
+ if (!(str instanceof Stream)) {
1001
+ stream = new Stream(str, 0);
1002
+ } else {
1003
+ stream = str;
1004
+ }
1005
+
1006
+ const streamStart = new Stream(stream);
1007
+ const tag = new ASN1Tag(stream);
1008
+ let len = ASN1.decodeLength(stream);
1009
+ const start = stream.pos;
1010
+ const header = start - streamStart.pos;
1011
+ let sub = null;
1012
+
1013
+ const getSub = function getSub() {
1014
+ const ret = [];
1015
+
1016
+ if (len !== null) {
1017
+ // definite length
1018
+ const end = start + len;
1019
+
1020
+ while (stream.pos < end) {
1021
+ ret[ret.length] = ASN1.decode(stream);
1022
+ }
1023
+
1024
+ if (stream.pos != end) {
1025
+ throw new Error('Content size is not correct for container starting at offset ' + start);
1026
+ }
1027
+ } else {
1028
+ // undefined length
1029
+ try {
1030
+ for (;;) {
1031
+ const s = ASN1.decode(stream);
1032
+
1033
+ if (s.tag.isEOC()) {
1034
+ break;
1035
+ }
1036
+
1037
+ ret[ret.length] = s;
1038
+ }
1039
+
1040
+ len = start - stream.pos; // undefined lengths are represented as negative values
1041
+ } catch (e) {
1042
+ throw new Error('Exception while decoding undefined length content: ' + e);
1043
+ }
1044
+ }
1045
+
1046
+ return ret;
1047
+ };
1048
+
1049
+ if (tag.tagConstructed) {
1050
+ // must have valid content
1051
+ sub = getSub();
1052
+ } else if (tag.isUniversal() && (tag.tagNumber == 0x03 || tag.tagNumber == 0x04)) {
1053
+ // sometimes BitString and OctetString are used to encapsulate ASN.1
1054
+ try {
1055
+ if (tag.tagNumber == 0x03) {
1056
+ if (stream.get() != 0) {
1057
+ throw new Error('BIT STRINGs with unused bits cannot encapsulate.');
1058
+ }
1059
+ }
1060
+
1061
+ sub = getSub();
1062
+
1063
+ for (let i = 0; i < sub.length; ++i) {
1064
+ if (sub[i].tag.isEOC()) {
1065
+ throw new Error('EOC is not supposed to be actual content.');
1066
+ }
1067
+ }
1068
+ } catch (e) {
1069
+ // but silently ignore when they don't
1070
+ sub = null;
1071
+ }
1072
+ }
1073
+
1074
+ if (len === null) {
1075
+ throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
1076
+ }
1077
+
1078
+ if (sub === null) {
1079
+ if (len === null) {
1080
+ throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
1081
+ }
1082
+
1083
+ stream.pos = start + Math.abs(len);
1084
+ }
1085
+
1086
+ return new ASN1(streamStart, header, len, tag, sub);
1087
+ }
1088
+
1089
+ }
1090
+ class ASN1Tag {
1091
+ constructor(stream) {
1092
+ let buf = stream.get();
1093
+ this.tagClass = buf >> 6;
1094
+ this.tagConstructed = (buf & 0x20) !== 0;
1095
+ this.tagNumber = buf & 0x1f;
1096
+
1097
+ if (this.tagNumber == 0x1f) {
1098
+ // long tag
1099
+ const n = new Int10();
1100
+
1101
+ do {
1102
+ buf = stream.get();
1103
+ n.mulAdd(128, buf & 0x7f);
1104
+ } while (buf & 0x80);
1105
+
1106
+ this.tagNumber = n.simplify();
1107
+ }
1108
+ }
1109
+
1110
+ isUniversal() {
1111
+ return this.tagClass === 0x00;
1112
+ }
1113
+
1114
+ isEOC() {
1115
+ return this.tagClass === 0x00 && this.tagNumber === 0x00;
1116
+ }
1117
+
1118
+ }
1119
+
1120
+ // Copyright (c) 2005 Tom Wu
1121
+
1122
+ let dbits; // JavaScript engine analysis
1123
+
1124
+ const canary = 0xdeadbeefcafe;
1125
+ const j_lm = (canary & 0xffffff) == 0xefcafe; // am: Compute w_j += (x*this_i), propagate carries,
1126
+ // c is initial carry, returns final carry.
1127
+ // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
1128
+ // We need to select the fastest one that works in this environment.
1129
+
1130
+ const inBrowser = typeof navigator !== 'undefined';
1131
+ const isIE = inBrowser && j_lm && navigator.appName === 'Microsoft Internet Explorer';
1132
+ const isNetscapeOrWx = (inBrowser && navigator.appName !== 'Netscape' || typeof wx !== 'undefined');
1133
+
1134
+ if (isIE) {
1135
+ dbits = 30;
1136
+ } else if (isNetscapeOrWx) {
1137
+ dbits = 26;
1138
+ } else {
1139
+ dbits = 28;
1140
+ }
1141
+
1142
+ const BI_FP = 52; //#region
1143
+
1144
+ const lowprimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];
1145
+ const lplim = (1 << 26) / lowprimes[lowprimes.length - 1]; //#endregion
1146
+ // (public) Constructor
1147
+
1148
+ class BigInteger {
1149
+ constructor(a, b, c) {
1150
+ this.DB = dbits;
1151
+ this.DM = (1 << dbits) - 1;
1152
+ this.DV = 1 << dbits;
1153
+ this.FV = Math.pow(2, BI_FP);
1154
+ this.F1 = BI_FP - dbits;
1155
+ this.F2 = 2 * dbits - BI_FP;
1156
+
1157
+ if (a != null) {
1158
+ if ('number' == typeof a) {
1159
+ if (typeof b !== 'undefined') {
1160
+ this.fromNumber(a, b, c);
1161
+ }
1162
+ } else if (b == null && 'string' != typeof a) {
1163
+ this.fromString(a, 256);
1164
+ } else {
1165
+ this.fromString(a, b);
1166
+ }
1167
+ }
1168
+ } //#region PUBLIC
1169
+ // (public) return string representation in given radix
1170
+
1171
+
1172
+ toString(b) {
1173
+ if (this.s < 0) {
1174
+ return '-' + this.negate().toString(b);
1175
+ }
1176
+
1177
+ let k;
1178
+
1179
+ if (b == 16) {
1180
+ k = 4;
1181
+ } else if (b == 8) {
1182
+ k = 3;
1183
+ } else if (b == 2) {
1184
+ k = 1;
1185
+ } else if (b == 32) {
1186
+ k = 5;
1187
+ } else if (b == 4) {
1188
+ k = 2;
1189
+ } else {
1190
+ return this.toRadix(b);
1191
+ }
1192
+
1193
+ const km = (1 << k) - 1;
1194
+ let d;
1195
+ let m = false;
1196
+ let r = '';
1197
+ let i = this.t;
1198
+ let p = this.DB - i * this.DB % k;
1199
+
1200
+ if (i-- > 0) {
1201
+ if (p < this.DB && (d = this[i] >> p) > 0) {
1202
+ m = true;
1203
+ r = int2char(d);
1204
+ }
1205
+
1206
+ while (i >= 0) {
1207
+ if (p < k) {
1208
+ d = (this[i] & (1 << p) - 1) << k - p;
1209
+ d |= this[--i] >> (p += this.DB - k);
1210
+ } else {
1211
+ d = this[i] >> (p -= k) & km;
1212
+
1213
+ if (p <= 0) {
1214
+ p += this.DB;
1215
+ --i;
1216
+ }
1217
+ }
1218
+
1219
+ if (d > 0) {
1220
+ m = true;
1221
+ }
1222
+
1223
+ if (m) {
1224
+ r += int2char(d);
1225
+ }
1226
+ }
1227
+ }
1228
+
1229
+ return m ? r : '0';
1230
+ } // (public) -this
1231
+
1232
+
1233
+ negate() {
1234
+ const r = nbi();
1235
+ BigInteger.ZERO.subTo(this, r);
1236
+ return r;
1237
+ } // (public) |this|
1238
+
1239
+
1240
+ abs() {
1241
+ return this.s < 0 ? this.negate() : this;
1242
+ } // (public) return + if this > a, - if this < a, 0 if equal
1243
+
1244
+
1245
+ compareTo(a) {
1246
+ let r = this.s - a.s;
1247
+
1248
+ if (r != 0) {
1249
+ return r;
1250
+ }
1251
+
1252
+ let i = this.t;
1253
+ r = i - a.t;
1254
+
1255
+ if (r != 0) {
1256
+ return this.s < 0 ? -r : r;
1257
+ }
1258
+
1259
+ while (--i >= 0) {
1260
+ if ((r = this[i] - a[i]) != 0) {
1261
+ return r;
1262
+ }
1263
+ }
1264
+
1265
+ return 0;
1266
+ } // (public) return the number of bits in "this"
1267
+
1268
+
1269
+ bitLength() {
1270
+ if (this.t <= 0) {
1271
+ return 0;
1272
+ }
1273
+
1274
+ return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM);
1275
+ } // (public) this mod a
1276
+
1277
+
1278
+ mod(a) {
1279
+ const r = nbi();
1280
+ this.abs().divRemTo(a, null, r);
1281
+
1282
+ if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) {
1283
+ a.subTo(r, r);
1284
+ }
1285
+
1286
+ return r;
1287
+ } // (public) this^e % m, 0 <= e < 2^32
1288
+
1289
+
1290
+ modPowInt(e, m) {
1291
+ let z;
1292
+
1293
+ if (e < 256 || m.isEven()) {
1294
+ z = new Classic(m);
1295
+ } else {
1296
+ z = new Montgomery(m);
1297
+ }
1298
+
1299
+ return this.exp(e, z);
1300
+ } // (public)
1301
+
1302
+
1303
+ clone() {
1304
+ const r = nbi();
1305
+ this.copyTo(r);
1306
+ return r;
1307
+ } // (public) return value as integer
1308
+
1309
+
1310
+ intValue() {
1311
+ if (this.s < 0) {
1312
+ if (this.t == 1) {
1313
+ return this[0] - this.DV;
1314
+ } else if (this.t == 0) {
1315
+ return -1;
1316
+ }
1317
+ } else if (this.t == 1) {
1318
+ return this[0];
1319
+ } else if (this.t == 0) {
1320
+ return 0;
1321
+ } // assumes 16 < DB < 32
1322
+
1323
+
1324
+ return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0];
1325
+ } // (public) return value as byte
1326
+
1327
+
1328
+ byteValue() {
1329
+ return this.t == 0 ? this.s : this[0] << 24 >> 24;
1330
+ } // (public) return value as short (assumes DB>=16)
1331
+
1332
+
1333
+ shortValue() {
1334
+ return this.t == 0 ? this.s : this[0] << 16 >> 16;
1335
+ } // (public) 0 if this == 0, 1 if this > 0
1336
+
1337
+
1338
+ signum() {
1339
+ if (this.s < 0) {
1340
+ return -1;
1341
+ } else if (this.t <= 0 || this.t == 1 && this[0] <= 0) {
1342
+ return 0;
1343
+ } else {
1344
+ return 1;
1345
+ }
1346
+ } // (public) convert to bigendian byte array
1347
+
1348
+
1349
+ toByteArray() {
1350
+ let i = this.t;
1351
+ const r = [];
1352
+ r[0] = this.s;
1353
+ let p = this.DB - i * this.DB % 8;
1354
+ let d;
1355
+ let k = 0;
1356
+
1357
+ if (i-- > 0) {
1358
+ if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) {
1359
+ r[k++] = d | this.s << this.DB - p;
1360
+ }
1361
+
1362
+ while (i >= 0) {
1363
+ if (p < 8) {
1364
+ d = (this[i] & (1 << p) - 1) << 8 - p;
1365
+ d |= this[--i] >> (p += this.DB - 8);
1366
+ } else {
1367
+ d = this[i] >> (p -= 8) & 0xff;
1368
+
1369
+ if (p <= 0) {
1370
+ p += this.DB;
1371
+ --i;
1372
+ }
1373
+ }
1374
+
1375
+ if ((d & 0x80) != 0) {
1376
+ d |= -256;
1377
+ }
1378
+
1379
+ if (k == 0 && (this.s & 0x80) != (d & 0x80)) {
1380
+ ++k;
1381
+ }
1382
+
1383
+ if (k > 0 || d != this.s) {
1384
+ r[k++] = d;
1385
+ }
1386
+ }
1387
+ }
1388
+
1389
+ return r;
1390
+ }
1391
+
1392
+ equals(a) {
1393
+ return this.compareTo(a) == 0;
1394
+ }
1395
+
1396
+ min(a) {
1397
+ return this.compareTo(a) < 0 ? this : a;
1398
+ }
1399
+
1400
+ max(a) {
1401
+ return this.compareTo(a) > 0 ? this : a;
1402
+ }
1403
+
1404
+ and(a) {
1405
+ const r = nbi();
1406
+ this.bitwiseTo(a, op_and, r);
1407
+ return r;
1408
+ }
1409
+
1410
+ or(a) {
1411
+ const r = nbi();
1412
+ this.bitwiseTo(a, op_or, r);
1413
+ return r;
1414
+ }
1415
+
1416
+ xor(a) {
1417
+ const r = nbi();
1418
+ this.bitwiseTo(a, op_xor, r);
1419
+ return r;
1420
+ }
1421
+
1422
+ andNot(a) {
1423
+ const r = nbi();
1424
+ this.bitwiseTo(a, op_andnot, r);
1425
+ return r;
1426
+ } // (public) ~this
1427
+
1428
+
1429
+ not() {
1430
+ const r = nbi();
1431
+
1432
+ for (let i = 0; i < this.t; ++i) {
1433
+ r[i] = this.DM & ~this[i];
1434
+ }
1435
+
1436
+ r.t = this.t;
1437
+ r.s = ~this.s;
1438
+ return r;
1439
+ } // (public) this << n
1440
+
1441
+
1442
+ shiftLeft(n) {
1443
+ const r = nbi();
1444
+
1445
+ if (n < 0) {
1446
+ this.rShiftTo(-n, r);
1447
+ } else {
1448
+ this.lShiftTo(n, r);
1449
+ }
1450
+
1451
+ return r;
1452
+ } // (public) this >> n
1453
+
1454
+
1455
+ shiftRight(n) {
1456
+ const r = nbi();
1457
+
1458
+ if (n < 0) {
1459
+ this.lShiftTo(-n, r);
1460
+ } else {
1461
+ this.rShiftTo(n, r);
1462
+ }
1463
+
1464
+ return r;
1465
+ } // (public) returns index of lowest 1-bit (or -1 if none)
1466
+
1467
+
1468
+ getLowestSetBit() {
1469
+ for (let i = 0; i < this.t; ++i) {
1470
+ if (this[i] != 0) {
1471
+ return i * this.DB + lbit(this[i]);
1472
+ }
1473
+ }
1474
+
1475
+ if (this.s < 0) {
1476
+ return this.t * this.DB;
1477
+ }
1478
+
1479
+ return -1;
1480
+ } // (public) return number of set bits
1481
+
1482
+
1483
+ bitCount() {
1484
+ let r = 0;
1485
+ const x = this.s & this.DM;
1486
+
1487
+ for (let i = 0; i < this.t; ++i) {
1488
+ r += cbit(this[i] ^ x);
1489
+ }
1490
+
1491
+ return r;
1492
+ } // (public) true iff nth bit is set
1493
+
1494
+
1495
+ testBit(n) {
1496
+ const j = Math.floor(n / this.DB);
1497
+
1498
+ if (j >= this.t) {
1499
+ return this.s != 0;
1500
+ }
1501
+
1502
+ return (this[j] & 1 << n % this.DB) != 0;
1503
+ } // (public) this | (1<<n)
1504
+
1505
+
1506
+ setBit(n) {
1507
+ return this.changeBit(n, op_or);
1508
+ } // (public) this & ~(1<<n)
1509
+
1510
+
1511
+ clearBit(n) {
1512
+ return this.changeBit(n, op_andnot);
1513
+ } // (public) this ^ (1<<n)
1514
+
1515
+
1516
+ flipBit(n) {
1517
+ return this.changeBit(n, op_xor);
1518
+ } // (public) this + a
1519
+
1520
+
1521
+ add(a) {
1522
+ const r = nbi();
1523
+ this.addTo(a, r);
1524
+ return r;
1525
+ } // (public) this - a
1526
+
1527
+
1528
+ subtract(a) {
1529
+ const r = nbi();
1530
+ this.subTo(a, r);
1531
+ return r;
1532
+ } // (public) this * a
1533
+
1534
+
1535
+ multiply(a) {
1536
+ const r = nbi();
1537
+ this.multiplyTo(a, r);
1538
+ return r;
1539
+ } // (public) this / a
1540
+
1541
+
1542
+ divide(a) {
1543
+ const r = nbi();
1544
+ this.divRemTo(a, r, null);
1545
+ return r;
1546
+ } // (public) this % a
1547
+
1548
+
1549
+ remainder(a) {
1550
+ const r = nbi();
1551
+ this.divRemTo(a, null, r);
1552
+ return r;
1553
+ } // (public) [this/a,this%a]
1554
+
1555
+
1556
+ divideAndRemainder(a) {
1557
+ const q = nbi();
1558
+ const r = nbi();
1559
+ this.divRemTo(a, q, r);
1560
+ return [q, r];
1561
+ } // (public) this^e % m (HAC 14.85)
1562
+
1563
+
1564
+ modPow(e, m) {
1565
+ let i = e.bitLength();
1566
+ let k;
1567
+ let r = nbv(1);
1568
+ let z;
1569
+
1570
+ if (i <= 0) {
1571
+ return r;
1572
+ } else if (i < 18) {
1573
+ k = 1;
1574
+ } else if (i < 48) {
1575
+ k = 3;
1576
+ } else if (i < 144) {
1577
+ k = 4;
1578
+ } else if (i < 768) {
1579
+ k = 5;
1580
+ } else {
1581
+ k = 6;
1582
+ }
1583
+
1584
+ if (i < 8) {
1585
+ z = new Classic(m);
1586
+ } else if (m.isEven()) {
1587
+ z = new Barrett(m);
1588
+ } else {
1589
+ z = new Montgomery(m);
1590
+ } // precomputation
1591
+
1592
+
1593
+ const g = [];
1594
+ let n = 3;
1595
+ const k1 = k - 1;
1596
+ const km = (1 << k) - 1;
1597
+ g[1] = z.convert(this);
1598
+
1599
+ if (k > 1) {
1600
+ const g2 = nbi();
1601
+ z.sqrTo(g[1], g2);
1602
+
1603
+ while (n <= km) {
1604
+ g[n] = nbi();
1605
+ z.mulTo(g2, g[n - 2], g[n]);
1606
+ n += 2;
1607
+ }
1608
+ }
1609
+
1610
+ let j = e.t - 1;
1611
+ let w;
1612
+ let is1 = true;
1613
+ let r2 = nbi();
1614
+ let t;
1615
+ i = nbits(e[j]) - 1;
1616
+
1617
+ while (j >= 0) {
1618
+ if (i >= k1) {
1619
+ w = e[j] >> i - k1 & km;
1620
+ } else {
1621
+ w = (e[j] & (1 << i + 1) - 1) << k1 - i;
1622
+
1623
+ if (j > 0) {
1624
+ w |= e[j - 1] >> this.DB + i - k1;
1625
+ }
1626
+ }
1627
+
1628
+ n = k;
1629
+
1630
+ while ((w & 1) == 0) {
1631
+ w >>= 1;
1632
+ --n;
1633
+ }
1634
+
1635
+ if ((i -= n) < 0) {
1636
+ i += this.DB;
1637
+ --j;
1638
+ }
1639
+
1640
+ if (is1) {
1641
+ // ret == 1, don't bother squaring or multiplying it
1642
+ g[w].copyTo(r);
1643
+ is1 = false;
1644
+ } else {
1645
+ while (n > 1) {
1646
+ z.sqrTo(r, r2);
1647
+ z.sqrTo(r2, r);
1648
+ n -= 2;
1649
+ }
1650
+
1651
+ if (n > 0) {
1652
+ z.sqrTo(r, r2);
1653
+ } else {
1654
+ t = r;
1655
+ r = r2;
1656
+ r2 = t;
1657
+ }
1658
+
1659
+ z.mulTo(r2, g[w], r);
1660
+ }
1661
+
1662
+ while (j >= 0 && (e[j] & 1 << i) == 0) {
1663
+ z.sqrTo(r, r2);
1664
+ t = r;
1665
+ r = r2;
1666
+ r2 = t;
1667
+
1668
+ if (--i < 0) {
1669
+ i = this.DB - 1;
1670
+ --j;
1671
+ }
1672
+ }
1673
+ }
1674
+
1675
+ return z.revert(r);
1676
+ } // (public) 1/this % m (HAC 14.61)
1677
+
1678
+
1679
+ modInverse(m) {
1680
+ const ac = m.isEven();
1681
+
1682
+ if (this.isEven() && ac || m.signum() == 0) {
1683
+ return BigInteger.ZERO;
1684
+ }
1685
+
1686
+ const u = m.clone();
1687
+ const v = this.clone();
1688
+ const a = nbv(1);
1689
+ const b = nbv(0);
1690
+ const c = nbv(0);
1691
+ const d = nbv(1);
1692
+
1693
+ while (u.signum() != 0) {
1694
+ while (u.isEven()) {
1695
+ u.rShiftTo(1, u);
1696
+
1697
+ if (ac) {
1698
+ if (!a.isEven() || !b.isEven()) {
1699
+ a.addTo(this, a);
1700
+ b.subTo(m, b);
1701
+ }
1702
+
1703
+ a.rShiftTo(1, a);
1704
+ } else if (!b.isEven()) {
1705
+ b.subTo(m, b);
1706
+ }
1707
+
1708
+ b.rShiftTo(1, b);
1709
+ }
1710
+
1711
+ while (v.isEven()) {
1712
+ v.rShiftTo(1, v);
1713
+
1714
+ if (ac) {
1715
+ if (!c.isEven() || !d.isEven()) {
1716
+ c.addTo(this, c);
1717
+ d.subTo(m, d);
1718
+ }
1719
+
1720
+ c.rShiftTo(1, c);
1721
+ } else if (!d.isEven()) {
1722
+ d.subTo(m, d);
1723
+ }
1724
+
1725
+ d.rShiftTo(1, d);
1726
+ }
1727
+
1728
+ if (u.compareTo(v) >= 0) {
1729
+ u.subTo(v, u);
1730
+
1731
+ if (ac) {
1732
+ a.subTo(c, a);
1733
+ }
1734
+
1735
+ b.subTo(d, b);
1736
+ } else {
1737
+ v.subTo(u, v);
1738
+
1739
+ if (ac) {
1740
+ c.subTo(a, c);
1741
+ }
1742
+
1743
+ d.subTo(b, d);
1744
+ }
1745
+ }
1746
+
1747
+ if (v.compareTo(BigInteger.ONE) != 0) {
1748
+ return BigInteger.ZERO;
1749
+ }
1750
+
1751
+ if (d.compareTo(m) >= 0) {
1752
+ return d.subtract(m);
1753
+ }
1754
+
1755
+ if (d.signum() < 0) {
1756
+ d.addTo(m, d);
1757
+ } else {
1758
+ return d;
1759
+ }
1760
+
1761
+ if (d.signum() < 0) {
1762
+ return d.add(m);
1763
+ } else {
1764
+ return d;
1765
+ }
1766
+ } // (public) this^e
1767
+
1768
+
1769
+ pow(e) {
1770
+ return this.exp(e, new NullExp());
1771
+ } // (public) gcd(this,a) (HAC 14.54)
1772
+
1773
+
1774
+ gcd(a) {
1775
+ let x = this.s < 0 ? this.negate() : this.clone();
1776
+ let y = a.s < 0 ? a.negate() : a.clone();
1777
+
1778
+ if (x.compareTo(y) < 0) {
1779
+ const t = x;
1780
+ x = y;
1781
+ y = t;
1782
+ }
1783
+
1784
+ let i = x.getLowestSetBit();
1785
+ let g = y.getLowestSetBit();
1786
+
1787
+ if (g < 0) {
1788
+ return x;
1789
+ }
1790
+
1791
+ if (i < g) {
1792
+ g = i;
1793
+ }
1794
+
1795
+ if (g > 0) {
1796
+ x.rShiftTo(g, x);
1797
+ y.rShiftTo(g, y);
1798
+ }
1799
+
1800
+ while (x.signum() > 0) {
1801
+ if ((i = x.getLowestSetBit()) > 0) {
1802
+ x.rShiftTo(i, x);
1803
+ }
1804
+
1805
+ if ((i = y.getLowestSetBit()) > 0) {
1806
+ y.rShiftTo(i, y);
1807
+ }
1808
+
1809
+ if (x.compareTo(y) >= 0) {
1810
+ x.subTo(y, x);
1811
+ x.rShiftTo(1, x);
1812
+ } else {
1813
+ y.subTo(x, y);
1814
+ y.rShiftTo(1, y);
1815
+ }
1816
+ }
1817
+
1818
+ if (g > 0) {
1819
+ y.lShiftTo(g, y);
1820
+ }
1821
+
1822
+ return y;
1823
+ } // (public) test primality with certainty >= 1-.5^t
1824
+
1825
+
1826
+ isProbablePrime(t) {
1827
+ let i;
1828
+ const x = this.abs();
1829
+
1830
+ if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
1831
+ for (i = 0; i < lowprimes.length; ++i) {
1832
+ if (x[0] == lowprimes[i]) {
1833
+ return true;
1834
+ }
1835
+ }
1836
+
1837
+ return false;
1838
+ }
1839
+
1840
+ if (x.isEven()) {
1841
+ return false;
1842
+ }
1843
+
1844
+ i = 1;
1845
+
1846
+ while (i < lowprimes.length) {
1847
+ let m = lowprimes[i];
1848
+ let j = i + 1;
1849
+
1850
+ while (j < lowprimes.length && m < lplim) {
1851
+ m *= lowprimes[j++];
1852
+ }
1853
+
1854
+ m = x.modInt(m);
1855
+
1856
+ while (i < j) {
1857
+ if (m % lowprimes[i++] == 0) {
1858
+ return false;
1859
+ }
1860
+ }
1861
+ }
1862
+
1863
+ return x.millerRabin(t);
1864
+ } //#endregion PUBLIC
1865
+ //#region PROTECTED
1866
+ // (protected) copy this to r
1867
+
1868
+
1869
+ copyTo(r) {
1870
+ for (let i = this.t - 1; i >= 0; --i) {
1871
+ r[i] = this[i];
1872
+ }
1873
+
1874
+ r.t = this.t;
1875
+ r.s = this.s;
1876
+ } // (protected) set from integer value x, -DV <= x < DV
1877
+
1878
+
1879
+ fromInt(x) {
1880
+ this.t = 1;
1881
+ this.s = x < 0 ? -1 : 0;
1882
+
1883
+ if (x > 0) {
1884
+ this[0] = x;
1885
+ } else if (x < -1) {
1886
+ this[0] = x + this.DV;
1887
+ } else {
1888
+ this.t = 0;
1889
+ }
1890
+ } // (protected) set from string and radix
1891
+
1892
+
1893
+ fromString(s, b) {
1894
+ let k;
1895
+
1896
+ if (b == 16) {
1897
+ k = 4;
1898
+ } else if (b == 8) {
1899
+ k = 3;
1900
+ } else if (b == 256) {
1901
+ k = 8;
1902
+ /* byte array */
1903
+ } else if (b == 2) {
1904
+ k = 1;
1905
+ } else if (b == 32) {
1906
+ k = 5;
1907
+ } else if (b == 4) {
1908
+ k = 2;
1909
+ } else {
1910
+ this.fromRadix(s, b);
1911
+ return;
1912
+ }
1913
+
1914
+ this.t = 0;
1915
+ this.s = 0;
1916
+ let i = s.length;
1917
+ let mi = false;
1918
+ let sh = 0;
1919
+
1920
+ while (--i >= 0) {
1921
+ const x = k == 8 ? +s[i] & 0xff : intAt(s, i);
1922
+
1923
+ if (x < 0) {
1924
+ if (s.charAt(i) == '-') {
1925
+ mi = true;
1926
+ }
1927
+
1928
+ continue;
1929
+ }
1930
+
1931
+ mi = false;
1932
+
1933
+ if (sh == 0) {
1934
+ this[this.t++] = x;
1935
+ } else if (sh + k > this.DB) {
1936
+ this[this.t - 1] |= (x & (1 << this.DB - sh) - 1) << sh;
1937
+ this[this.t++] = x >> this.DB - sh;
1938
+ } else {
1939
+ this[this.t - 1] |= x << sh;
1940
+ }
1941
+
1942
+ sh += k;
1943
+
1944
+ if (sh >= this.DB) {
1945
+ sh -= this.DB;
1946
+ }
1947
+ }
1948
+
1949
+ if (k == 8 && (+s[0] & 0x80) != 0) {
1950
+ this.s = -1;
1951
+
1952
+ if (sh > 0) {
1953
+ this[this.t - 1] |= (1 << this.DB - sh) - 1 << sh;
1954
+ }
1955
+ }
1956
+
1957
+ this.clamp();
1958
+
1959
+ if (mi) {
1960
+ BigInteger.ZERO.subTo(this, this);
1961
+ }
1962
+ } // (protected) clamp off excess high words
1963
+
1964
+
1965
+ clamp() {
1966
+ const c = this.s & this.DM;
1967
+
1968
+ while (this.t > 0 && this[this.t - 1] == c) {
1969
+ --this.t;
1970
+ }
1971
+ } // (protected) r = this << n*DB
1972
+
1973
+
1974
+ dlShiftTo(n, r) {
1975
+ let i;
1976
+
1977
+ for (i = this.t - 1; i >= 0; --i) {
1978
+ r[i + n] = this[i];
1979
+ }
1980
+
1981
+ for (i = n - 1; i >= 0; --i) {
1982
+ r[i] = 0;
1983
+ }
1984
+
1985
+ r.t = this.t + n;
1986
+ r.s = this.s;
1987
+ } // (protected) r = this >> n*DB
1988
+
1989
+
1990
+ drShiftTo(n, r) {
1991
+ for (let i = n; i < this.t; ++i) {
1992
+ r[i - n] = this[i];
1993
+ }
1994
+
1995
+ r.t = Math.max(this.t - n, 0);
1996
+ r.s = this.s;
1997
+ } // (protected) r = this << n
1998
+
1999
+
2000
+ lShiftTo(n, r) {
2001
+ const bs = n % this.DB;
2002
+ const cbs = this.DB - bs;
2003
+ const bm = (1 << cbs) - 1;
2004
+ const ds = Math.floor(n / this.DB);
2005
+ let c = this.s << bs & this.DM;
2006
+
2007
+ for (let i = this.t - 1; i >= 0; --i) {
2008
+ r[i + ds + 1] = this[i] >> cbs | c;
2009
+ c = (this[i] & bm) << bs;
2010
+ }
2011
+
2012
+ for (let i = ds - 1; i >= 0; --i) {
2013
+ r[i] = 0;
2014
+ }
2015
+
2016
+ r[ds] = c;
2017
+ r.t = this.t + ds + 1;
2018
+ r.s = this.s;
2019
+ r.clamp();
2020
+ } // (protected) r = this >> n
2021
+
2022
+
2023
+ rShiftTo(n, r) {
2024
+ r.s = this.s;
2025
+ const ds = Math.floor(n / this.DB);
2026
+
2027
+ if (ds >= this.t) {
2028
+ r.t = 0;
2029
+ return;
2030
+ }
2031
+
2032
+ const bs = n % this.DB;
2033
+ const cbs = this.DB - bs;
2034
+ const bm = (1 << bs) - 1;
2035
+ r[0] = this[ds] >> bs;
2036
+
2037
+ for (let i = ds + 1; i < this.t; ++i) {
2038
+ r[i - ds - 1] |= (this[i] & bm) << cbs;
2039
+ r[i - ds] = this[i] >> bs;
2040
+ }
2041
+
2042
+ if (bs > 0) {
2043
+ r[this.t - ds - 1] |= (this.s & bm) << cbs;
2044
+ }
2045
+
2046
+ r.t = this.t - ds;
2047
+ r.clamp();
2048
+ } // (protected) r = this - a
2049
+
2050
+
2051
+ subTo(a, r) {
2052
+ let i = 0;
2053
+ let c = 0;
2054
+ const m = Math.min(a.t, this.t);
2055
+
2056
+ while (i < m) {
2057
+ c += this[i] - a[i];
2058
+ r[i++] = c & this.DM;
2059
+ c >>= this.DB;
2060
+ }
2061
+
2062
+ if (a.t < this.t) {
2063
+ c -= a.s;
2064
+
2065
+ while (i < this.t) {
2066
+ c += this[i];
2067
+ r[i++] = c & this.DM;
2068
+ c >>= this.DB;
2069
+ }
2070
+
2071
+ c += this.s;
2072
+ } else {
2073
+ c += this.s;
2074
+
2075
+ while (i < a.t) {
2076
+ c -= a[i];
2077
+ r[i++] = c & this.DM;
2078
+ c >>= this.DB;
2079
+ }
2080
+
2081
+ c -= a.s;
2082
+ }
2083
+
2084
+ r.s = c < 0 ? -1 : 0;
2085
+
2086
+ if (c < -1) {
2087
+ r[i++] = this.DV + c;
2088
+ } else if (c > 0) {
2089
+ r[i++] = c;
2090
+ }
2091
+
2092
+ r.t = i;
2093
+ r.clamp();
2094
+ } // (protected) r = this * a, r != this,a (HAC 14.12)
2095
+ // "this" should be the larger one if appropriate.
2096
+
2097
+
2098
+ multiplyTo(a, r) {
2099
+ const x = this.abs();
2100
+ const y = a.abs();
2101
+ let i = x.t;
2102
+ r.t = i + y.t;
2103
+
2104
+ while (--i >= 0) {
2105
+ r[i] = 0;
2106
+ }
2107
+
2108
+ for (i = 0; i < y.t; ++i) {
2109
+ r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
2110
+ }
2111
+
2112
+ r.s = 0;
2113
+ r.clamp();
2114
+
2115
+ if (this.s != a.s) {
2116
+ BigInteger.ZERO.subTo(r, r);
2117
+ }
2118
+ } // (protected) r = this^2, r != this (HAC 14.16)
2119
+
2120
+
2121
+ squareTo(r) {
2122
+ const x = this.abs();
2123
+ let i = r.t = 2 * x.t;
2124
+
2125
+ while (--i >= 0) {
2126
+ r[i] = 0;
2127
+ }
2128
+
2129
+ for (i = 0; i < x.t - 1; ++i) {
2130
+ const c = x.am(i, x[i], r, 2 * i, 0, 1);
2131
+
2132
+ if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
2133
+ r[i + x.t] -= x.DV;
2134
+ r[i + x.t + 1] = 1;
2135
+ }
2136
+ }
2137
+
2138
+ if (r.t > 0) {
2139
+ r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
2140
+ }
2141
+
2142
+ r.s = 0;
2143
+ r.clamp();
2144
+ } // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
2145
+ // r != q, this != m. q or r may be null.
2146
+
2147
+
2148
+ divRemTo(m, q, r) {
2149
+ const pm = m.abs();
2150
+
2151
+ if (pm.t <= 0) {
2152
+ return;
2153
+ }
2154
+
2155
+ const pt = this.abs();
2156
+
2157
+ if (pt.t < pm.t) {
2158
+ if (q != null) {
2159
+ q.fromInt(0);
2160
+ }
2161
+
2162
+ if (r != null) {
2163
+ this.copyTo(r);
2164
+ }
2165
+
2166
+ return;
2167
+ }
2168
+
2169
+ if (r == null) {
2170
+ r = nbi();
2171
+ }
2172
+
2173
+ const y = nbi();
2174
+ const ts = this.s;
2175
+ const ms = m.s;
2176
+ const nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
2177
+
2178
+ if (nsh > 0) {
2179
+ pm.lShiftTo(nsh, y);
2180
+ pt.lShiftTo(nsh, r);
2181
+ } else {
2182
+ pm.copyTo(y);
2183
+ pt.copyTo(r);
2184
+ }
2185
+
2186
+ const ys = y.t;
2187
+ const y0 = y[ys - 1];
2188
+
2189
+ if (y0 == 0) {
2190
+ return;
2191
+ }
2192
+
2193
+ const yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
2194
+ const d1 = this.FV / yt;
2195
+ const d2 = (1 << this.F1) / yt;
2196
+ const e = 1 << this.F2;
2197
+ let i = r.t;
2198
+ let j = i - ys;
2199
+ const t = q == null ? nbi() : q;
2200
+ y.dlShiftTo(j, t);
2201
+
2202
+ if (r.compareTo(t) >= 0) {
2203
+ r[r.t++] = 1;
2204
+ r.subTo(t, r);
2205
+ }
2206
+
2207
+ BigInteger.ONE.dlShiftTo(ys, t);
2208
+ t.subTo(y, y); // "negative" y so we can replace sub with am later
2209
+
2210
+ while (y.t < ys) {
2211
+ y[y.t++] = 0;
2212
+ }
2213
+
2214
+ while (--j >= 0) {
2215
+ // Estimate quotient digit
2216
+ let qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
2217
+
2218
+ if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
2219
+ // Try it out
2220
+ y.dlShiftTo(j, t);
2221
+ r.subTo(t, r);
2222
+
2223
+ while (r[i] < --qd) {
2224
+ r.subTo(t, r);
2225
+ }
2226
+ }
2227
+ }
2228
+
2229
+ if (q != null) {
2230
+ r.drShiftTo(ys, q);
2231
+
2232
+ if (ts != ms) {
2233
+ BigInteger.ZERO.subTo(q, q);
2234
+ }
2235
+ }
2236
+
2237
+ r.t = ys;
2238
+ r.clamp();
2239
+
2240
+ if (nsh > 0) {
2241
+ r.rShiftTo(nsh, r);
2242
+ } // Denormalize remainder
2243
+
2244
+
2245
+ if (ts < 0) {
2246
+ BigInteger.ZERO.subTo(r, r);
2247
+ }
2248
+ } // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
2249
+ // justification:
2250
+ // xy == 1 (mod m)
2251
+ // xy = 1+km
2252
+ // xy(2-xy) = (1+km)(1-km)
2253
+ // x[y(2-xy)] = 1-k^2m^2
2254
+ // x[y(2-xy)] == 1 (mod m^2)
2255
+ // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
2256
+ // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
2257
+ // JS multiply "overflows" differently from C/C++, so care is needed here.
2258
+
2259
+
2260
+ invDigit() {
2261
+ if (this.t < 1) {
2262
+ return 0;
2263
+ }
2264
+
2265
+ const x = this[0];
2266
+
2267
+ if ((x & 1) == 0) {
2268
+ return 0;
2269
+ }
2270
+
2271
+ let y = x & 3; // y == 1/x mod 2^2
2272
+
2273
+ y = y * (2 - (x & 0xf) * y) & 0xf; // y == 1/x mod 2^4
2274
+
2275
+ y = y * (2 - (x & 0xff) * y) & 0xff; // y == 1/x mod 2^8
2276
+
2277
+ y = y * (2 - ((x & 0xffff) * y & 0xffff)) & 0xffff; // y == 1/x mod 2^16
2278
+ // last step - calculate inverse mod DV directly;
2279
+ // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
2280
+
2281
+ y = y * (2 - x * y % this.DV) % this.DV; // y == 1/x mod 2^dbits
2282
+ // we really want the negative inverse, and -DV < y < DV
2283
+
2284
+ return y > 0 ? this.DV - y : -y;
2285
+ } // (protected) true iff this is even
2286
+
2287
+
2288
+ isEven() {
2289
+ return (this.t > 0 ? this[0] & 1 : this.s) == 0;
2290
+ } // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
2291
+
2292
+
2293
+ exp(e, z) {
2294
+ if (e > 0xffffffff || e < 1) {
2295
+ return BigInteger.ONE;
2296
+ }
2297
+
2298
+ let r = nbi();
2299
+ let r2 = nbi();
2300
+ const g = z.convert(this);
2301
+ let i = nbits(e) - 1;
2302
+ g.copyTo(r);
2303
+
2304
+ while (--i >= 0) {
2305
+ z.sqrTo(r, r2);
2306
+
2307
+ if ((e & 1 << i) > 0) {
2308
+ z.mulTo(r2, g, r);
2309
+ } else {
2310
+ const t = r;
2311
+ r = r2;
2312
+ r2 = t;
2313
+ }
2314
+ }
2315
+
2316
+ return z.revert(r);
2317
+ } // (protected) return x s.t. r^x < DV
2318
+
2319
+
2320
+ chunkSize(r) {
2321
+ return Math.floor(Math.LN2 * this.DB / Math.log(r));
2322
+ } // (protected) convert to radix string
2323
+
2324
+
2325
+ toRadix(b) {
2326
+ if (b == null) {
2327
+ b = 10;
2328
+ }
2329
+
2330
+ if (this.signum() == 0 || b < 2 || b > 36) {
2331
+ return '0';
2332
+ }
2333
+
2334
+ const cs = this.chunkSize(b);
2335
+ const a = Math.pow(b, cs);
2336
+ const d = nbv(a);
2337
+ const y = nbi();
2338
+ const z = nbi();
2339
+ let r = '';
2340
+ this.divRemTo(d, y, z);
2341
+
2342
+ while (y.signum() > 0) {
2343
+ r = (a + z.intValue()).toString(b).substr(1) + r;
2344
+ y.divRemTo(d, y, z);
2345
+ }
2346
+
2347
+ return z.intValue().toString(b) + r;
2348
+ } // (protected) convert from radix string
2349
+
2350
+
2351
+ fromRadix(s, b) {
2352
+ this.fromInt(0);
2353
+
2354
+ if (b == null) {
2355
+ b = 10;
2356
+ }
2357
+
2358
+ const cs = this.chunkSize(b);
2359
+ const d = Math.pow(b, cs);
2360
+ let mi = false;
2361
+ let j = 0;
2362
+ let w = 0;
2363
+
2364
+ for (let i = 0; i < s.length; ++i) {
2365
+ const x = intAt(s, i);
2366
+
2367
+ if (x < 0) {
2368
+ if (s.charAt(i) == '-' && this.signum() == 0) {
2369
+ mi = true;
2370
+ }
2371
+
2372
+ continue;
2373
+ }
2374
+
2375
+ w = b * w + x;
2376
+
2377
+ if (++j >= cs) {
2378
+ this.dMultiply(d);
2379
+ this.dAddOffset(w, 0);
2380
+ j = 0;
2381
+ w = 0;
2382
+ }
2383
+ }
2384
+
2385
+ if (j > 0) {
2386
+ this.dMultiply(Math.pow(b, j));
2387
+ this.dAddOffset(w, 0);
2388
+ }
2389
+
2390
+ if (mi) {
2391
+ BigInteger.ZERO.subTo(this, this);
2392
+ }
2393
+ } // (protected) alternate constructor
2394
+
2395
+
2396
+ fromNumber(a, b, c) {
2397
+ if ('number' == typeof b) {
2398
+ // new BigInteger(int,int,RNG)
2399
+ if (a < 2) {
2400
+ this.fromInt(1);
2401
+ } else {
2402
+ if (typeof c !== 'undefined') {
2403
+ this.fromNumber(a, c);
2404
+ }
2405
+
2406
+ if (!this.testBit(a - 1)) {
2407
+ // force MSB set
2408
+ this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
2409
+ }
2410
+
2411
+ if (this.isEven()) {
2412
+ this.dAddOffset(1, 0);
2413
+ } // force odd
2414
+
2415
+
2416
+ while (!this.isProbablePrime(b)) {
2417
+ this.dAddOffset(2, 0);
2418
+
2419
+ if (this.bitLength() > a) {
2420
+ this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
2421
+ }
2422
+ }
2423
+ }
2424
+ } else {
2425
+ // new BigInteger(int,RNG)
2426
+ const x = [];
2427
+ const t = a & 7;
2428
+ x.length = (a >> 3) + 1;
2429
+ b.nextBytes(x);
2430
+
2431
+ if (t > 0) {
2432
+ x[0] &= (1 << t) - 1;
2433
+ } else {
2434
+ x[0] = 0;
2435
+ }
2436
+
2437
+ this.fromString(x, 256);
2438
+ }
2439
+ } // (protected) r = this op a (bitwise)
2440
+
2441
+
2442
+ bitwiseTo(a, op, r) {
2443
+ let i;
2444
+ let f;
2445
+ const m = Math.min(a.t, this.t);
2446
+
2447
+ for (i = 0; i < m; ++i) {
2448
+ r[i] = op(this[i], a[i]);
2449
+ }
2450
+
2451
+ if (a.t < this.t) {
2452
+ f = a.s & this.DM;
2453
+
2454
+ for (i = m; i < this.t; ++i) {
2455
+ r[i] = op(this[i], f);
2456
+ }
2457
+
2458
+ r.t = this.t;
2459
+ } else {
2460
+ f = this.s & this.DM;
2461
+
2462
+ for (i = m; i < a.t; ++i) {
2463
+ r[i] = op(f, a[i]);
2464
+ }
2465
+
2466
+ r.t = a.t;
2467
+ }
2468
+
2469
+ r.s = op(this.s, a.s);
2470
+ r.clamp();
2471
+ } // (protected) this op (1<<n)
2472
+
2473
+
2474
+ changeBit(n, op) {
2475
+ const r = BigInteger.ONE.shiftLeft(n);
2476
+ this.bitwiseTo(r, op, r);
2477
+ return r;
2478
+ } // (protected) r = this + a
2479
+
2480
+
2481
+ addTo(a, r) {
2482
+ let i = 0;
2483
+ let c = 0;
2484
+ const m = Math.min(a.t, this.t);
2485
+
2486
+ while (i < m) {
2487
+ c += this[i] + a[i];
2488
+ r[i++] = c & this.DM;
2489
+ c >>= this.DB;
2490
+ }
2491
+
2492
+ if (a.t < this.t) {
2493
+ c += a.s;
2494
+
2495
+ while (i < this.t) {
2496
+ c += this[i];
2497
+ r[i++] = c & this.DM;
2498
+ c >>= this.DB;
2499
+ }
2500
+
2501
+ c += this.s;
2502
+ } else {
2503
+ c += this.s;
2504
+
2505
+ while (i < a.t) {
2506
+ c += a[i];
2507
+ r[i++] = c & this.DM;
2508
+ c >>= this.DB;
2509
+ }
2510
+
2511
+ c += a.s;
2512
+ }
2513
+
2514
+ r.s = c < 0 ? -1 : 0;
2515
+
2516
+ if (c > 0) {
2517
+ r[i++] = c;
2518
+ } else if (c < -1) {
2519
+ r[i++] = this.DV + c;
2520
+ }
2521
+
2522
+ r.t = i;
2523
+ r.clamp();
2524
+ } // (protected) this *= n, this >= 0, 1 < n < DV
2525
+
2526
+
2527
+ dMultiply(n) {
2528
+ this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
2529
+ ++this.t;
2530
+ this.clamp();
2531
+ } // (protected) this += n << w words, this >= 0
2532
+
2533
+
2534
+ dAddOffset(n, w) {
2535
+ if (n == 0) {
2536
+ return;
2537
+ }
2538
+
2539
+ while (this.t <= w) {
2540
+ this[this.t++] = 0;
2541
+ }
2542
+
2543
+ this[w] += n;
2544
+
2545
+ while (this[w] >= this.DV) {
2546
+ this[w] -= this.DV;
2547
+
2548
+ if (++w >= this.t) {
2549
+ this[this.t++] = 0;
2550
+ }
2551
+
2552
+ ++this[w];
2553
+ }
2554
+ } // (protected) r = lower n words of "this * a", a.t <= n
2555
+ // "this" should be the larger one if appropriate.
2556
+
2557
+
2558
+ multiplyLowerTo(a, n, r) {
2559
+ let i = Math.min(this.t + a.t, n);
2560
+ r.s = 0; // assumes a,this >= 0
2561
+
2562
+ r.t = i;
2563
+
2564
+ while (i > 0) {
2565
+ r[--i] = 0;
2566
+ }
2567
+
2568
+ for (const j = r.t - this.t; i < j; ++i) {
2569
+ r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
2570
+ }
2571
+
2572
+ for (const j = Math.min(a.t, n); i < j; ++i) {
2573
+ this.am(0, a[i], r, i, 0, n - i);
2574
+ }
2575
+
2576
+ r.clamp();
2577
+ } // (protected) r = "this * a" without lower n words, n > 0
2578
+ // "this" should be the larger one if appropriate.
2579
+
2580
+
2581
+ multiplyUpperTo(a, n, r) {
2582
+ --n;
2583
+ let i = r.t = this.t + a.t - n;
2584
+ r.s = 0; // assumes a,this >= 0
2585
+
2586
+ while (--i >= 0) {
2587
+ r[i] = 0;
2588
+ }
2589
+
2590
+ for (i = Math.max(n - this.t, 0); i < a.t; ++i) {
2591
+ r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
2592
+ }
2593
+
2594
+ r.clamp();
2595
+ r.drShiftTo(1, r);
2596
+ } // (protected) this % n, n < 2^26
2597
+
2598
+
2599
+ modInt(n) {
2600
+ if (n <= 0) {
2601
+ return 0;
2602
+ }
2603
+
2604
+ const d = this.DV % n;
2605
+ let r = this.s < 0 ? n - 1 : 0;
2606
+
2607
+ if (this.t > 0) {
2608
+ if (d == 0) {
2609
+ r = this[0] % n;
2610
+ } else {
2611
+ for (let i = this.t - 1; i >= 0; --i) {
2612
+ r = (d * r + this[i]) % n;
2613
+ }
2614
+ }
2615
+ }
2616
+
2617
+ return r;
2618
+ } // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
2619
+
2620
+
2621
+ millerRabin(t) {
2622
+ const n1 = this.subtract(BigInteger.ONE);
2623
+ const k = n1.getLowestSetBit();
2624
+
2625
+ if (k <= 0) {
2626
+ return false;
2627
+ }
2628
+
2629
+ const r = n1.shiftRight(k);
2630
+ t = t + 1 >> 1;
2631
+
2632
+ if (t > lowprimes.length) {
2633
+ t = lowprimes.length;
2634
+ }
2635
+
2636
+ const a = nbi();
2637
+
2638
+ for (let i = 0; i < t; ++i) {
2639
+ // Pick bases at random, instead of starting at 2
2640
+ a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
2641
+ let y = a.modPow(r, this);
2642
+
2643
+ if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
2644
+ let j = 1;
2645
+
2646
+ while (j++ < k && y.compareTo(n1) != 0) {
2647
+ y = y.modPowInt(2, this);
2648
+
2649
+ if (y.compareTo(BigInteger.ONE) == 0) {
2650
+ return false;
2651
+ }
2652
+ }
2653
+
2654
+ if (y.compareTo(n1) != 0) {
2655
+ return false;
2656
+ }
2657
+ }
2658
+ }
2659
+
2660
+ return true;
2661
+ } // (public) this^2
2662
+
2663
+
2664
+ square() {
2665
+ const r = nbi();
2666
+ this.squareTo(r);
2667
+ return r;
2668
+ } //#region ASYNC
2669
+ // Public API method
2670
+
2671
+
2672
+ gcda(a, callback) {
2673
+ let x = this.s < 0 ? this.negate() : this.clone();
2674
+ let y = a.s < 0 ? a.negate() : a.clone();
2675
+
2676
+ if (x.compareTo(y) < 0) {
2677
+ const t = x;
2678
+ x = y;
2679
+ y = t;
2680
+ }
2681
+
2682
+ let i = x.getLowestSetBit();
2683
+ let g = y.getLowestSetBit();
2684
+
2685
+ if (g < 0) {
2686
+ callback(x);
2687
+ return;
2688
+ }
2689
+
2690
+ if (i < g) {
2691
+ g = i;
2692
+ }
2693
+
2694
+ if (g > 0) {
2695
+ x.rShiftTo(g, x);
2696
+ y.rShiftTo(g, y);
2697
+ } // Workhorse of the algorithm, gets called 200 - 800 times per 512 bit keygen.
2698
+
2699
+
2700
+ const gcda1 = function gcda1() {
2701
+ if ((i = x.getLowestSetBit()) > 0) {
2702
+ x.rShiftTo(i, x);
2703
+ }
2704
+
2705
+ if ((i = y.getLowestSetBit()) > 0) {
2706
+ y.rShiftTo(i, y);
2707
+ }
2708
+
2709
+ if (x.compareTo(y) >= 0) {
2710
+ x.subTo(y, x);
2711
+ x.rShiftTo(1, x);
2712
+ } else {
2713
+ y.subTo(x, y);
2714
+ y.rShiftTo(1, y);
2715
+ }
2716
+
2717
+ if (!(x.signum() > 0)) {
2718
+ if (g > 0) {
2719
+ y.lShiftTo(g, y);
2720
+ }
2721
+
2722
+ setTimeout(function () {
2723
+ callback(y);
2724
+ }, 0); // escape
2725
+ } else {
2726
+ setTimeout(gcda1, 0);
2727
+ }
2728
+ };
2729
+
2730
+ setTimeout(gcda1, 10);
2731
+ } // (protected) alternate constructor
2732
+
2733
+
2734
+ fromNumberAsync(a, b, c, callback) {
2735
+ if ('number' == typeof b) {
2736
+ if (a < 2) {
2737
+ this.fromInt(1);
2738
+ } else {
2739
+ this.fromNumber(a, c);
2740
+
2741
+ if (!this.testBit(a - 1)) {
2742
+ this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
2743
+ }
2744
+
2745
+ if (this.isEven()) {
2746
+ this.dAddOffset(1, 0);
2747
+ }
2748
+
2749
+ const bnpfn1 = () => {
2750
+ this.dAddOffset(2, 0);
2751
+
2752
+ if (this.bitLength() > a) {
2753
+ this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
2754
+ }
2755
+
2756
+ if (this.isProbablePrime(b)) {
2757
+ setTimeout(function () {
2758
+ callback();
2759
+ }, 0); // escape
2760
+ } else {
2761
+ setTimeout(bnpfn1, 0);
2762
+ }
2763
+ };
2764
+
2765
+ setTimeout(bnpfn1, 0);
2766
+ }
2767
+ } else {
2768
+ const x = [];
2769
+ const t = a & 7;
2770
+ x.length = (a >> 3) + 1;
2771
+ b.nextBytes(x);
2772
+
2773
+ if (t > 0) {
2774
+ x[0] &= (1 << t) - 1;
2775
+ } else {
2776
+ x[0] = 0;
2777
+ }
2778
+
2779
+ this.fromString(x, 256);
2780
+ }
2781
+ }
2782
+
2783
+ am(i, x, w, j, c, n) {
2784
+ if (isIE) {
2785
+ // am2 avoids a big mult-and-extract completely.
2786
+ // Max digit bits should be <= 30 because we do bitwise ops
2787
+ // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
2788
+ const xl = x & 0x7fff;
2789
+ const xh = x >> 15;
2790
+
2791
+ while (--n >= 0) {
2792
+ let l = this[i] & 0x7fff;
2793
+ const h = this[i++] >> 15;
2794
+ const m = xh * l + h * xl;
2795
+ l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
2796
+ c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
2797
+ w[j++] = l & 0x3fffffff;
2798
+ }
2799
+
2800
+ return c;
2801
+ } else if (isNetscapeOrWx) {
2802
+ // am1: use a single mult and divide to get the high bits,
2803
+ // max digit bits should be 26 because
2804
+ // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
2805
+ while (--n >= 0) {
2806
+ const v = x * this[i++] + w[j] + c;
2807
+ c = Math.floor(v / 0x4000000);
2808
+ w[j++] = v & 0x3ffffff;
2809
+ }
2810
+
2811
+ return c;
2812
+ } else {
2813
+ // Mozilla/Netscape seems to prefer am3
2814
+ // Alternately, set max digit bits to 28 since some
2815
+ // browsers slow down when dealing with 32-bit numbers.
2816
+ const xl = x & 0x3fff;
2817
+ const xh = x >> 14;
2818
+
2819
+ while (--n >= 0) {
2820
+ let l = this[i] & 0x3fff;
2821
+ const h = this[i++] >> 14;
2822
+ const m = xh * l + h * xl;
2823
+ l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
2824
+ c = (l >> 28) + (m >> 14) + xh * h;
2825
+ w[j++] = l & 0xfffffff;
2826
+ }
2827
+
2828
+ return c;
2829
+ }
2830
+ }
2831
+
2832
+ } //#region REDUCERS
2833
+ //#region NullExp
2834
+
2835
+ class NullExp {
2836
+ // NullExp.prototype.convert = nNop;
2837
+ convert(x) {
2838
+ return x;
2839
+ } // NullExp.prototype.revert = nNop;
2840
+
2841
+
2842
+ revert(x) {
2843
+ return x;
2844
+ } // NullExp.prototype.mulTo = nMulTo;
2845
+
2846
+
2847
+ mulTo(x, y, r) {
2848
+ x.multiplyTo(y, r);
2849
+ } // NullExp.prototype.sqrTo = nSqrTo;
2850
+
2851
+
2852
+ sqrTo(x, r) {
2853
+ x.squareTo(r);
2854
+ }
2855
+
2856
+ } // Modular reduction using "classic" algorithm
2857
+
2858
+
2859
+ class Classic {
2860
+ constructor(m) {
2861
+ this.m = m;
2862
+ } // Classic.prototype.convert = cConvert;
2863
+
2864
+
2865
+ convert(x) {
2866
+ if (x.s < 0 || x.compareTo(this.m) >= 0) {
2867
+ return x.mod(this.m);
2868
+ } else {
2869
+ return x;
2870
+ }
2871
+ } // Classic.prototype.revert = cRevert;
2872
+
2873
+
2874
+ revert(x) {
2875
+ return x;
2876
+ } // Classic.prototype.reduce = cReduce;
2877
+
2878
+
2879
+ reduce(x) {
2880
+ x.divRemTo(this.m, null, x);
2881
+ } // Classic.prototype.mulTo = cMulTo;
2882
+
2883
+
2884
+ mulTo(x, y, r) {
2885
+ x.multiplyTo(y, r);
2886
+ this.reduce(r);
2887
+ } // Classic.prototype.sqrTo = cSqrTo;
2888
+
2889
+
2890
+ sqrTo(x, r) {
2891
+ x.squareTo(r);
2892
+ this.reduce(r);
2893
+ }
2894
+
2895
+ } //#endregion
2896
+ //#region Montgomery
2897
+ // Montgomery reduction
2898
+
2899
+
2900
+ class Montgomery {
2901
+ constructor(m) {
2902
+ this.m = m;
2903
+ this.mp = m.invDigit();
2904
+ this.mpl = this.mp & 0x7fff;
2905
+ this.mph = this.mp >> 15;
2906
+ this.um = (1 << m.DB - 15) - 1;
2907
+ this.mt2 = 2 * m.t;
2908
+ } // Montgomery.prototype.convert = montConvert;
2909
+ // xR mod m
2910
+
2911
+
2912
+ convert(x) {
2913
+ const r = nbi();
2914
+ x.abs().dlShiftTo(this.m.t, r);
2915
+ r.divRemTo(this.m, null, r);
2916
+
2917
+ if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) {
2918
+ this.m.subTo(r, r);
2919
+ }
2920
+
2921
+ return r;
2922
+ } // Montgomery.prototype.revert = montRevert;
2923
+ // x/R mod m
2924
+
2925
+
2926
+ revert(x) {
2927
+ const r = nbi();
2928
+ x.copyTo(r);
2929
+ this.reduce(r);
2930
+ return r;
2931
+ } // Montgomery.prototype.reduce = montReduce;
2932
+ // x = x/R mod m (HAC 14.32)
2933
+
2934
+
2935
+ reduce(x) {
2936
+ while (x.t <= this.mt2) {
2937
+ // pad x so am has enough room later
2938
+ x[x.t++] = 0;
2939
+ }
2940
+
2941
+ for (let i = 0; i < this.m.t; ++i) {
2942
+ // faster way of calculating u0 = x[i]*mp mod DV
2943
+ let j = x[i] & 0x7fff;
2944
+ const u0 = j * this.mpl + ((j * this.mph + (x[i] >> 15) * this.mpl & this.um) << 15) & x.DM; // use am to combine the multiply-shift-add into one call
2945
+
2946
+ j = i + this.m.t;
2947
+ x[j] += this.m.am(0, u0, x, i, 0, this.m.t); // propagate carry
2948
+
2949
+ while (x[j] >= x.DV) {
2950
+ x[j] -= x.DV;
2951
+ x[++j]++;
2952
+ }
2953
+ }
2954
+
2955
+ x.clamp();
2956
+ x.drShiftTo(this.m.t, x);
2957
+
2958
+ if (x.compareTo(this.m) >= 0) {
2959
+ x.subTo(this.m, x);
2960
+ }
2961
+ } // Montgomery.prototype.mulTo = montMulTo;
2962
+ // r = "xy/R mod m"; x,y != r
2963
+
2964
+
2965
+ mulTo(x, y, r) {
2966
+ x.multiplyTo(y, r);
2967
+ this.reduce(r);
2968
+ } // Montgomery.prototype.sqrTo = montSqrTo;
2969
+ // r = "x^2/R mod m"; x != r
2970
+
2971
+
2972
+ sqrTo(x, r) {
2973
+ x.squareTo(r);
2974
+ this.reduce(r);
2975
+ }
2976
+
2977
+ } //#endregion Montgomery
2978
+ //#region Barrett
2979
+ // Barrett modular reduction
2980
+
2981
+
2982
+ class Barrett {
2983
+ constructor(m) {
2984
+ this.m = m; // setup Barrett
2985
+
2986
+ this.r2 = nbi();
2987
+ this.q3 = nbi();
2988
+ BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
2989
+ this.mu = this.r2.divide(m);
2990
+ } // Barrett.prototype.convert = barrettConvert;
2991
+
2992
+
2993
+ convert(x) {
2994
+ if (x.s < 0 || x.t > 2 * this.m.t) {
2995
+ return x.mod(this.m);
2996
+ } else if (x.compareTo(this.m) < 0) {
2997
+ return x;
2998
+ } else {
2999
+ const r = nbi();
3000
+ x.copyTo(r);
3001
+ this.reduce(r);
3002
+ return r;
3003
+ }
3004
+ } // Barrett.prototype.revert = barrettRevert;
3005
+
3006
+
3007
+ revert(x) {
3008
+ return x;
3009
+ } // Barrett.prototype.reduce = barrettReduce;
3010
+ // x = x mod m (HAC 14.42)
3011
+
3012
+
3013
+ reduce(x) {
3014
+ x.drShiftTo(this.m.t - 1, this.r2);
3015
+
3016
+ if (x.t > this.m.t + 1) {
3017
+ x.t = this.m.t + 1;
3018
+ x.clamp();
3019
+ }
3020
+
3021
+ this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
3022
+ this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
3023
+
3024
+ while (x.compareTo(this.r2) < 0) {
3025
+ x.dAddOffset(1, this.m.t + 1);
3026
+ }
3027
+
3028
+ x.subTo(this.r2, x);
3029
+
3030
+ while (x.compareTo(this.m) >= 0) {
3031
+ x.subTo(this.m, x);
3032
+ }
3033
+ } // Barrett.prototype.mulTo = barrettMulTo;
3034
+ // r = x*y mod m; x,y != r
3035
+
3036
+
3037
+ mulTo(x, y, r) {
3038
+ x.multiplyTo(y, r);
3039
+ this.reduce(r);
3040
+ } // Barrett.prototype.sqrTo = barrettSqrTo;
3041
+ // r = x^2 mod m; x != r
3042
+
3043
+
3044
+ sqrTo(x, r) {
3045
+ x.squareTo(r);
3046
+ this.reduce(r);
3047
+ }
3048
+
3049
+ } //#endregion
3050
+ //#endregion REDUCERS
3051
+ // return new, unset BigInteger
3052
+
3053
+
3054
+ function nbi() {
3055
+ return new BigInteger(null);
3056
+ }
3057
+ function parseBigInt(str, r) {
3058
+ return new BigInteger(str, r);
3059
+ } // Digit conversions
3060
+
3061
+ const BI_RC = [];
3062
+ let rr;
3063
+ let vv;
3064
+ rr = '0'.charCodeAt(0);
3065
+
3066
+ for (vv = 0; vv <= 9; ++vv) {
3067
+ BI_RC[rr++] = vv;
3068
+ }
3069
+
3070
+ rr = 'a'.charCodeAt(0);
3071
+
3072
+ for (vv = 10; vv < 36; ++vv) {
3073
+ BI_RC[rr++] = vv;
3074
+ }
3075
+
3076
+ rr = 'A'.charCodeAt(0);
3077
+
3078
+ for (vv = 10; vv < 36; ++vv) {
3079
+ BI_RC[rr++] = vv;
3080
+ }
3081
+
3082
+ function intAt(s, i) {
3083
+ const c = BI_RC[s.charCodeAt(i)];
3084
+ return c == null ? -1 : c;
3085
+ } // return bigint initialized to value
3086
+
3087
+ function nbv(i) {
3088
+ const r = nbi();
3089
+ r.fromInt(i);
3090
+ return r;
3091
+ } // returns bit length of the integer x
3092
+
3093
+ function nbits(x) {
3094
+ let r = 1;
3095
+ let t;
3096
+
3097
+ if ((t = x >>> 16) != 0) {
3098
+ x = t;
3099
+ r += 16;
3100
+ }
3101
+
3102
+ if ((t = x >> 8) != 0) {
3103
+ x = t;
3104
+ r += 8;
3105
+ }
3106
+
3107
+ if ((t = x >> 4) != 0) {
3108
+ x = t;
3109
+ r += 4;
3110
+ }
3111
+
3112
+ if ((t = x >> 2) != 0) {
3113
+ x = t;
3114
+ r += 2;
3115
+ }
3116
+
3117
+ if ((t = x >> 1) != 0) {
3118
+ x = t;
3119
+ r += 1;
3120
+ }
3121
+
3122
+ return r;
3123
+ } // "constants"
3124
+
3125
+ BigInteger.ZERO = nbv(0);
3126
+ BigInteger.ONE = nbv(1);
3127
+
3128
+ // prng4.js - uses Arcfour as a PRNG
3129
+ class Arcfour {
3130
+ constructor() {
3131
+ this.i = 0;
3132
+ this.j = 0;
3133
+ this.S = [];
3134
+ } // Arcfour.prototype.init = ARC4init;
3135
+ // Initialize arcfour context from key, an array of ints, each from [0..255]
3136
+
3137
+
3138
+ init(key) {
3139
+ let i;
3140
+ let j;
3141
+ let t;
3142
+
3143
+ for (i = 0; i < 256; ++i) {
3144
+ this.S[i] = i;
3145
+ }
3146
+
3147
+ j = 0;
3148
+
3149
+ for (i = 0; i < 256; ++i) {
3150
+ j = j + this.S[i] + key[i % key.length] & 255;
3151
+ t = this.S[i];
3152
+ this.S[i] = this.S[j];
3153
+ this.S[j] = t;
3154
+ }
3155
+
3156
+ this.i = 0;
3157
+ this.j = 0;
3158
+ } // Arcfour.prototype.next = ARC4next;
3159
+
3160
+
3161
+ next() {
3162
+ this.i = this.i + 1 & 255;
3163
+ this.j = this.j + this.S[this.i] & 255;
3164
+ const t = this.S[this.i];
3165
+ this.S[this.i] = this.S[this.j];
3166
+ this.S[this.j] = t;
3167
+ return this.S[t + this.S[this.i] & 255];
3168
+ }
3169
+
3170
+ } // Plug in your RNG constructor here
3171
+
3172
+ function prng_newstate() {
3173
+ return new Arcfour();
3174
+ } // Pool size must be a multiple of 4 and greater than 32.
3175
+ // An array of bytes the size of the pool will be passed to init()
3176
+
3177
+ const rng_psize = 256;
3178
+
3179
+ // Random number generator - requires a PRNG backend, e.g. prng4.js
3180
+ let rng_state;
3181
+ let rng_pool = [];
3182
+ let rng_pptr; // Initialize the pool with junk if needed.
3183
+
3184
+ if (rng_pool == null) {
3185
+ rng_pool = [];
3186
+ rng_pptr = 0;
3187
+ }
3188
+
3189
+ function rng_get_byte() {
3190
+ if (rng_state == null) {
3191
+ rng_state = prng_newstate(); // At this point, we may not have collected enough entropy. If not, fall back to Math.random
3192
+
3193
+ while (rng_pptr < rng_psize) {
3194
+ const random = Math.floor(65536 * Math.random());
3195
+ rng_pool[rng_pptr++] = random & 255;
3196
+ }
3197
+
3198
+ rng_state.init(rng_pool);
3199
+
3200
+ for (rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) {
3201
+ rng_pool[rng_pptr] = 0;
3202
+ }
3203
+
3204
+ rng_pptr = 0;
3205
+ } // TODO: allow reseeding after first request
3206
+
3207
+
3208
+ return rng_state.next();
3209
+ }
3210
+
3211
+ class SecureRandom {
3212
+ nextBytes(ba) {
3213
+ for (let i = 0; i < ba.length; ++i) {
3214
+ ba[i] = rng_get_byte();
3215
+ }
3216
+ }
3217
+
3218
+ }
3219
+
3220
+ // Depends on jsbn.js and rng.js
3221
+
3222
+ function pkcs1pad2(s, n) {
3223
+ if (n < s.length + 11) {
3224
+ // TODO: fix for utf-8
3225
+ console.error('Message too long for RSA');
3226
+ return null;
3227
+ }
3228
+
3229
+ const ba = [];
3230
+ let i = s.length - 1;
3231
+
3232
+ while (i >= 0 && n > 0) {
3233
+ const c = s.charCodeAt(i--);
3234
+
3235
+ if (c < 128) {
3236
+ // encode using utf-8
3237
+ ba[--n] = c;
3238
+ } else if (c > 127 && c < 2048) {
3239
+ ba[--n] = c & 63 | 128;
3240
+ ba[--n] = c >> 6 | 192;
3241
+ } else {
3242
+ ba[--n] = c & 63 | 128;
3243
+ ba[--n] = c >> 6 & 63 | 128;
3244
+ ba[--n] = c >> 12 | 224;
3245
+ }
3246
+ }
3247
+
3248
+ ba[--n] = 0;
3249
+ const rng = new SecureRandom();
3250
+ const x = [];
3251
+
3252
+ while (n > 2) {
3253
+ // random non-zero pad
3254
+ x[0] = 0;
3255
+
3256
+ while (x[0] == 0) {
3257
+ rng.nextBytes(x);
3258
+ }
3259
+
3260
+ ba[--n] = x[0];
3261
+ }
3262
+
3263
+ ba[--n] = 2;
3264
+ ba[--n] = 0;
3265
+ return new BigInteger(ba);
3266
+ } // "empty" RSA key constructor
3267
+
3268
+
3269
+ class RSAKey {
3270
+ constructor() {
3271
+ this.e = 0;
3272
+ } //#region PROTECTED
3273
+ // protected
3274
+ // RSAKey.prototype.doPublic = RSADoPublic;
3275
+ // Perform raw public operation on "x": return x^e (mod n)
3276
+
3277
+
3278
+ doPublic(x) {
3279
+ if (this.n) {
3280
+ return x.modPowInt(this.e, this.n);
3281
+ }
3282
+
3283
+ return null;
3284
+ } //#endregion PROTECTED
3285
+ //#region PUBLIC
3286
+ // RSAKey.prototype.setPublic = RSASetPublic;
3287
+ // Set the public key fields N and e from hex strings
3288
+
3289
+
3290
+ setPublic(N, E) {
3291
+ if (N != null && E != null && N.length > 0 && E.length > 0) {
3292
+ this.n = parseBigInt(N, 16);
3293
+ this.e = parseInt(E, 16);
3294
+ } else {
3295
+ console.error('Invalid RSA public key');
3296
+ }
3297
+ } // RSAKey.prototype.encrypt = RSAEncrypt;
3298
+ // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
3299
+
3300
+
3301
+ encrypt(text) {
3302
+ if (!this.n) {
3303
+ return null;
3304
+ }
3305
+
3306
+ const maxLength = this.n.bitLength() + 7 >> 3;
3307
+ const m = pkcs1pad2(text, maxLength);
3308
+
3309
+ if (m == null) {
3310
+ return null;
3311
+ }
3312
+
3313
+ const c = this.doPublic(m);
3314
+
3315
+ if (c == null) {
3316
+ return null;
3317
+ }
3318
+
3319
+ let h = c.toString(16);
3320
+ const length = h.length; // fix zero before result
3321
+
3322
+ for (let i = 0; i < maxLength * 2 - length; i++) {
3323
+ h = '0' + h;
3324
+ }
3325
+
3326
+ return h;
3327
+ } // RSAKey.prototype.generate = RSAGenerate;
3328
+ // Generate a new random private key B bits long, using public expt E
3329
+
3330
+
3331
+ generate(B, E) {
3332
+ const rng = new SecureRandom();
3333
+ const qs = B >> 1;
3334
+ this.e = parseInt(E, 16);
3335
+ const ee = new BigInteger(E, 16);
3336
+
3337
+ for (;;) {
3338
+ for (;;) {
3339
+ this.p = new BigInteger(B - qs, 1, rng);
3340
+
3341
+ if (this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) {
3342
+ break;
3343
+ }
3344
+ }
3345
+
3346
+ for (;;) {
3347
+ this.q = new BigInteger(qs, 1, rng);
3348
+
3349
+ if (this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) {
3350
+ break;
3351
+ }
3352
+ }
3353
+
3354
+ if (this.p.compareTo(this.q) <= 0) {
3355
+ const t = this.p;
3356
+ this.p = this.q;
3357
+ this.q = t;
3358
+ }
3359
+
3360
+ const p1 = this.p.subtract(BigInteger.ONE);
3361
+ const q1 = this.q.subtract(BigInteger.ONE);
3362
+ const phi = p1.multiply(q1);
3363
+
3364
+ if (phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
3365
+ this.n = this.p.multiply(this.q);
3366
+ this.d = ee.modInverse(phi);
3367
+ this.dmp1 = this.d.mod(p1);
3368
+ this.dmq1 = this.d.mod(q1);
3369
+ this.coeff = this.q.modInverse(this.p);
3370
+ break;
3371
+ }
3372
+ }
3373
+ } // Generate a new random private key B bits long, using public expt E
3374
+
3375
+
3376
+ generateAsync(B, E, callback) {
3377
+ const rng = new SecureRandom();
3378
+ const qs = B >> 1;
3379
+ this.e = parseInt(E, 16);
3380
+ const ee = new BigInteger(E, 16); // These functions have non-descript names because they were originally for(;;) loops.
3381
+ // I don't know about cryptography to give them better names than loop1-4.
3382
+
3383
+ const loop1 = () => {
3384
+ const loop4 = () => {
3385
+ if (this.p.compareTo(this.q) <= 0) {
3386
+ const t = this.p;
3387
+ this.p = this.q;
3388
+ this.q = t;
3389
+ }
3390
+
3391
+ const p1 = this.p.subtract(BigInteger.ONE);
3392
+ const q1 = this.q.subtract(BigInteger.ONE);
3393
+ const phi = p1.multiply(q1);
3394
+
3395
+ if (phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
3396
+ this.n = this.p.multiply(this.q);
3397
+ this.d = ee.modInverse(phi);
3398
+ this.dmp1 = this.d.mod(p1);
3399
+ this.dmq1 = this.d.mod(q1);
3400
+ this.coeff = this.q.modInverse(this.p);
3401
+ setTimeout(function () {
3402
+ callback();
3403
+ }, 0); // escape
3404
+ } else {
3405
+ setTimeout(loop1, 0);
3406
+ }
3407
+ };
3408
+
3409
+ const loop3 = () => {
3410
+ this.q = nbi();
3411
+ this.q.fromNumberAsync(qs, 1, rng, () => {
3412
+ this.q.subtract(BigInteger.ONE).gcda(ee, r => {
3413
+ if (r.compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) {
3414
+ setTimeout(loop4, 0);
3415
+ } else {
3416
+ setTimeout(loop3, 0);
3417
+ }
3418
+ });
3419
+ });
3420
+ };
3421
+
3422
+ const loop2 = () => {
3423
+ this.p = nbi();
3424
+ this.p.fromNumberAsync(B - qs, 1, rng, () => {
3425
+ this.p.subtract(BigInteger.ONE).gcda(ee, r => {
3426
+ if (r.compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) {
3427
+ setTimeout(loop3, 0);
3428
+ } else {
3429
+ setTimeout(loop2, 0);
3430
+ }
3431
+ });
3432
+ });
3433
+ };
3434
+
3435
+ setTimeout(loop2, 0);
3436
+ };
3437
+
3438
+ setTimeout(loop1, 0);
3439
+ }
3440
+
3441
+ }
3442
+
3443
+ /**
3444
+ * Create a new JSEncryptRSAKey that extends Tom Wu's RSA key object.
3445
+ * This object is just a decorator for parsing the key parameter
3446
+ * @param {string|Object} key - The key in string format, or an object containing
3447
+ * the parameters needed to build a RSAKey object.
3448
+ * @constructor
3449
+ */
3450
+
3451
+ class JSEncryptRSAKey extends RSAKey {
3452
+ constructor(key) {
3453
+ super(); // Call the super constructor.
3454
+ // RSAKey.call(this);
3455
+ // If a key key was provided.
3456
+
3457
+ if (key) {
3458
+ // If this is a string...
3459
+ if (typeof key === 'string') {
3460
+ this.parseKey(key);
3461
+ } else if (JSEncryptRSAKey.hasPublicKeyProperty(key)) {
3462
+ // Set the values for the key.
3463
+ this.parsePropertiesFrom(key);
3464
+ }
3465
+ }
3466
+ }
3467
+ /**
3468
+ * Method to parse a pem encoded string containing both a public or private key.
3469
+ * The method will translate the pem encoded string in a der encoded string and
3470
+ * will parse private key and public key parameters. This method accepts public key
3471
+ * in the rsaencryption pkcs #1 format (oid: 1.2.840.113549.1.1.1).
3472
+ *
3473
+ * @todo Check how many rsa formats use the same format of pkcs #1.
3474
+ *
3475
+ * The format is defined as:
3476
+ * PublicKeyInfo ::= SEQUENCE {
3477
+ * algorithm AlgorithmIdentifier,
3478
+ * PublicKey BIT STRING
3479
+ * }
3480
+ * Where AlgorithmIdentifier is:
3481
+ * AlgorithmIdentifier ::= SEQUENCE {
3482
+ * algorithm OBJECT IDENTIFIER, the OID of the enc algorithm
3483
+ * parameters ANY DEFINED BY algorithm OPTIONAL (NULL for PKCS #1)
3484
+ * }
3485
+ * and PublicKey is a SEQUENCE encapsulated in a BIT STRING
3486
+ * RSAPublicKey ::= SEQUENCE {
3487
+ * modulus INTEGER, -- n
3488
+ * publicExponent INTEGER -- e
3489
+ * }
3490
+ * it's possible to examine the structure of the keys obtained from openssl using
3491
+ * an asn.1 dumper as the one used here to parse the components: http://lapo.it/asn1js/
3492
+ * @argument {string} pem the pem encoded string, can include the BEGIN/END header/footer
3493
+ * @private
3494
+ */
3495
+
3496
+
3497
+ parseKey(pem) {
3498
+ try {
3499
+ let modulus = 0;
3500
+ let public_exponent = 0;
3501
+ const reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/;
3502
+ const der = reHex.test(pem) ? Hex.decode(pem) : Base64.unarmor(pem);
3503
+ let asn1 = ASN1.decode(der); // Fixes a bug with OpenSSL 1.0+ private keys
3504
+
3505
+ if (asn1.sub && asn1.sub.length === 3) {
3506
+ if (asn1.sub[2].sub) {
3507
+ asn1 = asn1.sub[2].sub[0];
3508
+ }
3509
+ }
3510
+
3511
+ if (asn1.sub && asn1.sub.length === 9) {
3512
+ // Parse the private key.
3513
+ modulus = asn1.sub[1].getHexStringValue(); // bigint
3514
+
3515
+ this.n = parseBigInt(modulus, 16);
3516
+ public_exponent = asn1.sub[2].getHexStringValue(); // int
3517
+
3518
+ this.e = parseInt(public_exponent, 16);
3519
+ const private_exponent = asn1.sub[3].getHexStringValue(); // bigint
3520
+
3521
+ this.d = parseBigInt(private_exponent, 16);
3522
+ const prime1 = asn1.sub[4].getHexStringValue(); // bigint
3523
+
3524
+ this.p = parseBigInt(prime1, 16);
3525
+ const prime2 = asn1.sub[5].getHexStringValue(); // bigint
3526
+
3527
+ this.q = parseBigInt(prime2, 16);
3528
+ const exponent1 = asn1.sub[6].getHexStringValue(); // bigint
3529
+
3530
+ this.dmp1 = parseBigInt(exponent1, 16);
3531
+ const exponent2 = asn1.sub[7].getHexStringValue(); // bigint
3532
+
3533
+ this.dmq1 = parseBigInt(exponent2, 16);
3534
+ const coefficient = asn1.sub[8].getHexStringValue(); // bigint
3535
+
3536
+ this.coeff = parseBigInt(coefficient, 16);
3537
+ } else if (asn1.sub && asn1.sub.length === 2) {
3538
+ // Parse the public key.
3539
+ const bit_string = asn1.sub[1];
3540
+
3541
+ if (bit_string.sub) {
3542
+ const sequence = bit_string.sub[0];
3543
+
3544
+ if (sequence.sub) {
3545
+ modulus = sequence.sub[0].getHexStringValue();
3546
+ this.n = parseBigInt(modulus, 16);
3547
+ public_exponent = sequence.sub[1].getHexStringValue();
3548
+ this.e = parseInt(public_exponent, 16);
3549
+ } else {
3550
+ return false;
3551
+ }
3552
+ } else {
3553
+ return false;
3554
+ }
3555
+ } else {
3556
+ return false;
3557
+ }
3558
+
3559
+ return true;
3560
+ } catch (ex) {
3561
+ return false;
3562
+ }
3563
+ }
3564
+ /**
3565
+ * Check if the object contains the necessary parameters to populate the rsa modulus
3566
+ * and public exponent parameters.
3567
+ * @param {Object} [obj={}] - An object that may contain the two public key
3568
+ * parameters
3569
+ * @returns {boolean} true if the object contains both the modulus and the public exponent
3570
+ * properties (n and e)
3571
+ * @todo check for types of n and e. N should be a parseable bigInt object, E should
3572
+ * be a parseable integer number
3573
+ * @private
3574
+ */
3575
+
3576
+
3577
+ static hasPublicKeyProperty(obj) {
3578
+ obj = obj || {}; // eslint-disable-next-line no-prototype-builtins
3579
+
3580
+ return obj.hasOwnProperty('n') && obj.hasOwnProperty('e');
3581
+ }
3582
+ /**
3583
+ * Parse the properties of obj in the current rsa object. Obj should AT LEAST
3584
+ * include the modulus and public exponent (n, e) parameters.
3585
+ * @param {Object} obj - the object containing rsa parameters
3586
+ * @private
3587
+ */
3588
+
3589
+
3590
+ parsePropertiesFrom(obj) {
3591
+ this.n = obj.n;
3592
+ this.e = obj.e; // eslint-disable-next-line no-prototype-builtins
3593
+
3594
+ if (obj.hasOwnProperty('d')) {
3595
+ this.d = obj.d;
3596
+ this.p = obj.p;
3597
+ this.q = obj.q;
3598
+ this.dmp1 = obj.dmp1;
3599
+ this.dmq1 = obj.dmq1;
3600
+ this.coeff = obj.coeff;
3601
+ }
3602
+ }
3603
+
3604
+ }
3605
+
3606
+ /**
3607
+ *
3608
+ * @param {Object} [options = {}] - An object to customize JSEncrypt behaviour
3609
+ * possible parameters are:
3610
+ * - default_key_size {number} default: 1024 the key size in bit
3611
+ * - default_public_exponent {string} default: '010001' the hexadecimal representation of the public exponent
3612
+ * - log {boolean} default: false whether log warn/error or not
3613
+ * @constructor
3614
+ */
3615
+
3616
+ class JSEncrypt {
3617
+ constructor(options = {}) {
3618
+ options = options || {};
3619
+ this.default_key_size = options.default_key_size ? parseInt(options.default_key_size, 10) : 1024;
3620
+ this.default_public_exponent = options.default_public_exponent || '010001'; // 65537 default openssl public exponent for rsa key type
3621
+
3622
+ this.log = options.log || false;
3623
+ }
3624
+ /**
3625
+ * Method to set the rsa key parameter (one method is enough to set both the public
3626
+ * and the private key, since the private key contains the public key paramenters)
3627
+ * Log a warning if logs are enabled
3628
+ * @param {Object|string} key the pem encoded string or an object (with or without header/footer)
3629
+ * @public
3630
+ */
3631
+
3632
+
3633
+ setKey(key) {
3634
+ if (this.log && this.key) {
3635
+ console.warn('A key was already set, overriding existing.');
3636
+ }
3637
+
3638
+ this.key = new JSEncryptRSAKey(key);
3639
+ }
3640
+ /**
3641
+ * Proxy method for setKey, for api compatibility
3642
+ * @see setKey
3643
+ * @public
3644
+ */
3645
+
3646
+
3647
+ setPublicKey(pubkey) {
3648
+ // Sets the public key.
3649
+ this.setKey(pubkey);
3650
+ }
3651
+ /**
3652
+ * Proxy method for RSAKey object's encrypt, encrypt the string using the public
3653
+ * components of the rsa key object. Note that if the object was not set will be created
3654
+ * on the fly (by the getKey method) using the parameters passed in the JSEncrypt constructor
3655
+ * @param {string} str the string to encrypt
3656
+ * @return {string} the encrypted string encoded in base64
3657
+ * @public
3658
+ */
3659
+
3660
+
3661
+ encrypt(str) {
3662
+ // Return the encrypted string.
3663
+ try {
3664
+ const text = this.getKey().encrypt(str);
3665
+
3666
+ if (typeof text === 'string') {
3667
+ return hex2b64(text);
3668
+ }
3669
+ } catch (ex) {//
3670
+ }
3671
+
3672
+ return '';
3673
+ }
3674
+ /**
3675
+ * Getter for the current JSEncryptRSAKey object. If it doesn't exists a new object
3676
+ * will be created and returned
3677
+ * @param {callback} [cb] the callback to be called if we want the key to be generated
3678
+ * in an async fashion
3679
+ * @returns {JSEncryptRSAKey} the JSEncryptRSAKey object
3680
+ * @public
3681
+ */
3682
+
3683
+
3684
+ getKey(cb) {
3685
+ // Only create new if it does not exist.
3686
+ if (!this.key) {
3687
+ // Get a new private key.
3688
+ this.key = new JSEncryptRSAKey();
3689
+
3690
+ if (cb && {}.toString.call(cb) === '[object Function]') {
3691
+ this.key.generateAsync(this.default_key_size, this.default_public_exponent, cb);
3692
+ return this.key;
3693
+ } // Generate the key.
3694
+
3695
+
3696
+ this.key.generate(this.default_key_size, this.default_public_exponent);
3697
+ }
3698
+
3699
+ return this.key;
3700
+ }
3701
+
3702
+ }
3703
+
3704
+ export { JSEncrypt, JSEncrypt as default };