wk-ie-ai-customer 1.0.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.
@@ -0,0 +1,3584 @@
1
+ function _arrayLikeToArray(r, a) {
2
+ (null == a || a > r.length) && (a = r.length);
3
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
4
+ return n;
5
+ }
6
+ function _arrayWithoutHoles(r) {
7
+ if (Array.isArray(r)) return _arrayLikeToArray(r);
8
+ }
9
+ function _assertThisInitialized(e) {
10
+ if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
11
+ return e;
12
+ }
13
+ function _callSuper(t, o, e) {
14
+ return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, [], _getPrototypeOf(t).constructor) : o.apply(t, e));
15
+ }
16
+ function _classCallCheck(a, n) {
17
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
18
+ }
19
+ function _defineProperties(e, r) {
20
+ for (var t = 0; t < r.length; t++) {
21
+ var o = r[t];
22
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
23
+ }
24
+ }
25
+ function _createClass(e, r, t) {
26
+ return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
27
+ writable: false
28
+ }), e;
29
+ }
30
+ function _getPrototypeOf(t) {
31
+ return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
32
+ return t.__proto__ || Object.getPrototypeOf(t);
33
+ }, _getPrototypeOf(t);
34
+ }
35
+ function _inherits(t, e) {
36
+ if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
37
+ t.prototype = Object.create(e && e.prototype, {
38
+ constructor: {
39
+ value: t,
40
+ writable: true,
41
+ configurable: true
42
+ }
43
+ }), Object.defineProperty(t, "prototype", {
44
+ writable: false
45
+ }), e && _setPrototypeOf(t, e);
46
+ }
47
+ function _isNativeReflectConstruct() {
48
+ try {
49
+ var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
50
+ } catch (t) {}
51
+ return (_isNativeReflectConstruct = function () {
52
+ return !!t;
53
+ })();
54
+ }
55
+ function _iterableToArray(r) {
56
+ if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
57
+ }
58
+ function _nonIterableSpread() {
59
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
60
+ }
61
+ function _possibleConstructorReturn(t, e) {
62
+ if (e && ("object" == typeof e || "function" == typeof e)) return e;
63
+ if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
64
+ return _assertThisInitialized(t);
65
+ }
66
+ function _setPrototypeOf(t, e) {
67
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
68
+ return t.__proto__ = e, t;
69
+ }, _setPrototypeOf(t, e);
70
+ }
71
+ function _toConsumableArray(r) {
72
+ return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
73
+ }
74
+ function _toPrimitive(t, r) {
75
+ if ("object" != typeof t || !t) return t;
76
+ var e = t[Symbol.toPrimitive];
77
+ if (void 0 !== e) {
78
+ var i = e.call(t, r);
79
+ if ("object" != typeof i) return i;
80
+ throw new TypeError("@@toPrimitive must return a primitive value.");
81
+ }
82
+ return (String )(t);
83
+ }
84
+ function _toPropertyKey(t) {
85
+ var i = _toPrimitive(t, "string");
86
+ return "symbol" == typeof i ? i : i + "";
87
+ }
88
+ function _unsupportedIterableToArray(r, a) {
89
+ if (r) {
90
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
91
+ var t = {}.toString.call(r).slice(8, -1);
92
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
93
+ }
94
+ }
95
+
96
+ var jsbn$1 = {exports: {}};
97
+
98
+ var jsbn = jsbn$1.exports;
99
+
100
+ var hasRequiredJsbn;
101
+
102
+ function requireJsbn () {
103
+ if (hasRequiredJsbn) return jsbn$1.exports;
104
+ hasRequiredJsbn = 1;
105
+ (function (module, exports) {
106
+ (function(){
107
+
108
+ // Copyright (c) 2005 Tom Wu
109
+ // All Rights Reserved.
110
+ // See "LICENSE" for details.
111
+
112
+ // Basic JavaScript BN library - subset useful for RSA encryption.
113
+
114
+ // Bits per digit
115
+ var dbits;
116
+
117
+ // JavaScript engine analysis
118
+ var canary = 0xdeadbeefcafe;
119
+ var j_lm = ((canary&0xffffff)==0xefcafe);
120
+
121
+ // (public) Constructor
122
+ function BigInteger(a,b,c) {
123
+ if(a != null)
124
+ if("number" == typeof a) this.fromNumber(a,b,c);
125
+ else if(b == null && "string" != typeof a) this.fromString(a,256);
126
+ else this.fromString(a,b);
127
+ }
128
+
129
+ // return new, unset BigInteger
130
+ function nbi() { return new BigInteger(null); }
131
+
132
+ // am: Compute w_j += (x*this_i), propagate carries,
133
+ // c is initial carry, returns final carry.
134
+ // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
135
+ // We need to select the fastest one that works in this environment.
136
+
137
+ // am1: use a single mult and divide to get the high bits,
138
+ // max digit bits should be 26 because
139
+ // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
140
+ function am1(i,x,w,j,c,n) {
141
+ while(--n >= 0) {
142
+ var v = x*this[i++]+w[j]+c;
143
+ c = Math.floor(v/0x4000000);
144
+ w[j++] = v&0x3ffffff;
145
+ }
146
+ return c;
147
+ }
148
+ // am2 avoids a big mult-and-extract completely.
149
+ // Max digit bits should be <= 30 because we do bitwise ops
150
+ // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
151
+ function am2(i,x,w,j,c,n) {
152
+ var xl = x&0x7fff, xh = x>>15;
153
+ while(--n >= 0) {
154
+ var l = this[i]&0x7fff;
155
+ var h = this[i++]>>15;
156
+ var m = xh*l+h*xl;
157
+ l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
158
+ c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
159
+ w[j++] = l&0x3fffffff;
160
+ }
161
+ return c;
162
+ }
163
+ // Alternately, set max digit bits to 28 since some
164
+ // browsers slow down when dealing with 32-bit numbers.
165
+ function am3(i,x,w,j,c,n) {
166
+ var xl = x&0x3fff, xh = x>>14;
167
+ while(--n >= 0) {
168
+ var l = this[i]&0x3fff;
169
+ var h = this[i++]>>14;
170
+ var m = xh*l+h*xl;
171
+ l = xl*l+((m&0x3fff)<<14)+w[j]+c;
172
+ c = (l>>28)+(m>>14)+xh*h;
173
+ w[j++] = l&0xfffffff;
174
+ }
175
+ return c;
176
+ }
177
+ var inBrowser = typeof navigator !== "undefined";
178
+ if(inBrowser && j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
179
+ BigInteger.prototype.am = am2;
180
+ dbits = 30;
181
+ }
182
+ else if(inBrowser && j_lm && (navigator.appName != "Netscape")) {
183
+ BigInteger.prototype.am = am1;
184
+ dbits = 26;
185
+ }
186
+ else { // Mozilla/Netscape seems to prefer am3
187
+ BigInteger.prototype.am = am3;
188
+ dbits = 28;
189
+ }
190
+
191
+ BigInteger.prototype.DB = dbits;
192
+ BigInteger.prototype.DM = ((1<<dbits)-1);
193
+ BigInteger.prototype.DV = (1<<dbits);
194
+
195
+ var BI_FP = 52;
196
+ BigInteger.prototype.FV = Math.pow(2,BI_FP);
197
+ BigInteger.prototype.F1 = BI_FP-dbits;
198
+ BigInteger.prototype.F2 = 2*dbits-BI_FP;
199
+
200
+ // Digit conversions
201
+ var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
202
+ var BI_RC = new Array();
203
+ var rr,vv;
204
+ rr = "0".charCodeAt(0);
205
+ for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
206
+ rr = "a".charCodeAt(0);
207
+ for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
208
+ rr = "A".charCodeAt(0);
209
+ for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
210
+
211
+ function int2char(n) { return BI_RM.charAt(n); }
212
+ function intAt(s,i) {
213
+ var c = BI_RC[s.charCodeAt(i)];
214
+ return (c==null)?-1:c;
215
+ }
216
+
217
+ // (protected) copy this to r
218
+ function bnpCopyTo(r) {
219
+ for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
220
+ r.t = this.t;
221
+ r.s = this.s;
222
+ }
223
+
224
+ // (protected) set from integer value x, -DV <= x < DV
225
+ function bnpFromInt(x) {
226
+ this.t = 1;
227
+ this.s = (x<0)?-1:0;
228
+ if(x > 0) this[0] = x;
229
+ else if(x < -1) this[0] = x+this.DV;
230
+ else this.t = 0;
231
+ }
232
+
233
+ // return bigint initialized to value
234
+ function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
235
+
236
+ // (protected) set from string and radix
237
+ function bnpFromString(s,b) {
238
+ var k;
239
+ if(b == 16) k = 4;
240
+ else if(b == 8) k = 3;
241
+ else if(b == 256) k = 8; // byte array
242
+ else if(b == 2) k = 1;
243
+ else if(b == 32) k = 5;
244
+ else if(b == 4) k = 2;
245
+ else { this.fromRadix(s,b); return; }
246
+ this.t = 0;
247
+ this.s = 0;
248
+ var i = s.length, mi = false, sh = 0;
249
+ while(--i >= 0) {
250
+ var x = (k==8)?s[i]&0xff:intAt(s,i);
251
+ if(x < 0) {
252
+ if(s.charAt(i) == "-") mi = true;
253
+ continue;
254
+ }
255
+ mi = false;
256
+ if(sh == 0)
257
+ this[this.t++] = x;
258
+ else if(sh+k > this.DB) {
259
+ this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
260
+ this[this.t++] = (x>>(this.DB-sh));
261
+ }
262
+ else
263
+ this[this.t-1] |= x<<sh;
264
+ sh += k;
265
+ if(sh >= this.DB) sh -= this.DB;
266
+ }
267
+ if(k == 8 && (s[0]&0x80) != 0) {
268
+ this.s = -1;
269
+ if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
270
+ }
271
+ this.clamp();
272
+ if(mi) BigInteger.ZERO.subTo(this,this);
273
+ }
274
+
275
+ // (protected) clamp off excess high words
276
+ function bnpClamp() {
277
+ var c = this.s&this.DM;
278
+ while(this.t > 0 && this[this.t-1] == c) --this.t;
279
+ }
280
+
281
+ // (public) return string representation in given radix
282
+ function bnToString(b) {
283
+ if(this.s < 0) return "-"+this.negate().toString(b);
284
+ var k;
285
+ if(b == 16) k = 4;
286
+ else if(b == 8) k = 3;
287
+ else if(b == 2) k = 1;
288
+ else if(b == 32) k = 5;
289
+ else if(b == 4) k = 2;
290
+ else return this.toRadix(b);
291
+ var km = (1<<k)-1, d, m = false, r = "", i = this.t;
292
+ var p = this.DB-(i*this.DB)%k;
293
+ if(i-- > 0) {
294
+ if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
295
+ while(i >= 0) {
296
+ if(p < k) {
297
+ d = (this[i]&((1<<p)-1))<<(k-p);
298
+ d |= this[--i]>>(p+=this.DB-k);
299
+ }
300
+ else {
301
+ d = (this[i]>>(p-=k))&km;
302
+ if(p <= 0) { p += this.DB; --i; }
303
+ }
304
+ if(d > 0) m = true;
305
+ if(m) r += int2char(d);
306
+ }
307
+ }
308
+ return m?r:"0";
309
+ }
310
+
311
+ // (public) -this
312
+ function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
313
+
314
+ // (public) |this|
315
+ function bnAbs() { return (this.s<0)?this.negate():this; }
316
+
317
+ // (public) return + if this > a, - if this < a, 0 if equal
318
+ function bnCompareTo(a) {
319
+ var r = this.s-a.s;
320
+ if(r != 0) return r;
321
+ var i = this.t;
322
+ r = i-a.t;
323
+ if(r != 0) return (this.s<0)?-r:r;
324
+ while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
325
+ return 0;
326
+ }
327
+
328
+ // returns bit length of the integer x
329
+ function nbits(x) {
330
+ var r = 1, t;
331
+ if((t=x>>>16) != 0) { x = t; r += 16; }
332
+ if((t=x>>8) != 0) { x = t; r += 8; }
333
+ if((t=x>>4) != 0) { x = t; r += 4; }
334
+ if((t=x>>2) != 0) { x = t; r += 2; }
335
+ if((t=x>>1) != 0) { x = t; r += 1; }
336
+ return r;
337
+ }
338
+
339
+ // (public) return the number of bits in "this"
340
+ function bnBitLength() {
341
+ if(this.t <= 0) return 0;
342
+ return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
343
+ }
344
+
345
+ // (protected) r = this << n*DB
346
+ function bnpDLShiftTo(n,r) {
347
+ var i;
348
+ for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
349
+ for(i = n-1; i >= 0; --i) r[i] = 0;
350
+ r.t = this.t+n;
351
+ r.s = this.s;
352
+ }
353
+
354
+ // (protected) r = this >> n*DB
355
+ function bnpDRShiftTo(n,r) {
356
+ for(var i = n; i < this.t; ++i) r[i-n] = this[i];
357
+ r.t = Math.max(this.t-n,0);
358
+ r.s = this.s;
359
+ }
360
+
361
+ // (protected) r = this << n
362
+ function bnpLShiftTo(n,r) {
363
+ var bs = n%this.DB;
364
+ var cbs = this.DB-bs;
365
+ var bm = (1<<cbs)-1;
366
+ var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
367
+ for(i = this.t-1; i >= 0; --i) {
368
+ r[i+ds+1] = (this[i]>>cbs)|c;
369
+ c = (this[i]&bm)<<bs;
370
+ }
371
+ for(i = ds-1; i >= 0; --i) r[i] = 0;
372
+ r[ds] = c;
373
+ r.t = this.t+ds+1;
374
+ r.s = this.s;
375
+ r.clamp();
376
+ }
377
+
378
+ // (protected) r = this >> n
379
+ function bnpRShiftTo(n,r) {
380
+ r.s = this.s;
381
+ var ds = Math.floor(n/this.DB);
382
+ if(ds >= this.t) { r.t = 0; return; }
383
+ var bs = n%this.DB;
384
+ var cbs = this.DB-bs;
385
+ var bm = (1<<bs)-1;
386
+ r[0] = this[ds]>>bs;
387
+ for(var i = ds+1; i < this.t; ++i) {
388
+ r[i-ds-1] |= (this[i]&bm)<<cbs;
389
+ r[i-ds] = this[i]>>bs;
390
+ }
391
+ if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
392
+ r.t = this.t-ds;
393
+ r.clamp();
394
+ }
395
+
396
+ // (protected) r = this - a
397
+ function bnpSubTo(a,r) {
398
+ var i = 0, c = 0, m = Math.min(a.t,this.t);
399
+ while(i < m) {
400
+ c += this[i]-a[i];
401
+ r[i++] = c&this.DM;
402
+ c >>= this.DB;
403
+ }
404
+ if(a.t < this.t) {
405
+ c -= a.s;
406
+ while(i < this.t) {
407
+ c += this[i];
408
+ r[i++] = c&this.DM;
409
+ c >>= this.DB;
410
+ }
411
+ c += this.s;
412
+ }
413
+ else {
414
+ c += this.s;
415
+ while(i < a.t) {
416
+ c -= a[i];
417
+ r[i++] = c&this.DM;
418
+ c >>= this.DB;
419
+ }
420
+ c -= a.s;
421
+ }
422
+ r.s = (c<0)?-1:0;
423
+ if(c < -1) r[i++] = this.DV+c;
424
+ else if(c > 0) r[i++] = c;
425
+ r.t = i;
426
+ r.clamp();
427
+ }
428
+
429
+ // (protected) r = this * a, r != this,a (HAC 14.12)
430
+ // "this" should be the larger one if appropriate.
431
+ function bnpMultiplyTo(a,r) {
432
+ var x = this.abs(), y = a.abs();
433
+ var i = x.t;
434
+ r.t = i+y.t;
435
+ while(--i >= 0) r[i] = 0;
436
+ for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
437
+ r.s = 0;
438
+ r.clamp();
439
+ if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
440
+ }
441
+
442
+ // (protected) r = this^2, r != this (HAC 14.16)
443
+ function bnpSquareTo(r) {
444
+ var x = this.abs();
445
+ var i = r.t = 2*x.t;
446
+ while(--i >= 0) r[i] = 0;
447
+ for(i = 0; i < x.t-1; ++i) {
448
+ var c = x.am(i,x[i],r,2*i,0,1);
449
+ if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
450
+ r[i+x.t] -= x.DV;
451
+ r[i+x.t+1] = 1;
452
+ }
453
+ }
454
+ if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
455
+ r.s = 0;
456
+ r.clamp();
457
+ }
458
+
459
+ // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
460
+ // r != q, this != m. q or r may be null.
461
+ function bnpDivRemTo(m,q,r) {
462
+ var pm = m.abs();
463
+ if(pm.t <= 0) return;
464
+ var pt = this.abs();
465
+ if(pt.t < pm.t) {
466
+ if(q != null) q.fromInt(0);
467
+ if(r != null) this.copyTo(r);
468
+ return;
469
+ }
470
+ if(r == null) r = nbi();
471
+ var y = nbi(), ts = this.s, ms = m.s;
472
+ var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus
473
+ if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
474
+ else { pm.copyTo(y); pt.copyTo(r); }
475
+ var ys = y.t;
476
+ var y0 = y[ys-1];
477
+ if(y0 == 0) return;
478
+ var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
479
+ var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
480
+ var i = r.t, j = i-ys, t = (q==null)?nbi():q;
481
+ y.dlShiftTo(j,t);
482
+ if(r.compareTo(t) >= 0) {
483
+ r[r.t++] = 1;
484
+ r.subTo(t,r);
485
+ }
486
+ BigInteger.ONE.dlShiftTo(ys,t);
487
+ t.subTo(y,y); // "negative" y so we can replace sub with am later
488
+ while(y.t < ys) y[y.t++] = 0;
489
+ while(--j >= 0) {
490
+ // Estimate quotient digit
491
+ var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
492
+ if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
493
+ y.dlShiftTo(j,t);
494
+ r.subTo(t,r);
495
+ while(r[i] < --qd) r.subTo(t,r);
496
+ }
497
+ }
498
+ if(q != null) {
499
+ r.drShiftTo(ys,q);
500
+ if(ts != ms) BigInteger.ZERO.subTo(q,q);
501
+ }
502
+ r.t = ys;
503
+ r.clamp();
504
+ if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
505
+ if(ts < 0) BigInteger.ZERO.subTo(r,r);
506
+ }
507
+
508
+ // (public) this mod a
509
+ function bnMod(a) {
510
+ var r = nbi();
511
+ this.abs().divRemTo(a,null,r);
512
+ if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
513
+ return r;
514
+ }
515
+
516
+ // Modular reduction using "classic" algorithm
517
+ function Classic(m) { this.m = m; }
518
+ function cConvert(x) {
519
+ if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
520
+ else return x;
521
+ }
522
+ function cRevert(x) { return x; }
523
+ function cReduce(x) { x.divRemTo(this.m,null,x); }
524
+ function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
525
+ function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
526
+
527
+ Classic.prototype.convert = cConvert;
528
+ Classic.prototype.revert = cRevert;
529
+ Classic.prototype.reduce = cReduce;
530
+ Classic.prototype.mulTo = cMulTo;
531
+ Classic.prototype.sqrTo = cSqrTo;
532
+
533
+ // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
534
+ // justification:
535
+ // xy == 1 (mod m)
536
+ // xy = 1+km
537
+ // xy(2-xy) = (1+km)(1-km)
538
+ // x[y(2-xy)] = 1-k^2m^2
539
+ // x[y(2-xy)] == 1 (mod m^2)
540
+ // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
541
+ // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
542
+ // JS multiply "overflows" differently from C/C++, so care is needed here.
543
+ function bnpInvDigit() {
544
+ if(this.t < 1) return 0;
545
+ var x = this[0];
546
+ if((x&1) == 0) return 0;
547
+ var y = x&3; // y == 1/x mod 2^2
548
+ y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
549
+ y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
550
+ y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
551
+ // last step - calculate inverse mod DV directly;
552
+ // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
553
+ y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
554
+ // we really want the negative inverse, and -DV < y < DV
555
+ return (y>0)?this.DV-y:-y;
556
+ }
557
+
558
+ // Montgomery reduction
559
+ function Montgomery(m) {
560
+ this.m = m;
561
+ this.mp = m.invDigit();
562
+ this.mpl = this.mp&0x7fff;
563
+ this.mph = this.mp>>15;
564
+ this.um = (1<<(m.DB-15))-1;
565
+ this.mt2 = 2*m.t;
566
+ }
567
+
568
+ // xR mod m
569
+ function montConvert(x) {
570
+ var r = nbi();
571
+ x.abs().dlShiftTo(this.m.t,r);
572
+ r.divRemTo(this.m,null,r);
573
+ if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
574
+ return r;
575
+ }
576
+
577
+ // x/R mod m
578
+ function montRevert(x) {
579
+ var r = nbi();
580
+ x.copyTo(r);
581
+ this.reduce(r);
582
+ return r;
583
+ }
584
+
585
+ // x = x/R mod m (HAC 14.32)
586
+ function montReduce(x) {
587
+ while(x.t <= this.mt2) // pad x so am has enough room later
588
+ x[x.t++] = 0;
589
+ for(var i = 0; i < this.m.t; ++i) {
590
+ // faster way of calculating u0 = x[i]*mp mod DV
591
+ var j = x[i]&0x7fff;
592
+ var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
593
+ // use am to combine the multiply-shift-add into one call
594
+ j = i+this.m.t;
595
+ x[j] += this.m.am(0,u0,x,i,0,this.m.t);
596
+ // propagate carry
597
+ while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
598
+ }
599
+ x.clamp();
600
+ x.drShiftTo(this.m.t,x);
601
+ if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
602
+ }
603
+
604
+ // r = "x^2/R mod m"; x != r
605
+ function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
606
+
607
+ // r = "xy/R mod m"; x,y != r
608
+ function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
609
+
610
+ Montgomery.prototype.convert = montConvert;
611
+ Montgomery.prototype.revert = montRevert;
612
+ Montgomery.prototype.reduce = montReduce;
613
+ Montgomery.prototype.mulTo = montMulTo;
614
+ Montgomery.prototype.sqrTo = montSqrTo;
615
+
616
+ // (protected) true iff this is even
617
+ function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
618
+
619
+ // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
620
+ function bnpExp(e,z) {
621
+ if(e > 0xffffffff || e < 1) return BigInteger.ONE;
622
+ var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
623
+ g.copyTo(r);
624
+ while(--i >= 0) {
625
+ z.sqrTo(r,r2);
626
+ if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
627
+ else { var t = r; r = r2; r2 = t; }
628
+ }
629
+ return z.revert(r);
630
+ }
631
+
632
+ // (public) this^e % m, 0 <= e < 2^32
633
+ function bnModPowInt(e,m) {
634
+ var z;
635
+ if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
636
+ return this.exp(e,z);
637
+ }
638
+
639
+ // protected
640
+ BigInteger.prototype.copyTo = bnpCopyTo;
641
+ BigInteger.prototype.fromInt = bnpFromInt;
642
+ BigInteger.prototype.fromString = bnpFromString;
643
+ BigInteger.prototype.clamp = bnpClamp;
644
+ BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
645
+ BigInteger.prototype.drShiftTo = bnpDRShiftTo;
646
+ BigInteger.prototype.lShiftTo = bnpLShiftTo;
647
+ BigInteger.prototype.rShiftTo = bnpRShiftTo;
648
+ BigInteger.prototype.subTo = bnpSubTo;
649
+ BigInteger.prototype.multiplyTo = bnpMultiplyTo;
650
+ BigInteger.prototype.squareTo = bnpSquareTo;
651
+ BigInteger.prototype.divRemTo = bnpDivRemTo;
652
+ BigInteger.prototype.invDigit = bnpInvDigit;
653
+ BigInteger.prototype.isEven = bnpIsEven;
654
+ BigInteger.prototype.exp = bnpExp;
655
+
656
+ // public
657
+ BigInteger.prototype.toString = bnToString;
658
+ BigInteger.prototype.negate = bnNegate;
659
+ BigInteger.prototype.abs = bnAbs;
660
+ BigInteger.prototype.compareTo = bnCompareTo;
661
+ BigInteger.prototype.bitLength = bnBitLength;
662
+ BigInteger.prototype.mod = bnMod;
663
+ BigInteger.prototype.modPowInt = bnModPowInt;
664
+
665
+ // "constants"
666
+ BigInteger.ZERO = nbv(0);
667
+ BigInteger.ONE = nbv(1);
668
+
669
+ // Copyright (c) 2005-2009 Tom Wu
670
+ // All Rights Reserved.
671
+ // See "LICENSE" for details.
672
+
673
+ // Extended JavaScript BN functions, required for RSA private ops.
674
+
675
+ // Version 1.1: new BigInteger("0", 10) returns "proper" zero
676
+ // Version 1.2: square() API, isProbablePrime fix
677
+
678
+ // (public)
679
+ function bnClone() { var r = nbi(); this.copyTo(r); return r; }
680
+
681
+ // (public) return value as integer
682
+ function bnIntValue() {
683
+ if(this.s < 0) {
684
+ if(this.t == 1) return this[0]-this.DV;
685
+ else if(this.t == 0) return -1;
686
+ }
687
+ else if(this.t == 1) return this[0];
688
+ else if(this.t == 0) return 0;
689
+ // assumes 16 < DB < 32
690
+ return ((this[1]&((1<<(32-this.DB))-1))<<this.DB)|this[0];
691
+ }
692
+
693
+ // (public) return value as byte
694
+ function bnByteValue() { return (this.t==0)?this.s:(this[0]<<24)>>24; }
695
+
696
+ // (public) return value as short (assumes DB>=16)
697
+ function bnShortValue() { return (this.t==0)?this.s:(this[0]<<16)>>16; }
698
+
699
+ // (protected) return x s.t. r^x < DV
700
+ function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
701
+
702
+ // (public) 0 if this == 0, 1 if this > 0
703
+ function bnSigNum() {
704
+ if(this.s < 0) return -1;
705
+ else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
706
+ else return 1;
707
+ }
708
+
709
+ // (protected) convert to radix string
710
+ function bnpToRadix(b) {
711
+ if(b == null) b = 10;
712
+ if(this.signum() == 0 || b < 2 || b > 36) return "0";
713
+ var cs = this.chunkSize(b);
714
+ var a = Math.pow(b,cs);
715
+ var d = nbv(a), y = nbi(), z = nbi(), r = "";
716
+ this.divRemTo(d,y,z);
717
+ while(y.signum() > 0) {
718
+ r = (a+z.intValue()).toString(b).substr(1) + r;
719
+ y.divRemTo(d,y,z);
720
+ }
721
+ return z.intValue().toString(b) + r;
722
+ }
723
+
724
+ // (protected) convert from radix string
725
+ function bnpFromRadix(s,b) {
726
+ this.fromInt(0);
727
+ if(b == null) b = 10;
728
+ var cs = this.chunkSize(b);
729
+ var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
730
+ for(var i = 0; i < s.length; ++i) {
731
+ var x = intAt(s,i);
732
+ if(x < 0) {
733
+ if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
734
+ continue;
735
+ }
736
+ w = b*w+x;
737
+ if(++j >= cs) {
738
+ this.dMultiply(d);
739
+ this.dAddOffset(w,0);
740
+ j = 0;
741
+ w = 0;
742
+ }
743
+ }
744
+ if(j > 0) {
745
+ this.dMultiply(Math.pow(b,j));
746
+ this.dAddOffset(w,0);
747
+ }
748
+ if(mi) BigInteger.ZERO.subTo(this,this);
749
+ }
750
+
751
+ // (protected) alternate constructor
752
+ function bnpFromNumber(a,b,c) {
753
+ if("number" == typeof b) {
754
+ // new BigInteger(int,int,RNG)
755
+ if(a < 2) this.fromInt(1);
756
+ else {
757
+ this.fromNumber(a,c);
758
+ if(!this.testBit(a-1)) // force MSB set
759
+ this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
760
+ if(this.isEven()) this.dAddOffset(1,0); // force odd
761
+ while(!this.isProbablePrime(b)) {
762
+ this.dAddOffset(2,0);
763
+ if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
764
+ }
765
+ }
766
+ }
767
+ else {
768
+ // new BigInteger(int,RNG)
769
+ var x = new Array(), t = a&7;
770
+ x.length = (a>>3)+1;
771
+ b.nextBytes(x);
772
+ if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
773
+ this.fromString(x,256);
774
+ }
775
+ }
776
+
777
+ // (public) convert to bigendian byte array
778
+ function bnToByteArray() {
779
+ var i = this.t, r = new Array();
780
+ r[0] = this.s;
781
+ var p = this.DB-(i*this.DB)%8, d, k = 0;
782
+ if(i-- > 0) {
783
+ if(p < this.DB && (d = this[i]>>p) != (this.s&this.DM)>>p)
784
+ r[k++] = d|(this.s<<(this.DB-p));
785
+ while(i >= 0) {
786
+ if(p < 8) {
787
+ d = (this[i]&((1<<p)-1))<<(8-p);
788
+ d |= this[--i]>>(p+=this.DB-8);
789
+ }
790
+ else {
791
+ d = (this[i]>>(p-=8))&0xff;
792
+ if(p <= 0) { p += this.DB; --i; }
793
+ }
794
+ if((d&0x80) != 0) d |= -256;
795
+ if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
796
+ if(k > 0 || d != this.s) r[k++] = d;
797
+ }
798
+ }
799
+ return r;
800
+ }
801
+
802
+ function bnEquals(a) { return(this.compareTo(a)==0); }
803
+ function bnMin(a) { return (this.compareTo(a)<0)?this:a; }
804
+ function bnMax(a) { return (this.compareTo(a)>0)?this:a; }
805
+
806
+ // (protected) r = this op a (bitwise)
807
+ function bnpBitwiseTo(a,op,r) {
808
+ var i, f, m = Math.min(a.t,this.t);
809
+ for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]);
810
+ if(a.t < this.t) {
811
+ f = a.s&this.DM;
812
+ for(i = m; i < this.t; ++i) r[i] = op(this[i],f);
813
+ r.t = this.t;
814
+ }
815
+ else {
816
+ f = this.s&this.DM;
817
+ for(i = m; i < a.t; ++i) r[i] = op(f,a[i]);
818
+ r.t = a.t;
819
+ }
820
+ r.s = op(this.s,a.s);
821
+ r.clamp();
822
+ }
823
+
824
+ // (public) this & a
825
+ function op_and(x,y) { return x&y; }
826
+ function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
827
+
828
+ // (public) this | a
829
+ function op_or(x,y) { return x|y; }
830
+ function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
831
+
832
+ // (public) this ^ a
833
+ function op_xor(x,y) { return x^y; }
834
+ function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
835
+
836
+ // (public) this & ~a
837
+ function op_andnot(x,y) { return x&~y; }
838
+ function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
839
+
840
+ // (public) ~this
841
+ function bnNot() {
842
+ var r = nbi();
843
+ for(var i = 0; i < this.t; ++i) r[i] = this.DM&~this[i];
844
+ r.t = this.t;
845
+ r.s = ~this.s;
846
+ return r;
847
+ }
848
+
849
+ // (public) this << n
850
+ function bnShiftLeft(n) {
851
+ var r = nbi();
852
+ if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
853
+ return r;
854
+ }
855
+
856
+ // (public) this >> n
857
+ function bnShiftRight(n) {
858
+ var r = nbi();
859
+ if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
860
+ return r;
861
+ }
862
+
863
+ // return index of lowest 1-bit in x, x < 2^31
864
+ function lbit(x) {
865
+ if(x == 0) return -1;
866
+ var r = 0;
867
+ if((x&0xffff) == 0) { x >>= 16; r += 16; }
868
+ if((x&0xff) == 0) { x >>= 8; r += 8; }
869
+ if((x&0xf) == 0) { x >>= 4; r += 4; }
870
+ if((x&3) == 0) { x >>= 2; r += 2; }
871
+ if((x&1) == 0) ++r;
872
+ return r;
873
+ }
874
+
875
+ // (public) returns index of lowest 1-bit (or -1 if none)
876
+ function bnGetLowestSetBit() {
877
+ for(var i = 0; i < this.t; ++i)
878
+ if(this[i] != 0) return i*this.DB+lbit(this[i]);
879
+ if(this.s < 0) return this.t*this.DB;
880
+ return -1;
881
+ }
882
+
883
+ // return number of 1 bits in x
884
+ function cbit(x) {
885
+ var r = 0;
886
+ while(x != 0) { x &= x-1; ++r; }
887
+ return r;
888
+ }
889
+
890
+ // (public) return number of set bits
891
+ function bnBitCount() {
892
+ var r = 0, x = this.s&this.DM;
893
+ for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x);
894
+ return r;
895
+ }
896
+
897
+ // (public) true iff nth bit is set
898
+ function bnTestBit(n) {
899
+ var j = Math.floor(n/this.DB);
900
+ if(j >= this.t) return(this.s!=0);
901
+ return((this[j]&(1<<(n%this.DB)))!=0);
902
+ }
903
+
904
+ // (protected) this op (1<<n)
905
+ function bnpChangeBit(n,op) {
906
+ var r = BigInteger.ONE.shiftLeft(n);
907
+ this.bitwiseTo(r,op,r);
908
+ return r;
909
+ }
910
+
911
+ // (public) this | (1<<n)
912
+ function bnSetBit(n) { return this.changeBit(n,op_or); }
913
+
914
+ // (public) this & ~(1<<n)
915
+ function bnClearBit(n) { return this.changeBit(n,op_andnot); }
916
+
917
+ // (public) this ^ (1<<n)
918
+ function bnFlipBit(n) { return this.changeBit(n,op_xor); }
919
+
920
+ // (protected) r = this + a
921
+ function bnpAddTo(a,r) {
922
+ var i = 0, c = 0, m = Math.min(a.t,this.t);
923
+ while(i < m) {
924
+ c += this[i]+a[i];
925
+ r[i++] = c&this.DM;
926
+ c >>= this.DB;
927
+ }
928
+ if(a.t < this.t) {
929
+ c += a.s;
930
+ while(i < this.t) {
931
+ c += this[i];
932
+ r[i++] = c&this.DM;
933
+ c >>= this.DB;
934
+ }
935
+ c += this.s;
936
+ }
937
+ else {
938
+ c += this.s;
939
+ while(i < a.t) {
940
+ c += a[i];
941
+ r[i++] = c&this.DM;
942
+ c >>= this.DB;
943
+ }
944
+ c += a.s;
945
+ }
946
+ r.s = (c<0)?-1:0;
947
+ if(c > 0) r[i++] = c;
948
+ else if(c < -1) r[i++] = this.DV+c;
949
+ r.t = i;
950
+ r.clamp();
951
+ }
952
+
953
+ // (public) this + a
954
+ function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
955
+
956
+ // (public) this - a
957
+ function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
958
+
959
+ // (public) this * a
960
+ function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
961
+
962
+ // (public) this^2
963
+ function bnSquare() { var r = nbi(); this.squareTo(r); return r; }
964
+
965
+ // (public) this / a
966
+ function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
967
+
968
+ // (public) this % a
969
+ function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
970
+
971
+ // (public) [this/a,this%a]
972
+ function bnDivideAndRemainder(a) {
973
+ var q = nbi(), r = nbi();
974
+ this.divRemTo(a,q,r);
975
+ return new Array(q,r);
976
+ }
977
+
978
+ // (protected) this *= n, this >= 0, 1 < n < DV
979
+ function bnpDMultiply(n) {
980
+ this[this.t] = this.am(0,n-1,this,0,0,this.t);
981
+ ++this.t;
982
+ this.clamp();
983
+ }
984
+
985
+ // (protected) this += n << w words, this >= 0
986
+ function bnpDAddOffset(n,w) {
987
+ if(n == 0) return;
988
+ while(this.t <= w) this[this.t++] = 0;
989
+ this[w] += n;
990
+ while(this[w] >= this.DV) {
991
+ this[w] -= this.DV;
992
+ if(++w >= this.t) this[this.t++] = 0;
993
+ ++this[w];
994
+ }
995
+ }
996
+
997
+ // A "null" reducer
998
+ function NullExp() {}
999
+ function nNop(x) { return x; }
1000
+ function nMulTo(x,y,r) { x.multiplyTo(y,r); }
1001
+ function nSqrTo(x,r) { x.squareTo(r); }
1002
+
1003
+ NullExp.prototype.convert = nNop;
1004
+ NullExp.prototype.revert = nNop;
1005
+ NullExp.prototype.mulTo = nMulTo;
1006
+ NullExp.prototype.sqrTo = nSqrTo;
1007
+
1008
+ // (public) this^e
1009
+ function bnPow(e) { return this.exp(e,new NullExp()); }
1010
+
1011
+ // (protected) r = lower n words of "this * a", a.t <= n
1012
+ // "this" should be the larger one if appropriate.
1013
+ function bnpMultiplyLowerTo(a,n,r) {
1014
+ var i = Math.min(this.t+a.t,n);
1015
+ r.s = 0; // assumes a,this >= 0
1016
+ r.t = i;
1017
+ while(i > 0) r[--i] = 0;
1018
+ var j;
1019
+ for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t);
1020
+ for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i);
1021
+ r.clamp();
1022
+ }
1023
+
1024
+ // (protected) r = "this * a" without lower n words, n > 0
1025
+ // "this" should be the larger one if appropriate.
1026
+ function bnpMultiplyUpperTo(a,n,r) {
1027
+ --n;
1028
+ var i = r.t = this.t+a.t-n;
1029
+ r.s = 0; // assumes a,this >= 0
1030
+ while(--i >= 0) r[i] = 0;
1031
+ for(i = Math.max(n-this.t,0); i < a.t; ++i)
1032
+ r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n);
1033
+ r.clamp();
1034
+ r.drShiftTo(1,r);
1035
+ }
1036
+
1037
+ // Barrett modular reduction
1038
+ function Barrett(m) {
1039
+ // setup Barrett
1040
+ this.r2 = nbi();
1041
+ this.q3 = nbi();
1042
+ BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
1043
+ this.mu = this.r2.divide(m);
1044
+ this.m = m;
1045
+ }
1046
+
1047
+ function barrettConvert(x) {
1048
+ if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
1049
+ else if(x.compareTo(this.m) < 0) return x;
1050
+ else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
1051
+ }
1052
+
1053
+ function barrettRevert(x) { return x; }
1054
+
1055
+ // x = x mod m (HAC 14.42)
1056
+ function barrettReduce(x) {
1057
+ x.drShiftTo(this.m.t-1,this.r2);
1058
+ if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
1059
+ this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
1060
+ this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
1061
+ while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
1062
+ x.subTo(this.r2,x);
1063
+ while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
1064
+ }
1065
+
1066
+ // r = x^2 mod m; x != r
1067
+ function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
1068
+
1069
+ // r = x*y mod m; x,y != r
1070
+ function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
1071
+
1072
+ Barrett.prototype.convert = barrettConvert;
1073
+ Barrett.prototype.revert = barrettRevert;
1074
+ Barrett.prototype.reduce = barrettReduce;
1075
+ Barrett.prototype.mulTo = barrettMulTo;
1076
+ Barrett.prototype.sqrTo = barrettSqrTo;
1077
+
1078
+ // (public) this^e % m (HAC 14.85)
1079
+ function bnModPow(e,m) {
1080
+ var i = e.bitLength(), k, r = nbv(1), z;
1081
+ if(i <= 0) return r;
1082
+ else if(i < 18) k = 1;
1083
+ else if(i < 48) k = 3;
1084
+ else if(i < 144) k = 4;
1085
+ else if(i < 768) k = 5;
1086
+ else k = 6;
1087
+ if(i < 8)
1088
+ z = new Classic(m);
1089
+ else if(m.isEven())
1090
+ z = new Barrett(m);
1091
+ else
1092
+ z = new Montgomery(m);
1093
+
1094
+ // precomputation
1095
+ var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
1096
+ g[1] = z.convert(this);
1097
+ if(k > 1) {
1098
+ var g2 = nbi();
1099
+ z.sqrTo(g[1],g2);
1100
+ while(n <= km) {
1101
+ g[n] = nbi();
1102
+ z.mulTo(g2,g[n-2],g[n]);
1103
+ n += 2;
1104
+ }
1105
+ }
1106
+
1107
+ var j = e.t-1, w, is1 = true, r2 = nbi(), t;
1108
+ i = nbits(e[j])-1;
1109
+ while(j >= 0) {
1110
+ if(i >= k1) w = (e[j]>>(i-k1))&km;
1111
+ else {
1112
+ w = (e[j]&((1<<(i+1))-1))<<(k1-i);
1113
+ if(j > 0) w |= e[j-1]>>(this.DB+i-k1);
1114
+ }
1115
+
1116
+ n = k;
1117
+ while((w&1) == 0) { w >>= 1; --n; }
1118
+ if((i -= n) < 0) { i += this.DB; --j; }
1119
+ if(is1) { // ret == 1, don't bother squaring or multiplying it
1120
+ g[w].copyTo(r);
1121
+ is1 = false;
1122
+ }
1123
+ else {
1124
+ while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
1125
+ if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
1126
+ z.mulTo(r2,g[w],r);
1127
+ }
1128
+
1129
+ while(j >= 0 && (e[j]&(1<<i)) == 0) {
1130
+ z.sqrTo(r,r2); t = r; r = r2; r2 = t;
1131
+ if(--i < 0) { i = this.DB-1; --j; }
1132
+ }
1133
+ }
1134
+ return z.revert(r);
1135
+ }
1136
+
1137
+ // (public) gcd(this,a) (HAC 14.54)
1138
+ function bnGCD(a) {
1139
+ var x = (this.s<0)?this.negate():this.clone();
1140
+ var y = (a.s<0)?a.negate():a.clone();
1141
+ if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
1142
+ var i = x.getLowestSetBit(), g = y.getLowestSetBit();
1143
+ if(g < 0) return x;
1144
+ if(i < g) g = i;
1145
+ if(g > 0) {
1146
+ x.rShiftTo(g,x);
1147
+ y.rShiftTo(g,y);
1148
+ }
1149
+ while(x.signum() > 0) {
1150
+ if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
1151
+ if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
1152
+ if(x.compareTo(y) >= 0) {
1153
+ x.subTo(y,x);
1154
+ x.rShiftTo(1,x);
1155
+ }
1156
+ else {
1157
+ y.subTo(x,y);
1158
+ y.rShiftTo(1,y);
1159
+ }
1160
+ }
1161
+ if(g > 0) y.lShiftTo(g,y);
1162
+ return y;
1163
+ }
1164
+
1165
+ // (protected) this % n, n < 2^26
1166
+ function bnpModInt(n) {
1167
+ if(n <= 0) return 0;
1168
+ var d = this.DV%n, r = (this.s<0)?n-1:0;
1169
+ if(this.t > 0)
1170
+ if(d == 0) r = this[0]%n;
1171
+ else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n;
1172
+ return r;
1173
+ }
1174
+
1175
+ // (public) 1/this % m (HAC 14.61)
1176
+ function bnModInverse(m) {
1177
+ var ac = m.isEven();
1178
+ if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
1179
+ var u = m.clone(), v = this.clone();
1180
+ var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
1181
+ while(u.signum() != 0) {
1182
+ while(u.isEven()) {
1183
+ u.rShiftTo(1,u);
1184
+ if(ac) {
1185
+ if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
1186
+ a.rShiftTo(1,a);
1187
+ }
1188
+ else if(!b.isEven()) b.subTo(m,b);
1189
+ b.rShiftTo(1,b);
1190
+ }
1191
+ while(v.isEven()) {
1192
+ v.rShiftTo(1,v);
1193
+ if(ac) {
1194
+ if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
1195
+ c.rShiftTo(1,c);
1196
+ }
1197
+ else if(!d.isEven()) d.subTo(m,d);
1198
+ d.rShiftTo(1,d);
1199
+ }
1200
+ if(u.compareTo(v) >= 0) {
1201
+ u.subTo(v,u);
1202
+ if(ac) a.subTo(c,a);
1203
+ b.subTo(d,b);
1204
+ }
1205
+ else {
1206
+ v.subTo(u,v);
1207
+ if(ac) c.subTo(a,c);
1208
+ d.subTo(b,d);
1209
+ }
1210
+ }
1211
+ if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
1212
+ if(d.compareTo(m) >= 0) return d.subtract(m);
1213
+ if(d.signum() < 0) d.addTo(m,d); else return d;
1214
+ if(d.signum() < 0) return d.add(m); else return d;
1215
+ }
1216
+
1217
+ var 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];
1218
+ var lplim = (1<<26)/lowprimes[lowprimes.length-1];
1219
+
1220
+ // (public) test primality with certainty >= 1-.5^t
1221
+ function bnIsProbablePrime(t) {
1222
+ var i, x = this.abs();
1223
+ if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) {
1224
+ for(i = 0; i < lowprimes.length; ++i)
1225
+ if(x[0] == lowprimes[i]) return true;
1226
+ return false;
1227
+ }
1228
+ if(x.isEven()) return false;
1229
+ i = 1;
1230
+ while(i < lowprimes.length) {
1231
+ var m = lowprimes[i], j = i+1;
1232
+ while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
1233
+ m = x.modInt(m);
1234
+ while(i < j) if(m%lowprimes[i++] == 0) return false;
1235
+ }
1236
+ return x.millerRabin(t);
1237
+ }
1238
+
1239
+ // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
1240
+ function bnpMillerRabin(t) {
1241
+ var n1 = this.subtract(BigInteger.ONE);
1242
+ var k = n1.getLowestSetBit();
1243
+ if(k <= 0) return false;
1244
+ var r = n1.shiftRight(k);
1245
+ t = (t+1)>>1;
1246
+ if(t > lowprimes.length) t = lowprimes.length;
1247
+ var a = nbi();
1248
+ for(var i = 0; i < t; ++i) {
1249
+ //Pick bases at random, instead of starting at 2
1250
+ a.fromInt(lowprimes[Math.floor(Math.random()*lowprimes.length)]);
1251
+ var y = a.modPow(r,this);
1252
+ if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
1253
+ var j = 1;
1254
+ while(j++ < k && y.compareTo(n1) != 0) {
1255
+ y = y.modPowInt(2,this);
1256
+ if(y.compareTo(BigInteger.ONE) == 0) return false;
1257
+ }
1258
+ if(y.compareTo(n1) != 0) return false;
1259
+ }
1260
+ }
1261
+ return true;
1262
+ }
1263
+
1264
+ // protected
1265
+ BigInteger.prototype.chunkSize = bnpChunkSize;
1266
+ BigInteger.prototype.toRadix = bnpToRadix;
1267
+ BigInteger.prototype.fromRadix = bnpFromRadix;
1268
+ BigInteger.prototype.fromNumber = bnpFromNumber;
1269
+ BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
1270
+ BigInteger.prototype.changeBit = bnpChangeBit;
1271
+ BigInteger.prototype.addTo = bnpAddTo;
1272
+ BigInteger.prototype.dMultiply = bnpDMultiply;
1273
+ BigInteger.prototype.dAddOffset = bnpDAddOffset;
1274
+ BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
1275
+ BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
1276
+ BigInteger.prototype.modInt = bnpModInt;
1277
+ BigInteger.prototype.millerRabin = bnpMillerRabin;
1278
+
1279
+ // public
1280
+ BigInteger.prototype.clone = bnClone;
1281
+ BigInteger.prototype.intValue = bnIntValue;
1282
+ BigInteger.prototype.byteValue = bnByteValue;
1283
+ BigInteger.prototype.shortValue = bnShortValue;
1284
+ BigInteger.prototype.signum = bnSigNum;
1285
+ BigInteger.prototype.toByteArray = bnToByteArray;
1286
+ BigInteger.prototype.equals = bnEquals;
1287
+ BigInteger.prototype.min = bnMin;
1288
+ BigInteger.prototype.max = bnMax;
1289
+ BigInteger.prototype.and = bnAnd;
1290
+ BigInteger.prototype.or = bnOr;
1291
+ BigInteger.prototype.xor = bnXor;
1292
+ BigInteger.prototype.andNot = bnAndNot;
1293
+ BigInteger.prototype.not = bnNot;
1294
+ BigInteger.prototype.shiftLeft = bnShiftLeft;
1295
+ BigInteger.prototype.shiftRight = bnShiftRight;
1296
+ BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
1297
+ BigInteger.prototype.bitCount = bnBitCount;
1298
+ BigInteger.prototype.testBit = bnTestBit;
1299
+ BigInteger.prototype.setBit = bnSetBit;
1300
+ BigInteger.prototype.clearBit = bnClearBit;
1301
+ BigInteger.prototype.flipBit = bnFlipBit;
1302
+ BigInteger.prototype.add = bnAdd;
1303
+ BigInteger.prototype.subtract = bnSubtract;
1304
+ BigInteger.prototype.multiply = bnMultiply;
1305
+ BigInteger.prototype.divide = bnDivide;
1306
+ BigInteger.prototype.remainder = bnRemainder;
1307
+ BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
1308
+ BigInteger.prototype.modPow = bnModPow;
1309
+ BigInteger.prototype.modInverse = bnModInverse;
1310
+ BigInteger.prototype.pow = bnPow;
1311
+ BigInteger.prototype.gcd = bnGCD;
1312
+ BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
1313
+
1314
+ // JSBN-specific extension
1315
+ BigInteger.prototype.square = bnSquare;
1316
+
1317
+ // Expose the Barrett function
1318
+ BigInteger.prototype.Barrett = Barrett;
1319
+
1320
+ // BigInteger interfaces not implemented in jsbn:
1321
+
1322
+ // BigInteger(int signum, byte[] magnitude)
1323
+ // double doubleValue()
1324
+ // float floatValue()
1325
+ // int hashCode()
1326
+ // long longValue()
1327
+ // static BigInteger valueOf(long val)
1328
+
1329
+ // Random number generator - requires a PRNG backend, e.g. prng4.js
1330
+
1331
+ // For best results, put code like
1332
+ // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
1333
+ // in your main HTML document.
1334
+
1335
+ var rng_state;
1336
+ var rng_pool;
1337
+ var rng_pptr;
1338
+
1339
+ // Mix in a 32-bit integer into the pool
1340
+ function rng_seed_int(x) {
1341
+ rng_pool[rng_pptr++] ^= x & 255;
1342
+ rng_pool[rng_pptr++] ^= (x >> 8) & 255;
1343
+ rng_pool[rng_pptr++] ^= (x >> 16) & 255;
1344
+ rng_pool[rng_pptr++] ^= (x >> 24) & 255;
1345
+ if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
1346
+ }
1347
+
1348
+ // Mix in the current time (w/milliseconds) into the pool
1349
+ function rng_seed_time() {
1350
+ rng_seed_int(new Date().getTime());
1351
+ }
1352
+
1353
+ // Initialize the pool with junk if needed.
1354
+ if(rng_pool == null) {
1355
+ rng_pool = new Array();
1356
+ rng_pptr = 0;
1357
+ var t;
1358
+ if(typeof window !== "undefined" && window.crypto) {
1359
+ if (window.crypto.getRandomValues) {
1360
+ // Use webcrypto if available
1361
+ var ua = new Uint8Array(32);
1362
+ window.crypto.getRandomValues(ua);
1363
+ for(t = 0; t < 32; ++t)
1364
+ rng_pool[rng_pptr++] = ua[t];
1365
+ }
1366
+ else if(navigator.appName == "Netscape" && navigator.appVersion < "5") {
1367
+ // Extract entropy (256 bits) from NS4 RNG if available
1368
+ var z = window.crypto.random(32);
1369
+ for(t = 0; t < z.length; ++t)
1370
+ rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
1371
+ }
1372
+ }
1373
+ while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
1374
+ t = Math.floor(65536 * Math.random());
1375
+ rng_pool[rng_pptr++] = t >>> 8;
1376
+ rng_pool[rng_pptr++] = t & 255;
1377
+ }
1378
+ rng_pptr = 0;
1379
+ rng_seed_time();
1380
+ //rng_seed_int(window.screenX);
1381
+ //rng_seed_int(window.screenY);
1382
+ }
1383
+
1384
+ function rng_get_byte() {
1385
+ if(rng_state == null) {
1386
+ rng_seed_time();
1387
+ rng_state = prng_newstate();
1388
+ rng_state.init(rng_pool);
1389
+ for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
1390
+ rng_pool[rng_pptr] = 0;
1391
+ rng_pptr = 0;
1392
+ //rng_pool = null;
1393
+ }
1394
+ // TODO: allow reseeding after first request
1395
+ return rng_state.next();
1396
+ }
1397
+
1398
+ function rng_get_bytes(ba) {
1399
+ var i;
1400
+ for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
1401
+ }
1402
+
1403
+ function SecureRandom() {}
1404
+
1405
+ SecureRandom.prototype.nextBytes = rng_get_bytes;
1406
+
1407
+ // prng4.js - uses Arcfour as a PRNG
1408
+
1409
+ function Arcfour() {
1410
+ this.i = 0;
1411
+ this.j = 0;
1412
+ this.S = new Array();
1413
+ }
1414
+
1415
+ // Initialize arcfour context from key, an array of ints, each from [0..255]
1416
+ function ARC4init(key) {
1417
+ var i, j, t;
1418
+ for(i = 0; i < 256; ++i)
1419
+ this.S[i] = i;
1420
+ j = 0;
1421
+ for(i = 0; i < 256; ++i) {
1422
+ j = (j + this.S[i] + key[i % key.length]) & 255;
1423
+ t = this.S[i];
1424
+ this.S[i] = this.S[j];
1425
+ this.S[j] = t;
1426
+ }
1427
+ this.i = 0;
1428
+ this.j = 0;
1429
+ }
1430
+
1431
+ function ARC4next() {
1432
+ var t;
1433
+ this.i = (this.i + 1) & 255;
1434
+ this.j = (this.j + this.S[this.i]) & 255;
1435
+ t = this.S[this.i];
1436
+ this.S[this.i] = this.S[this.j];
1437
+ this.S[this.j] = t;
1438
+ return this.S[(t + this.S[this.i]) & 255];
1439
+ }
1440
+
1441
+ Arcfour.prototype.init = ARC4init;
1442
+ Arcfour.prototype.next = ARC4next;
1443
+
1444
+ // Plug in your RNG constructor here
1445
+ function prng_newstate() {
1446
+ return new Arcfour();
1447
+ }
1448
+
1449
+ // Pool size must be a multiple of 4 and greater than 32.
1450
+ // An array of bytes the size of the pool will be passed to init()
1451
+ var rng_psize = 256;
1452
+
1453
+ {
1454
+ module.exports = {
1455
+ default: BigInteger,
1456
+ BigInteger: BigInteger,
1457
+ SecureRandom: SecureRandom,
1458
+ };
1459
+ }
1460
+
1461
+ }).call(jsbn);
1462
+ } (jsbn$1));
1463
+ return jsbn$1.exports;
1464
+ }
1465
+
1466
+ var asn1;
1467
+ var hasRequiredAsn1;
1468
+ function requireAsn1() {
1469
+ if (hasRequiredAsn1) return asn1;
1470
+ hasRequiredAsn1 = 1;
1471
+ var _require$$ = requireJsbn(),
1472
+ BigInteger = _require$$.BigInteger;
1473
+ function bigintToValue(bigint) {
1474
+ var h = bigint.toString(16);
1475
+ if (h[0] !== '-') {
1476
+ // 正数
1477
+ if (h.length % 2 === 1) h = '0' + h; // 补齐到整字节
1478
+ else if (!h.match(/^[0-7]/)) h = '00' + h; // 非0开头,则补一个全0字节
1479
+ } else {
1480
+ // 负数
1481
+ h = h.substr(1);
1482
+ var len = h.length;
1483
+ if (len % 2 === 1) len += 1; // 补齐到整字节
1484
+ else if (!h.match(/^[0-7]/)) len += 2; // 非0开头,则补一个全0字节
1485
+
1486
+ var mask = '';
1487
+ for (var i = 0; i < len; i++) mask += 'f';
1488
+ mask = new BigInteger(mask, 16);
1489
+
1490
+ // 对绝对值取反,加1
1491
+ h = mask.xor(bigint).add(BigInteger.ONE);
1492
+ h = h.toString(16).replace(/^-/, '');
1493
+ }
1494
+ return h;
1495
+ }
1496
+ var ASN1Object = /*#__PURE__*/function () {
1497
+ function ASN1Object() {
1498
+ _classCallCheck(this, ASN1Object);
1499
+ this.tlv = null;
1500
+ this.t = '00';
1501
+ this.l = '00';
1502
+ this.v = '';
1503
+ }
1504
+
1505
+ /**
1506
+ * 获取 der 编码比特流16进制串
1507
+ */
1508
+ return _createClass(ASN1Object, [{
1509
+ key: "getEncodedHex",
1510
+ value: function getEncodedHex() {
1511
+ if (!this.tlv) {
1512
+ this.v = this.getValue();
1513
+ this.l = this.getLength();
1514
+ this.tlv = this.t + this.l + this.v;
1515
+ }
1516
+ return this.tlv;
1517
+ }
1518
+ }, {
1519
+ key: "getLength",
1520
+ value: function getLength() {
1521
+ var n = this.v.length / 2; // 字节数
1522
+ var nHex = n.toString(16);
1523
+ if (nHex.length % 2 === 1) nHex = '0' + nHex; // 补齐到整字节
1524
+
1525
+ if (n < 128) {
1526
+ // 短格式,以 0 开头
1527
+ return nHex;
1528
+ } else {
1529
+ // 长格式,以 1 开头
1530
+ var head = 128 + nHex.length / 2; // 1(1位) + 真正的长度占用字节数(7位) + 真正的长度
1531
+ return head.toString(16) + nHex;
1532
+ }
1533
+ }
1534
+ }, {
1535
+ key: "getValue",
1536
+ value: function getValue() {
1537
+ return '';
1538
+ }
1539
+ }]);
1540
+ }();
1541
+ var DERInteger = /*#__PURE__*/function (_ASN1Object) {
1542
+ function DERInteger(bigint) {
1543
+ var _this;
1544
+ _classCallCheck(this, DERInteger);
1545
+ _this = _callSuper(this, DERInteger);
1546
+ _this.t = '02'; // 整型标签说明
1547
+ if (bigint) _this.v = bigintToValue(bigint);
1548
+ return _this;
1549
+ }
1550
+ _inherits(DERInteger, _ASN1Object);
1551
+ return _createClass(DERInteger, [{
1552
+ key: "getValue",
1553
+ value: function getValue() {
1554
+ return this.v;
1555
+ }
1556
+ }]);
1557
+ }(ASN1Object);
1558
+ var DERSequence = /*#__PURE__*/function (_ASN1Object2) {
1559
+ function DERSequence(asn1Array) {
1560
+ var _this2;
1561
+ _classCallCheck(this, DERSequence);
1562
+ _this2 = _callSuper(this, DERSequence);
1563
+ _this2.t = '30'; // 序列标签说明
1564
+ _this2.asn1Array = asn1Array;
1565
+ return _this2;
1566
+ }
1567
+ _inherits(DERSequence, _ASN1Object2);
1568
+ return _createClass(DERSequence, [{
1569
+ key: "getValue",
1570
+ value: function getValue() {
1571
+ this.v = this.asn1Array.map(function (asn1Object) {
1572
+ return asn1Object.getEncodedHex();
1573
+ }).join('');
1574
+ return this.v;
1575
+ }
1576
+ }]);
1577
+ }(ASN1Object);
1578
+ /**
1579
+ * 获取 l 占用字节数
1580
+ */
1581
+ function getLenOfL(str, start) {
1582
+ if (+str[start + 2] < 8) return 1; // l 以0开头,则表示短格式,只占一个字节
1583
+ return +str.substr(start + 2, 2) & 0x7f + 1; // 长格式,取第一个字节后7位作为长度真正占用字节数,再加上本身
1584
+ }
1585
+
1586
+ /**
1587
+ * 获取 l
1588
+ */
1589
+ function getL(str, start) {
1590
+ // 获取 l
1591
+ var len = getLenOfL(str, start);
1592
+ var l = str.substr(start + 2, len * 2);
1593
+ if (!l) return -1;
1594
+ var bigint = +l[0] < 8 ? new BigInteger(l, 16) : new BigInteger(l.substr(2), 16);
1595
+ return bigint.intValue();
1596
+ }
1597
+
1598
+ /**
1599
+ * 获取 v 的位置
1600
+ */
1601
+ function getStartOfV(str, start) {
1602
+ var len = getLenOfL(str, start);
1603
+ return start + (len + 1) * 2;
1604
+ }
1605
+ asn1 = {
1606
+ /**
1607
+ * ASN.1 der 编码,针对 sm2 签名
1608
+ */
1609
+ encodeDer: function encodeDer(r, s) {
1610
+ var derR = new DERInteger(r);
1611
+ var derS = new DERInteger(s);
1612
+ var derSeq = new DERSequence([derR, derS]);
1613
+ return derSeq.getEncodedHex();
1614
+ },
1615
+ /**
1616
+ * 解析 ASN.1 der,针对 sm2 验签
1617
+ */
1618
+ decodeDer: function decodeDer(input) {
1619
+ // 结构:
1620
+ // input = | tSeq | lSeq | vSeq |
1621
+ // vSeq = | tR | lR | vR | tS | lS | vS |
1622
+ var start = getStartOfV(input, 0);
1623
+ var vIndexR = getStartOfV(input, start);
1624
+ var lR = getL(input, start);
1625
+ var vR = input.substr(vIndexR, lR * 2);
1626
+ var nextStart = vIndexR + vR.length;
1627
+ var vIndexS = getStartOfV(input, nextStart);
1628
+ var lS = getL(input, nextStart);
1629
+ var vS = input.substr(vIndexS, lS * 2);
1630
+ var r = new BigInteger(vR, 16);
1631
+ var s = new BigInteger(vS, 16);
1632
+ return {
1633
+ r: r,
1634
+ s: s
1635
+ };
1636
+ }
1637
+ };
1638
+ return asn1;
1639
+ }
1640
+
1641
+ var ec;
1642
+ var hasRequiredEc;
1643
+ function requireEc() {
1644
+ if (hasRequiredEc) return ec;
1645
+ hasRequiredEc = 1;
1646
+ var _require$$ = requireJsbn(),
1647
+ BigInteger = _require$$.BigInteger;
1648
+
1649
+ /**
1650
+ * thanks for Tom Wu : http://www-cs-students.stanford.edu/~tjw/jsbn/
1651
+ *
1652
+ * Basic Javascript Elliptic Curve implementation
1653
+ * Ported loosely from BouncyCastle's Java EC code
1654
+ * Only Fp curves implemented for now
1655
+ */
1656
+
1657
+ var TWO = new BigInteger('2');
1658
+ var THREE = new BigInteger('3');
1659
+
1660
+ /**
1661
+ * 椭圆曲线域元素
1662
+ */
1663
+ var ECFieldElementFp = /*#__PURE__*/function () {
1664
+ function ECFieldElementFp(q, x) {
1665
+ _classCallCheck(this, ECFieldElementFp);
1666
+ this.x = x;
1667
+ this.q = q;
1668
+ // TODO if (x.compareTo(q) >= 0) error
1669
+ }
1670
+
1671
+ /**
1672
+ * 判断相等
1673
+ */
1674
+ return _createClass(ECFieldElementFp, [{
1675
+ key: "equals",
1676
+ value: function equals(other) {
1677
+ if (other === this) return true;
1678
+ return this.q.equals(other.q) && this.x.equals(other.x);
1679
+ }
1680
+
1681
+ /**
1682
+ * 返回具体数值
1683
+ */
1684
+ }, {
1685
+ key: "toBigInteger",
1686
+ value: function toBigInteger() {
1687
+ return this.x;
1688
+ }
1689
+
1690
+ /**
1691
+ * 取反
1692
+ */
1693
+ }, {
1694
+ key: "negate",
1695
+ value: function negate() {
1696
+ return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
1697
+ }
1698
+
1699
+ /**
1700
+ * 相加
1701
+ */
1702
+ }, {
1703
+ key: "add",
1704
+ value: function add(b) {
1705
+ return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
1706
+ }
1707
+
1708
+ /**
1709
+ * 相减
1710
+ */
1711
+ }, {
1712
+ key: "subtract",
1713
+ value: function subtract(b) {
1714
+ return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
1715
+ }
1716
+
1717
+ /**
1718
+ * 相乘
1719
+ */
1720
+ }, {
1721
+ key: "multiply",
1722
+ value: function multiply(b) {
1723
+ return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
1724
+ }
1725
+
1726
+ /**
1727
+ * 相除
1728
+ */
1729
+ }, {
1730
+ key: "divide",
1731
+ value: function divide(b) {
1732
+ return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
1733
+ }
1734
+
1735
+ /**
1736
+ * 平方
1737
+ */
1738
+ }, {
1739
+ key: "square",
1740
+ value: function square() {
1741
+ return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
1742
+ }
1743
+ }]);
1744
+ }();
1745
+ var ECPointFp = /*#__PURE__*/function () {
1746
+ function ECPointFp(curve, x, y, z) {
1747
+ _classCallCheck(this, ECPointFp);
1748
+ this.curve = curve;
1749
+ this.x = x;
1750
+ this.y = y;
1751
+ // 标准射影坐标系:zinv == null 或 z * zinv == 1
1752
+ this.z = z == null ? BigInteger.ONE : z;
1753
+ this.zinv = null;
1754
+ // TODO: compression flag
1755
+ }
1756
+ return _createClass(ECPointFp, [{
1757
+ key: "getX",
1758
+ value: function getX() {
1759
+ if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q);
1760
+ return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
1761
+ }
1762
+ }, {
1763
+ key: "getY",
1764
+ value: function getY() {
1765
+ if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q);
1766
+ return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
1767
+ }
1768
+
1769
+ /**
1770
+ * 判断相等
1771
+ */
1772
+ }, {
1773
+ key: "equals",
1774
+ value: function equals(other) {
1775
+ if (other === this) return true;
1776
+ if (this.isInfinity()) return other.isInfinity();
1777
+ if (other.isInfinity()) return this.isInfinity();
1778
+
1779
+ // u = y2 * z1 - y1 * z2
1780
+ var u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
1781
+ if (!u.equals(BigInteger.ZERO)) return false;
1782
+
1783
+ // v = x2 * z1 - x1 * z2
1784
+ var v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
1785
+ return v.equals(BigInteger.ZERO);
1786
+ }
1787
+
1788
+ /**
1789
+ * 是否是无穷远点
1790
+ */
1791
+ }, {
1792
+ key: "isInfinity",
1793
+ value: function isInfinity() {
1794
+ if (this.x === null && this.y === null) return true;
1795
+ return this.z.equals(BigInteger.ZERO) && !this.y.toBigInteger().equals(BigInteger.ZERO);
1796
+ }
1797
+
1798
+ /**
1799
+ * 取反,x 轴对称点
1800
+ */
1801
+ }, {
1802
+ key: "negate",
1803
+ value: function negate() {
1804
+ return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
1805
+ }
1806
+
1807
+ /**
1808
+ * 相加
1809
+ *
1810
+ * 标准射影坐标系:
1811
+ *
1812
+ * λ1 = x1 * z2
1813
+ * λ2 = x2 * z1
1814
+ * λ3 = λ1 − λ2
1815
+ * λ4 = y1 * z2
1816
+ * λ5 = y2 * z1
1817
+ * λ6 = λ4 − λ5
1818
+ * λ7 = λ1 + λ2
1819
+ * λ8 = z1 * z2
1820
+ * λ9 = λ3^2
1821
+ * λ10 = λ3 * λ9
1822
+ * λ11 = λ8 * λ6^2 − λ7 * λ9
1823
+ * x3 = λ3 * λ11
1824
+ * y3 = λ6 * (λ9 * λ1 − λ11) − λ4 * λ10
1825
+ * z3 = λ10 * λ8
1826
+ */
1827
+ }, {
1828
+ key: "add",
1829
+ value: function add(b) {
1830
+ if (this.isInfinity()) return b;
1831
+ if (b.isInfinity()) return this;
1832
+ var x1 = this.x.toBigInteger();
1833
+ var y1 = this.y.toBigInteger();
1834
+ var z1 = this.z;
1835
+ var x2 = b.x.toBigInteger();
1836
+ var y2 = b.y.toBigInteger();
1837
+ var z2 = b.z;
1838
+ var q = this.curve.q;
1839
+ var w1 = x1.multiply(z2).mod(q);
1840
+ var w2 = x2.multiply(z1).mod(q);
1841
+ var w3 = w1.subtract(w2);
1842
+ var w4 = y1.multiply(z2).mod(q);
1843
+ var w5 = y2.multiply(z1).mod(q);
1844
+ var w6 = w4.subtract(w5);
1845
+ if (BigInteger.ZERO.equals(w3)) {
1846
+ if (BigInteger.ZERO.equals(w6)) {
1847
+ return this.twice(); // this == b,计算自加
1848
+ }
1849
+ return this.curve.infinity; // this == -b,则返回无穷远点
1850
+ }
1851
+ var w7 = w1.add(w2);
1852
+ var w8 = z1.multiply(z2).mod(q);
1853
+ var w9 = w3.square().mod(q);
1854
+ var w10 = w3.multiply(w9).mod(q);
1855
+ var w11 = w8.multiply(w6.square()).subtract(w7.multiply(w9)).mod(q);
1856
+ var x3 = w3.multiply(w11).mod(q);
1857
+ var y3 = w6.multiply(w9.multiply(w1).subtract(w11)).subtract(w4.multiply(w10)).mod(q);
1858
+ var z3 = w10.multiply(w8).mod(q);
1859
+ return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
1860
+ }
1861
+
1862
+ /**
1863
+ * 自加
1864
+ *
1865
+ * 标准射影坐标系:
1866
+ *
1867
+ * λ1 = 3 * x1^2 + a * z1^2
1868
+ * λ2 = 2 * y1 * z1
1869
+ * λ3 = y1^2
1870
+ * λ4 = λ3 * x1 * z1
1871
+ * λ5 = λ2^2
1872
+ * λ6 = λ1^2 − 8 * λ4
1873
+ * x3 = λ2 * λ6
1874
+ * y3 = λ1 * (4 * λ4 − λ6) − 2 * λ5 * λ3
1875
+ * z3 = λ2 * λ5
1876
+ */
1877
+ }, {
1878
+ key: "twice",
1879
+ value: function twice() {
1880
+ if (this.isInfinity()) return this;
1881
+ if (!this.y.toBigInteger().signum()) return this.curve.infinity;
1882
+ var x1 = this.x.toBigInteger();
1883
+ var y1 = this.y.toBigInteger();
1884
+ var z1 = this.z;
1885
+ var q = this.curve.q;
1886
+ var a = this.curve.a.toBigInteger();
1887
+ var w1 = x1.square().multiply(THREE).add(a.multiply(z1.square())).mod(q);
1888
+ var w2 = y1.shiftLeft(1).multiply(z1).mod(q);
1889
+ var w3 = y1.square().mod(q);
1890
+ var w4 = w3.multiply(x1).multiply(z1).mod(q);
1891
+ var w5 = w2.square().mod(q);
1892
+ var w6 = w1.square().subtract(w4.shiftLeft(3)).mod(q);
1893
+ var x3 = w2.multiply(w6).mod(q);
1894
+ var y3 = w1.multiply(w4.shiftLeft(2).subtract(w6)).subtract(w5.shiftLeft(1).multiply(w3)).mod(q);
1895
+ var z3 = w2.multiply(w5).mod(q);
1896
+ return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
1897
+ }
1898
+
1899
+ /**
1900
+ * 倍点计算
1901
+ */
1902
+ }, {
1903
+ key: "multiply",
1904
+ value: function multiply(k) {
1905
+ if (this.isInfinity()) return this;
1906
+ if (!k.signum()) return this.curve.infinity;
1907
+
1908
+ // 使用加减法
1909
+ var k3 = k.multiply(THREE);
1910
+ var neg = this.negate();
1911
+ var Q = this;
1912
+ for (var i = k3.bitLength() - 2; i > 0; i--) {
1913
+ Q = Q.twice();
1914
+ var k3Bit = k3.testBit(i);
1915
+ var kBit = k.testBit(i);
1916
+ if (k3Bit !== kBit) {
1917
+ Q = Q.add(k3Bit ? this : neg);
1918
+ }
1919
+ }
1920
+ return Q;
1921
+ }
1922
+ }]);
1923
+ }();
1924
+ /**
1925
+ * 椭圆曲线 y^2 = x^3 + ax + b
1926
+ */
1927
+ var ECCurveFp = /*#__PURE__*/function () {
1928
+ function ECCurveFp(q, a, b) {
1929
+ _classCallCheck(this, ECCurveFp);
1930
+ this.q = q;
1931
+ this.a = this.fromBigInteger(a);
1932
+ this.b = this.fromBigInteger(b);
1933
+ this.infinity = new ECPointFp(this, null, null); // 无穷远点
1934
+ }
1935
+
1936
+ /**
1937
+ * 判断两个椭圆曲线是否相等
1938
+ */
1939
+ return _createClass(ECCurveFp, [{
1940
+ key: "equals",
1941
+ value: function equals(other) {
1942
+ if (other === this) return true;
1943
+ return this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b);
1944
+ }
1945
+
1946
+ /**
1947
+ * 生成椭圆曲线域元素
1948
+ */
1949
+ }, {
1950
+ key: "fromBigInteger",
1951
+ value: function fromBigInteger(x) {
1952
+ return new ECFieldElementFp(this.q, x);
1953
+ }
1954
+
1955
+ /**
1956
+ * 解析 16 进制串为椭圆曲线点
1957
+ */
1958
+ }, {
1959
+ key: "decodePointHex",
1960
+ value: function decodePointHex(s) {
1961
+ switch (parseInt(s.substr(0, 2), 16)) {
1962
+ // 第一个字节
1963
+ case 0:
1964
+ return this.infinity;
1965
+ case 2:
1966
+ case 3:
1967
+ // 压缩
1968
+ var x = this.fromBigInteger(new BigInteger(s.substr(2), 16));
1969
+ // 对 p ≡ 3 (mod4),即存在正整数 u,使得 p = 4u + 3
1970
+ // 计算 y = (√ (x^3 + ax + b) % p)^(u + 1) modp
1971
+ var y = this.fromBigInteger(x.multiply(x.square()).add(x.multiply(this.a)).add(this.b).toBigInteger().modPow(this.q.divide(new BigInteger('4')).add(BigInteger.ONE), this.q));
1972
+ // 算出结果 2 进制最后 1 位不等于第 1 个字节减 2 则取反
1973
+ if (!y.toBigInteger().mod(TWO).equals(new BigInteger(s.substr(0, 2), 16).subtract(TWO))) {
1974
+ y = y.negate();
1975
+ }
1976
+ return new ECPointFp(this, x, y);
1977
+ case 4:
1978
+ case 6:
1979
+ case 7:
1980
+ var len = (s.length - 2) / 2;
1981
+ var xHex = s.substr(2, len);
1982
+ var yHex = s.substr(len + 2, len);
1983
+ return new ECPointFp(this, this.fromBigInteger(new BigInteger(xHex, 16)), this.fromBigInteger(new BigInteger(yHex, 16)));
1984
+ default:
1985
+ // 不支持
1986
+ return null;
1987
+ }
1988
+ }
1989
+ }]);
1990
+ }();
1991
+ ec = {
1992
+ ECPointFp: ECPointFp,
1993
+ ECCurveFp: ECCurveFp
1994
+ };
1995
+ return ec;
1996
+ }
1997
+
1998
+ /* eslint-disable no-bitwise, no-mixed-operators, no-use-before-define, max-len */
1999
+ var utils;
2000
+ var hasRequiredUtils;
2001
+ function requireUtils() {
2002
+ if (hasRequiredUtils) return utils;
2003
+ hasRequiredUtils = 1;
2004
+ var _require$$ = requireJsbn(),
2005
+ BigInteger = _require$$.BigInteger,
2006
+ SecureRandom = _require$$.SecureRandom;
2007
+ var _require$$2 = requireEc(),
2008
+ ECCurveFp = _require$$2.ECCurveFp;
2009
+ var rng = new SecureRandom();
2010
+ var _generateEcparam = generateEcparam(),
2011
+ curve = _generateEcparam.curve,
2012
+ G = _generateEcparam.G,
2013
+ n = _generateEcparam.n;
2014
+
2015
+ /**
2016
+ * 获取公共椭圆曲线
2017
+ */
2018
+ function getGlobalCurve() {
2019
+ return curve;
2020
+ }
2021
+
2022
+ /**
2023
+ * 生成ecparam
2024
+ */
2025
+ function generateEcparam() {
2026
+ // 椭圆曲线
2027
+ var p = new BigInteger('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF', 16);
2028
+ var a = new BigInteger('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC', 16);
2029
+ var b = new BigInteger('28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93', 16);
2030
+ var curve = new ECCurveFp(p, a, b);
2031
+
2032
+ // 基点
2033
+ var gxHex = '32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7';
2034
+ var gyHex = 'BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0';
2035
+ var G = curve.decodePointHex('04' + gxHex + gyHex);
2036
+ var n = new BigInteger('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123', 16);
2037
+ return {
2038
+ curve: curve,
2039
+ G: G,
2040
+ n: n
2041
+ };
2042
+ }
2043
+
2044
+ /**
2045
+ * 生成密钥对:publicKey = privateKey * G
2046
+ */
2047
+ function generateKeyPairHex(a, b, c) {
2048
+ var random = a ? new BigInteger(a, b, c) : new BigInteger(n.bitLength(), rng);
2049
+ var d = random.mod(n.subtract(BigInteger.ONE)).add(BigInteger.ONE); // 随机数
2050
+ var privateKey = leftPad(d.toString(16), 64);
2051
+ var P = G.multiply(d); // P = dG,p 为公钥,d 为私钥
2052
+ var Px = leftPad(P.getX().toBigInteger().toString(16), 64);
2053
+ var Py = leftPad(P.getY().toBigInteger().toString(16), 64);
2054
+ var publicKey = '04' + Px + Py;
2055
+ return {
2056
+ privateKey: privateKey,
2057
+ publicKey: publicKey
2058
+ };
2059
+ }
2060
+
2061
+ /**
2062
+ * 生成压缩公钥
2063
+ */
2064
+ function compressPublicKeyHex(s) {
2065
+ if (s.length !== 130) throw new Error('Invalid public key to compress');
2066
+ var len = (s.length - 2) / 2;
2067
+ var xHex = s.substr(2, len);
2068
+ var y = new BigInteger(s.substr(len + 2, len), 16);
2069
+ var prefix = '03';
2070
+ if (y.mod(new BigInteger('2')).equals(BigInteger.ZERO)) prefix = '02';
2071
+ return prefix + xHex;
2072
+ }
2073
+
2074
+ /**
2075
+ * utf8串转16进制串
2076
+ */
2077
+ function utf8ToHex(input) {
2078
+ input = unescape(encodeURIComponent(input));
2079
+ var length = input.length;
2080
+
2081
+ // 转换到字数组
2082
+ var words = [];
2083
+ for (var i = 0; i < length; i++) {
2084
+ words[i >>> 2] |= (input.charCodeAt(i) & 0xff) << 24 - i % 4 * 8;
2085
+ }
2086
+
2087
+ // 转换到16进制
2088
+ var hexChars = [];
2089
+ for (var _i = 0; _i < length; _i++) {
2090
+ var bite = words[_i >>> 2] >>> 24 - _i % 4 * 8 & 0xff;
2091
+ hexChars.push((bite >>> 4).toString(16));
2092
+ hexChars.push((bite & 0x0f).toString(16));
2093
+ }
2094
+ return hexChars.join('');
2095
+ }
2096
+
2097
+ /**
2098
+ * 补全16进制字符串
2099
+ */
2100
+ function leftPad(input, num) {
2101
+ if (input.length >= num) return input;
2102
+ return new Array(num - input.length + 1).join('0') + input;
2103
+ }
2104
+
2105
+ /**
2106
+ * 转成16进制串
2107
+ */
2108
+ function arrayToHex(arr) {
2109
+ return arr.map(function (item) {
2110
+ item = item.toString(16);
2111
+ return item.length === 1 ? '0' + item : item;
2112
+ }).join('');
2113
+ }
2114
+
2115
+ /**
2116
+ * 转成utf8串
2117
+ */
2118
+ function arrayToUtf8(arr) {
2119
+ var words = [];
2120
+ var j = 0;
2121
+ for (var i = 0; i < arr.length * 2; i += 2) {
2122
+ words[i >>> 3] |= parseInt(arr[j], 10) << 24 - i % 8 * 4;
2123
+ j++;
2124
+ }
2125
+ try {
2126
+ var latin1Chars = [];
2127
+ for (var _i2 = 0; _i2 < arr.length; _i2++) {
2128
+ var bite = words[_i2 >>> 2] >>> 24 - _i2 % 4 * 8 & 0xff;
2129
+ latin1Chars.push(String.fromCharCode(bite));
2130
+ }
2131
+ return decodeURIComponent(escape(latin1Chars.join('')));
2132
+ } catch (e) {
2133
+ throw new Error('Malformed UTF-8 data');
2134
+ }
2135
+ }
2136
+
2137
+ /**
2138
+ * 转成字节数组
2139
+ */
2140
+ function hexToArray(hexStr) {
2141
+ var words = [];
2142
+ var hexStrLength = hexStr.length;
2143
+ if (hexStrLength % 2 !== 0) {
2144
+ hexStr = leftPad(hexStr, hexStrLength + 1);
2145
+ }
2146
+ hexStrLength = hexStr.length;
2147
+ for (var i = 0; i < hexStrLength; i += 2) {
2148
+ words.push(parseInt(hexStr.substr(i, 2), 16));
2149
+ }
2150
+ return words;
2151
+ }
2152
+
2153
+ /**
2154
+ * 验证公钥是否为椭圆曲线上的点
2155
+ */
2156
+ function verifyPublicKey(publicKey) {
2157
+ var point = curve.decodePointHex(publicKey);
2158
+ if (!point) return false;
2159
+ var x = point.getX();
2160
+ var y = point.getY();
2161
+
2162
+ // 验证 y^2 是否等于 x^3 + ax + b
2163
+ return y.square().equals(x.multiply(x.square()).add(x.multiply(curve.a)).add(curve.b));
2164
+ }
2165
+
2166
+ /**
2167
+ * 验证公钥是否等价,等价返回true
2168
+ */
2169
+ function comparePublicKeyHex(publicKey1, publicKey2) {
2170
+ var point1 = curve.decodePointHex(publicKey1);
2171
+ if (!point1) return false;
2172
+ var point2 = curve.decodePointHex(publicKey2);
2173
+ if (!point2) return false;
2174
+ return point1.equals(point2);
2175
+ }
2176
+ utils = {
2177
+ getGlobalCurve: getGlobalCurve,
2178
+ generateEcparam: generateEcparam,
2179
+ generateKeyPairHex: generateKeyPairHex,
2180
+ compressPublicKeyHex: compressPublicKeyHex,
2181
+ utf8ToHex: utf8ToHex,
2182
+ leftPad: leftPad,
2183
+ arrayToHex: arrayToHex,
2184
+ arrayToUtf8: arrayToUtf8,
2185
+ hexToArray: hexToArray,
2186
+ verifyPublicKey: verifyPublicKey,
2187
+ comparePublicKeyHex: comparePublicKeyHex
2188
+ };
2189
+ return utils;
2190
+ }
2191
+
2192
+ var sm3_1$1;
2193
+ var hasRequiredSm3$1;
2194
+ function requireSm3$1() {
2195
+ if (hasRequiredSm3$1) return sm3_1$1;
2196
+ hasRequiredSm3$1 = 1;
2197
+ // 消息扩展
2198
+ var W = new Uint32Array(68);
2199
+ var M = new Uint32Array(64); // W'
2200
+
2201
+ /**
2202
+ * 循环左移
2203
+ */
2204
+ function rotl(x, n) {
2205
+ var s = n & 31;
2206
+ return x << s | x >>> 32 - s;
2207
+ }
2208
+
2209
+ /**
2210
+ * 二进制异或运算
2211
+ */
2212
+ function xor(x, y) {
2213
+ var result = [];
2214
+ for (var i = x.length - 1; i >= 0; i--) result[i] = (x[i] ^ y[i]) & 0xff;
2215
+ return result;
2216
+ }
2217
+
2218
+ /**
2219
+ * 压缩函数中的置换函数 P0(X) = X xor (X <<< 9) xor (X <<< 17)
2220
+ */
2221
+ function P0(X) {
2222
+ return X ^ rotl(X, 9) ^ rotl(X, 17);
2223
+ }
2224
+
2225
+ /**
2226
+ * 消息扩展中的置换函数 P1(X) = X xor (X <<< 15) xor (X <<< 23)
2227
+ */
2228
+ function P1(X) {
2229
+ return X ^ rotl(X, 15) ^ rotl(X, 23);
2230
+ }
2231
+
2232
+ /**
2233
+ * sm3 本体
2234
+ */
2235
+ function sm3(array) {
2236
+ var len = array.length * 8;
2237
+
2238
+ // k 是满足 len + 1 + k = 448mod512 的最小的非负整数
2239
+ var k = len % 512;
2240
+ // 如果 448 <= (512 % len) < 512,需要多补充 (len % 448) 比特'0'以满足总比特长度为512的倍数
2241
+ k = k >= 448 ? 512 - k % 448 - 1 : 448 - k - 1;
2242
+
2243
+ // 填充
2244
+ var kArr = new Array((k - 7) / 8);
2245
+ var lenArr = new Array(8);
2246
+ for (var i = 0, _len = kArr.length; i < _len; i++) kArr[i] = 0;
2247
+ for (var _i = 0, _len2 = lenArr.length; _i < _len2; _i++) lenArr[_i] = 0;
2248
+ len = len.toString(2);
2249
+ for (var _i2 = 7; _i2 >= 0; _i2--) {
2250
+ if (len.length > 8) {
2251
+ var start = len.length - 8;
2252
+ lenArr[_i2] = parseInt(len.substr(start), 2);
2253
+ len = len.substr(0, start);
2254
+ } else if (len.length > 0) {
2255
+ lenArr[_i2] = parseInt(len, 2);
2256
+ len = '';
2257
+ }
2258
+ }
2259
+ var m = new Uint8Array([].concat(_toConsumableArray(array), [0x80], kArr, lenArr));
2260
+ var dataView = new DataView(m.buffer, 0);
2261
+
2262
+ // 迭代压缩
2263
+ var n = m.length / 64;
2264
+ var V = new Uint32Array([0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e]);
2265
+ for (var _i3 = 0; _i3 < n; _i3++) {
2266
+ W.fill(0);
2267
+ M.fill(0);
2268
+
2269
+ // 将消息分组B划分为 16 个字 W0, W1,……,W15
2270
+ var _start = 16 * _i3;
2271
+ for (var j = 0; j < 16; j++) {
2272
+ W[j] = dataView.getUint32((_start + j) * 4, false);
2273
+ }
2274
+
2275
+ // W16 ~ W67:W[j] <- P1(W[j−16] xor W[j−9] xor (W[j−3] <<< 15)) xor (W[j−13] <<< 7) xor W[j−6]
2276
+ for (var _j = 16; _j < 68; _j++) {
2277
+ W[_j] = P1(W[_j - 16] ^ W[_j - 9] ^ rotl(W[_j - 3], 15)) ^ rotl(W[_j - 13], 7) ^ W[_j - 6];
2278
+ }
2279
+
2280
+ // W′0 ~ W′63:W′[j] = W[j] xor W[j+4]
2281
+ for (var _j2 = 0; _j2 < 64; _j2++) {
2282
+ M[_j2] = W[_j2] ^ W[_j2 + 4];
2283
+ }
2284
+
2285
+ // 压缩
2286
+ var T1 = 0x79cc4519;
2287
+ var T2 = 0x7a879d8a;
2288
+ // 字寄存器
2289
+ var A = V[0];
2290
+ var B = V[1];
2291
+ var C = V[2];
2292
+ var D = V[3];
2293
+ var E = V[4];
2294
+ var F = V[5];
2295
+ var G = V[6];
2296
+ var H = V[7];
2297
+ // 中间变量
2298
+ var SS1 = void 0;
2299
+ var SS2 = void 0;
2300
+ var TT1 = void 0;
2301
+ var TT2 = void 0;
2302
+ var T = void 0;
2303
+ for (var _j3 = 0; _j3 < 64; _j3++) {
2304
+ T = _j3 >= 0 && _j3 <= 15 ? T1 : T2;
2305
+ SS1 = rotl(rotl(A, 12) + E + rotl(T, _j3), 7);
2306
+ SS2 = SS1 ^ rotl(A, 12);
2307
+ TT1 = (_j3 >= 0 && _j3 <= 15 ? A ^ B ^ C : A & B | A & C | B & C) + D + SS2 + M[_j3];
2308
+ TT2 = (_j3 >= 0 && _j3 <= 15 ? E ^ F ^ G : E & F | ~E & G) + H + SS1 + W[_j3];
2309
+ D = C;
2310
+ C = rotl(B, 9);
2311
+ B = A;
2312
+ A = TT1;
2313
+ H = G;
2314
+ G = rotl(F, 19);
2315
+ F = E;
2316
+ E = P0(TT2);
2317
+ }
2318
+ V[0] ^= A;
2319
+ V[1] ^= B;
2320
+ V[2] ^= C;
2321
+ V[3] ^= D;
2322
+ V[4] ^= E;
2323
+ V[5] ^= F;
2324
+ V[6] ^= G;
2325
+ V[7] ^= H;
2326
+ }
2327
+
2328
+ // 转回 uint8
2329
+ var result = [];
2330
+ for (var _i4 = 0, _len3 = V.length; _i4 < _len3; _i4++) {
2331
+ var word = V[_i4];
2332
+ result.push((word & 0xff000000) >>> 24, (word & 0xff0000) >>> 16, (word & 0xff00) >>> 8, word & 0xff);
2333
+ }
2334
+ return result;
2335
+ }
2336
+
2337
+ /**
2338
+ * hmac 实现
2339
+ */
2340
+ var blockLen = 64;
2341
+ var iPad = new Uint8Array(blockLen);
2342
+ var oPad = new Uint8Array(blockLen);
2343
+ for (var i = 0; i < blockLen; i++) {
2344
+ iPad[i] = 0x36;
2345
+ oPad[i] = 0x5c;
2346
+ }
2347
+ function hmac(input, key) {
2348
+ // 密钥填充
2349
+ if (key.length > blockLen) key = sm3(key);
2350
+ while (key.length < blockLen) key.push(0);
2351
+ var iPadKey = xor(key, iPad);
2352
+ var oPadKey = xor(key, oPad);
2353
+ var hash = sm3([].concat(_toConsumableArray(iPadKey), _toConsumableArray(input)));
2354
+ return sm3([].concat(_toConsumableArray(oPadKey), _toConsumableArray(hash)));
2355
+ }
2356
+ sm3_1$1 = {
2357
+ sm3: sm3,
2358
+ hmac: hmac
2359
+ };
2360
+ return sm3_1$1;
2361
+ }
2362
+
2363
+ var sm2;
2364
+ var hasRequiredSm2;
2365
+ function requireSm2() {
2366
+ if (hasRequiredSm2) return sm2;
2367
+ hasRequiredSm2 = 1;
2368
+ var _require$$ = requireJsbn(),
2369
+ BigInteger = _require$$.BigInteger;
2370
+ var _require$$2 = requireAsn1(),
2371
+ encodeDer = _require$$2.encodeDer,
2372
+ decodeDer = _require$$2.decodeDer;
2373
+ var _ = requireUtils();
2374
+ var sm3 = requireSm3$1().sm3;
2375
+ var _$generateEcparam = _.generateEcparam(),
2376
+ G = _$generateEcparam.G,
2377
+ curve = _$generateEcparam.curve,
2378
+ n = _$generateEcparam.n;
2379
+ var C1C2C3 = 0;
2380
+
2381
+ /**
2382
+ * 加密
2383
+ */
2384
+ function doEncrypt(msg, publicKey) {
2385
+ var cipherMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
2386
+ msg = typeof msg === 'string' ? _.hexToArray(_.utf8ToHex(msg)) : Array.prototype.slice.call(msg);
2387
+ publicKey = _.getGlobalCurve().decodePointHex(publicKey); // 先将公钥转成点
2388
+
2389
+ var keypair = _.generateKeyPairHex();
2390
+ var k = new BigInteger(keypair.privateKey, 16); // 随机数 k
2391
+
2392
+ // c1 = k * G
2393
+ var c1 = keypair.publicKey;
2394
+ if (c1.length > 128) c1 = c1.substr(c1.length - 128);
2395
+
2396
+ // (x2, y2) = k * publicKey
2397
+ var p = publicKey.multiply(k);
2398
+ var x2 = _.hexToArray(_.leftPad(p.getX().toBigInteger().toRadix(16), 64));
2399
+ var y2 = _.hexToArray(_.leftPad(p.getY().toBigInteger().toRadix(16), 64));
2400
+
2401
+ // c3 = hash(x2 || msg || y2)
2402
+ var c3 = _.arrayToHex(sm3([].concat(x2, msg, y2)));
2403
+ var ct = 1;
2404
+ var offset = 0;
2405
+ var t = []; // 256 位
2406
+ var z = [].concat(x2, y2);
2407
+ var nextT = function nextT() {
2408
+ // (1) Hai = hash(z || ct)
2409
+ // (2) ct++
2410
+ t = sm3([].concat(_toConsumableArray(z), [ct >> 24 & 0x00ff, ct >> 16 & 0x00ff, ct >> 8 & 0x00ff, ct & 0x00ff]));
2411
+ ct++;
2412
+ offset = 0;
2413
+ };
2414
+ nextT(); // 先生成 Ha1
2415
+
2416
+ for (var i = 0, len = msg.length; i < len; i++) {
2417
+ // t = Ha1 || Ha2 || Ha3 || Ha4
2418
+ if (offset === t.length) nextT();
2419
+
2420
+ // c2 = msg ^ t
2421
+ msg[i] ^= t[offset++] & 0xff;
2422
+ }
2423
+ var c2 = _.arrayToHex(msg);
2424
+ return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
2425
+ }
2426
+
2427
+ /**
2428
+ * 解密
2429
+ */
2430
+ function doDecrypt(encryptData, privateKey) {
2431
+ var cipherMode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
2432
+ var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
2433
+ _ref$output = _ref.output,
2434
+ output = _ref$output === void 0 ? 'string' : _ref$output;
2435
+ privateKey = new BigInteger(privateKey, 16);
2436
+ var c3 = encryptData.substr(128, 64);
2437
+ var c2 = encryptData.substr(128 + 64);
2438
+ if (cipherMode === C1C2C3) {
2439
+ c3 = encryptData.substr(encryptData.length - 64);
2440
+ c2 = encryptData.substr(128, encryptData.length - 128 - 64);
2441
+ }
2442
+ var msg = _.hexToArray(c2);
2443
+ var c1 = _.getGlobalCurve().decodePointHex('04' + encryptData.substr(0, 128));
2444
+ var p = c1.multiply(privateKey);
2445
+ var x2 = _.hexToArray(_.leftPad(p.getX().toBigInteger().toRadix(16), 64));
2446
+ var y2 = _.hexToArray(_.leftPad(p.getY().toBigInteger().toRadix(16), 64));
2447
+ var ct = 1;
2448
+ var offset = 0;
2449
+ var t = []; // 256 位
2450
+ var z = [].concat(x2, y2);
2451
+ var nextT = function nextT() {
2452
+ // (1) Hai = hash(z || ct)
2453
+ // (2) ct++
2454
+ t = sm3([].concat(_toConsumableArray(z), [ct >> 24 & 0x00ff, ct >> 16 & 0x00ff, ct >> 8 & 0x00ff, ct & 0x00ff]));
2455
+ ct++;
2456
+ offset = 0;
2457
+ };
2458
+ nextT(); // 先生成 Ha1
2459
+
2460
+ for (var i = 0, len = msg.length; i < len; i++) {
2461
+ // t = Ha1 || Ha2 || Ha3 || Ha4
2462
+ if (offset === t.length) nextT();
2463
+
2464
+ // c2 = msg ^ t
2465
+ msg[i] ^= t[offset++] & 0xff;
2466
+ }
2467
+
2468
+ // c3 = hash(x2 || msg || y2)
2469
+ var checkC3 = _.arrayToHex(sm3([].concat(x2, msg, y2)));
2470
+ if (checkC3 === c3.toLowerCase()) {
2471
+ return output === 'array' ? msg : _.arrayToUtf8(msg);
2472
+ } else {
2473
+ return output === 'array' ? [] : '';
2474
+ }
2475
+ }
2476
+
2477
+ /**
2478
+ * 签名
2479
+ */
2480
+ function doSignature(msg, privateKey) {
2481
+ var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
2482
+ pointPool = _ref2.pointPool,
2483
+ der = _ref2.der,
2484
+ hash = _ref2.hash,
2485
+ publicKey = _ref2.publicKey,
2486
+ userId = _ref2.userId;
2487
+ var hashHex = typeof msg === 'string' ? _.utf8ToHex(msg) : _.arrayToHex(msg);
2488
+ if (hash) {
2489
+ // sm3杂凑
2490
+ publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
2491
+ hashHex = getHash(hashHex, publicKey, userId);
2492
+ }
2493
+ var dA = new BigInteger(privateKey, 16);
2494
+ var e = new BigInteger(hashHex, 16);
2495
+
2496
+ // k
2497
+ var k = null;
2498
+ var r = null;
2499
+ var s = null;
2500
+ do {
2501
+ do {
2502
+ var point = void 0;
2503
+ if (pointPool && pointPool.length) {
2504
+ point = pointPool.pop();
2505
+ } else {
2506
+ point = getPoint();
2507
+ }
2508
+ k = point.k;
2509
+
2510
+ // r = (e + x1) mod n
2511
+ r = e.add(point.x1).mod(n);
2512
+ } while (r.equals(BigInteger.ZERO) || r.add(k).equals(n));
2513
+
2514
+ // s = ((1 + dA)^-1 * (k - r * dA)) mod n
2515
+ s = dA.add(BigInteger.ONE).modInverse(n).multiply(k.subtract(r.multiply(dA))).mod(n);
2516
+ } while (s.equals(BigInteger.ZERO));
2517
+ if (der) return encodeDer(r, s); // asn.1 der 编码
2518
+
2519
+ return _.leftPad(r.toString(16), 64) + _.leftPad(s.toString(16), 64);
2520
+ }
2521
+
2522
+ /**
2523
+ * 验签
2524
+ */
2525
+ function doVerifySignature(msg, signHex, publicKey) {
2526
+ var _ref3 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
2527
+ der = _ref3.der,
2528
+ hash = _ref3.hash,
2529
+ userId = _ref3.userId;
2530
+ var hashHex = typeof msg === 'string' ? _.utf8ToHex(msg) : _.arrayToHex(msg);
2531
+ if (hash) {
2532
+ // sm3杂凑
2533
+ hashHex = getHash(hashHex, publicKey, userId);
2534
+ }
2535
+ var r;
2536
+ var s;
2537
+ if (der) {
2538
+ var decodeDerObj = decodeDer(signHex); // asn.1 der 解码
2539
+ r = decodeDerObj.r;
2540
+ s = decodeDerObj.s;
2541
+ } else {
2542
+ r = new BigInteger(signHex.substring(0, 64), 16);
2543
+ s = new BigInteger(signHex.substring(64), 16);
2544
+ }
2545
+ var PA = curve.decodePointHex(publicKey);
2546
+ var e = new BigInteger(hashHex, 16);
2547
+
2548
+ // t = (r + s) mod n
2549
+ var t = r.add(s).mod(n);
2550
+ if (t.equals(BigInteger.ZERO)) return false;
2551
+
2552
+ // x1y1 = s * G + t * PA
2553
+ var x1y1 = G.multiply(s).add(PA.multiply(t));
2554
+
2555
+ // R = (e + x1) mod n
2556
+ var R = e.add(x1y1.getX().toBigInteger()).mod(n);
2557
+ return r.equals(R);
2558
+ }
2559
+
2560
+ /**
2561
+ * sm3杂凑算法
2562
+ */
2563
+ function getHash(hashHex, publicKey) {
2564
+ var userId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '1234567812345678';
2565
+ // z = hash(entl || userId || a || b || gx || gy || px || py)
2566
+ userId = _.utf8ToHex(userId);
2567
+ var a = _.leftPad(G.curve.a.toBigInteger().toRadix(16), 64);
2568
+ var b = _.leftPad(G.curve.b.toBigInteger().toRadix(16), 64);
2569
+ var gx = _.leftPad(G.getX().toBigInteger().toRadix(16), 64);
2570
+ var gy = _.leftPad(G.getY().toBigInteger().toRadix(16), 64);
2571
+ var px;
2572
+ var py;
2573
+ if (publicKey.length === 128) {
2574
+ px = publicKey.substr(0, 64);
2575
+ py = publicKey.substr(64, 64);
2576
+ } else {
2577
+ var point = G.curve.decodePointHex(publicKey);
2578
+ px = _.leftPad(point.getX().toBigInteger().toRadix(16), 64);
2579
+ py = _.leftPad(point.getY().toBigInteger().toRadix(16), 64);
2580
+ }
2581
+ var data = _.hexToArray(userId + a + b + gx + gy + px + py);
2582
+ var entl = userId.length * 4;
2583
+ data.unshift(entl & 0x00ff);
2584
+ data.unshift(entl >> 8 & 0x00ff);
2585
+ var z = sm3(data);
2586
+
2587
+ // e = hash(z || msg)
2588
+ return _.arrayToHex(sm3(z.concat(_.hexToArray(hashHex))));
2589
+ }
2590
+
2591
+ /**
2592
+ * 计算公钥
2593
+ */
2594
+ function getPublicKeyFromPrivateKey(privateKey) {
2595
+ var PA = G.multiply(new BigInteger(privateKey, 16));
2596
+ var x = _.leftPad(PA.getX().toBigInteger().toString(16), 64);
2597
+ var y = _.leftPad(PA.getY().toBigInteger().toString(16), 64);
2598
+ return '04' + x + y;
2599
+ }
2600
+
2601
+ /**
2602
+ * 获取椭圆曲线点
2603
+ */
2604
+ function getPoint() {
2605
+ var keypair = _.generateKeyPairHex();
2606
+ var PA = curve.decodePointHex(keypair.publicKey);
2607
+ keypair.k = new BigInteger(keypair.privateKey, 16);
2608
+ keypair.x1 = PA.getX().toBigInteger();
2609
+ return keypair;
2610
+ }
2611
+ sm2 = {
2612
+ generateKeyPairHex: _.generateKeyPairHex,
2613
+ compressPublicKeyHex: _.compressPublicKeyHex,
2614
+ comparePublicKeyHex: _.comparePublicKeyHex,
2615
+ doEncrypt: doEncrypt,
2616
+ doDecrypt: doDecrypt,
2617
+ doSignature: doSignature,
2618
+ doVerifySignature: doVerifySignature,
2619
+ getPublicKeyFromPrivateKey: getPublicKeyFromPrivateKey,
2620
+ getPoint: getPoint,
2621
+ verifyPublicKey: _.verifyPublicKey
2622
+ };
2623
+ return sm2;
2624
+ }
2625
+
2626
+ var sm3_1;
2627
+ var hasRequiredSm3;
2628
+ function requireSm3() {
2629
+ if (hasRequiredSm3) return sm3_1;
2630
+ hasRequiredSm3 = 1;
2631
+ var _require$$ = requireSm3$1(),
2632
+ sm3 = _require$$.sm3,
2633
+ hmac = _require$$.hmac;
2634
+
2635
+ /**
2636
+ * 补全16进制字符串
2637
+ */
2638
+ function leftPad(input, num) {
2639
+ if (input.length >= num) return input;
2640
+ return new Array(num - input.length + 1).join('0') + input;
2641
+ }
2642
+
2643
+ /**
2644
+ * 字节数组转 16 进制串
2645
+ */
2646
+ function ArrayToHex(arr) {
2647
+ return arr.map(function (item) {
2648
+ item = item.toString(16);
2649
+ return item.length === 1 ? '0' + item : item;
2650
+ }).join('');
2651
+ }
2652
+
2653
+ /**
2654
+ * 转成字节数组
2655
+ */
2656
+ function hexToArray(hexStr) {
2657
+ var words = [];
2658
+ var hexStrLength = hexStr.length;
2659
+ if (hexStrLength % 2 !== 0) {
2660
+ hexStr = leftPad(hexStr, hexStrLength + 1);
2661
+ }
2662
+ hexStrLength = hexStr.length;
2663
+ for (var i = 0; i < hexStrLength; i += 2) {
2664
+ words.push(parseInt(hexStr.substr(i, 2), 16));
2665
+ }
2666
+ return words;
2667
+ }
2668
+
2669
+ /**
2670
+ * utf8 串转字节数组
2671
+ */
2672
+ function utf8ToArray(str) {
2673
+ var arr = [];
2674
+ for (var i = 0, len = str.length; i < len; i++) {
2675
+ var point = str.codePointAt(i);
2676
+ if (point <= 0x007f) {
2677
+ // 单字节,标量值:00000000 00000000 0zzzzzzz
2678
+ arr.push(point);
2679
+ } else if (point <= 0x07ff) {
2680
+ // 双字节,标量值:00000000 00000yyy yyzzzzzz
2681
+ arr.push(0xc0 | point >>> 6); // 110yyyyy(0xc0-0xdf)
2682
+ arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
2683
+ } else if (point <= 0xD7FF || point >= 0xE000 && point <= 0xFFFF) {
2684
+ // 三字节:标量值:00000000 xxxxyyyy yyzzzzzz
2685
+ arr.push(0xe0 | point >>> 12); // 1110xxxx(0xe0-0xef)
2686
+ arr.push(0x80 | point >>> 6 & 0x3f); // 10yyyyyy(0x80-0xbf)
2687
+ arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
2688
+ } else if (point >= 0x010000 && point <= 0x10FFFF) {
2689
+ // 四字节:标量值:000wwwxx xxxxyyyy yyzzzzzz
2690
+ i++;
2691
+ arr.push(0xf0 | point >>> 18 & 0x1c); // 11110www(0xf0-0xf7)
2692
+ arr.push(0x80 | point >>> 12 & 0x3f); // 10xxxxxx(0x80-0xbf)
2693
+ arr.push(0x80 | point >>> 6 & 0x3f); // 10yyyyyy(0x80-0xbf)
2694
+ arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
2695
+ } else {
2696
+ // 五、六字节,暂时不支持
2697
+ arr.push(point);
2698
+ throw new Error('input is not supported');
2699
+ }
2700
+ }
2701
+ return arr;
2702
+ }
2703
+ sm3_1 = function sm3_1(input, options) {
2704
+ input = typeof input === 'string' ? utf8ToArray(input) : Array.prototype.slice.call(input);
2705
+ if (options) {
2706
+ var mode = options.mode || 'hmac';
2707
+ if (mode !== 'hmac') throw new Error('invalid mode');
2708
+ var key = options.key;
2709
+ if (!key) throw new Error('invalid key');
2710
+ key = typeof key === 'string' ? hexToArray(key) : Array.prototype.slice.call(key);
2711
+ return ArrayToHex(hmac(input, key));
2712
+ }
2713
+ return ArrayToHex(sm3(input));
2714
+ };
2715
+ return sm3_1;
2716
+ }
2717
+
2718
+ var sm4_1;
2719
+ var hasRequiredSm4;
2720
+ function requireSm4() {
2721
+ if (hasRequiredSm4) return sm4_1;
2722
+ hasRequiredSm4 = 1;
2723
+ var DECRYPT = 0;
2724
+ var ROUND = 32;
2725
+ var BLOCK = 16;
2726
+ var Sbox = [0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8, 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35, 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f, 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51, 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48];
2727
+ var CK = [0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279];
2728
+
2729
+ /**
2730
+ * 16 进制串转字节数组
2731
+ */
2732
+ function hexToArray(str) {
2733
+ var arr = [];
2734
+ for (var i = 0, len = str.length; i < len; i += 2) {
2735
+ arr.push(parseInt(str.substr(i, 2), 16));
2736
+ }
2737
+ return arr;
2738
+ }
2739
+
2740
+ /**
2741
+ * 字节数组转 16 进制串
2742
+ */
2743
+ function ArrayToHex(arr) {
2744
+ return arr.map(function (item) {
2745
+ item = item.toString(16);
2746
+ return item.length === 1 ? '0' + item : item;
2747
+ }).join('');
2748
+ }
2749
+
2750
+ /**
2751
+ * utf8 串转字节数组
2752
+ */
2753
+ function utf8ToArray(str) {
2754
+ var arr = [];
2755
+ for (var i = 0, len = str.length; i < len; i++) {
2756
+ var point = str.codePointAt(i);
2757
+ if (point <= 0x007f) {
2758
+ // 单字节,标量值:00000000 00000000 0zzzzzzz
2759
+ arr.push(point);
2760
+ } else if (point <= 0x07ff) {
2761
+ // 双字节,标量值:00000000 00000yyy yyzzzzzz
2762
+ arr.push(0xc0 | point >>> 6); // 110yyyyy(0xc0-0xdf)
2763
+ arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
2764
+ } else if (point <= 0xD7FF || point >= 0xE000 && point <= 0xFFFF) {
2765
+ // 三字节:标量值:00000000 xxxxyyyy yyzzzzzz
2766
+ arr.push(0xe0 | point >>> 12); // 1110xxxx(0xe0-0xef)
2767
+ arr.push(0x80 | point >>> 6 & 0x3f); // 10yyyyyy(0x80-0xbf)
2768
+ arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
2769
+ } else if (point >= 0x010000 && point <= 0x10FFFF) {
2770
+ // 四字节:标量值:000wwwxx xxxxyyyy yyzzzzzz
2771
+ i++;
2772
+ arr.push(0xf0 | point >>> 18 & 0x1c); // 11110www(0xf0-0xf7)
2773
+ arr.push(0x80 | point >>> 12 & 0x3f); // 10xxxxxx(0x80-0xbf)
2774
+ arr.push(0x80 | point >>> 6 & 0x3f); // 10yyyyyy(0x80-0xbf)
2775
+ arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
2776
+ } else {
2777
+ // 五、六字节,暂时不支持
2778
+ arr.push(point);
2779
+ throw new Error('input is not supported');
2780
+ }
2781
+ }
2782
+ return arr;
2783
+ }
2784
+
2785
+ /**
2786
+ * 字节数组转 utf8 串
2787
+ */
2788
+ function arrayToUtf8(arr) {
2789
+ var str = [];
2790
+ for (var i = 0, len = arr.length; i < len; i++) {
2791
+ if (arr[i] >= 0xf0 && arr[i] <= 0xf7) {
2792
+ // 四字节
2793
+ str.push(String.fromCodePoint(((arr[i] & 0x07) << 18) + ((arr[i + 1] & 0x3f) << 12) + ((arr[i + 2] & 0x3f) << 6) + (arr[i + 3] & 0x3f)));
2794
+ i += 3;
2795
+ } else if (arr[i] >= 0xe0 && arr[i] <= 0xef) {
2796
+ // 三字节
2797
+ str.push(String.fromCodePoint(((arr[i] & 0x0f) << 12) + ((arr[i + 1] & 0x3f) << 6) + (arr[i + 2] & 0x3f)));
2798
+ i += 2;
2799
+ } else if (arr[i] >= 0xc0 && arr[i] <= 0xdf) {
2800
+ // 双字节
2801
+ str.push(String.fromCodePoint(((arr[i] & 0x1f) << 6) + (arr[i + 1] & 0x3f)));
2802
+ i++;
2803
+ } else {
2804
+ // 单字节
2805
+ str.push(String.fromCodePoint(arr[i]));
2806
+ }
2807
+ }
2808
+ return str.join('');
2809
+ }
2810
+
2811
+ /**
2812
+ * 32 比特循环左移
2813
+ */
2814
+ function rotl(x, n) {
2815
+ var s = n & 31;
2816
+ return x << s | x >>> 32 - s;
2817
+ }
2818
+
2819
+ /**
2820
+ * 非线性变换
2821
+ */
2822
+ function byteSub(a) {
2823
+ return (Sbox[a >>> 24 & 0xFF] & 0xFF) << 24 | (Sbox[a >>> 16 & 0xFF] & 0xFF) << 16 | (Sbox[a >>> 8 & 0xFF] & 0xFF) << 8 | Sbox[a & 0xFF] & 0xFF;
2824
+ }
2825
+
2826
+ /**
2827
+ * 线性变换,加密/解密用
2828
+ */
2829
+ function l1(b) {
2830
+ return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
2831
+ }
2832
+
2833
+ /**
2834
+ * 线性变换,生成轮密钥用
2835
+ */
2836
+ function l2(b) {
2837
+ return b ^ rotl(b, 13) ^ rotl(b, 23);
2838
+ }
2839
+
2840
+ /**
2841
+ * 以一组 128 比特进行加密/解密操作
2842
+ */
2843
+ function sms4Crypt(input, output, roundKey) {
2844
+ var x = new Array(4);
2845
+
2846
+ // 字节数组转成字数组(此处 1 字 = 32 比特)
2847
+ var tmp = new Array(4);
2848
+ for (var i = 0; i < 4; i++) {
2849
+ tmp[0] = input[4 * i] & 0xff;
2850
+ tmp[1] = input[4 * i + 1] & 0xff;
2851
+ tmp[2] = input[4 * i + 2] & 0xff;
2852
+ tmp[3] = input[4 * i + 3] & 0xff;
2853
+ x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
2854
+ }
2855
+
2856
+ // x[i + 4] = x[i] ^ l1(byteSub(x[i + 1] ^ x[i + 2] ^ x[i + 3] ^ roundKey[i]))
2857
+ for (var r = 0, mid; r < 32; r += 4) {
2858
+ mid = x[1] ^ x[2] ^ x[3] ^ roundKey[r + 0];
2859
+ x[0] ^= l1(byteSub(mid)); // x[4]
2860
+
2861
+ mid = x[2] ^ x[3] ^ x[0] ^ roundKey[r + 1];
2862
+ x[1] ^= l1(byteSub(mid)); // x[5]
2863
+
2864
+ mid = x[3] ^ x[0] ^ x[1] ^ roundKey[r + 2];
2865
+ x[2] ^= l1(byteSub(mid)); // x[6]
2866
+
2867
+ mid = x[0] ^ x[1] ^ x[2] ^ roundKey[r + 3];
2868
+ x[3] ^= l1(byteSub(mid)); // x[7]
2869
+ }
2870
+
2871
+ // 反序变换
2872
+ for (var j = 0; j < 16; j += 4) {
2873
+ output[j] = x[3 - j / 4] >>> 24 & 0xff;
2874
+ output[j + 1] = x[3 - j / 4] >>> 16 & 0xff;
2875
+ output[j + 2] = x[3 - j / 4] >>> 8 & 0xff;
2876
+ output[j + 3] = x[3 - j / 4] & 0xff;
2877
+ }
2878
+ }
2879
+
2880
+ /**
2881
+ * 密钥扩展算法
2882
+ */
2883
+ function sms4KeyExt(key, roundKey, cryptFlag) {
2884
+ var x = new Array(4);
2885
+
2886
+ // 字节数组转成字数组(此处 1 字 = 32 比特)
2887
+ var tmp = new Array(4);
2888
+ for (var i = 0; i < 4; i++) {
2889
+ tmp[0] = key[0 + 4 * i] & 0xff;
2890
+ tmp[1] = key[1 + 4 * i] & 0xff;
2891
+ tmp[2] = key[2 + 4 * i] & 0xff;
2892
+ tmp[3] = key[3 + 4 * i] & 0xff;
2893
+ x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
2894
+ }
2895
+
2896
+ // 与系统参数做异或
2897
+ x[0] ^= 0xa3b1bac6;
2898
+ x[1] ^= 0x56aa3350;
2899
+ x[2] ^= 0x677d9197;
2900
+ x[3] ^= 0xb27022dc;
2901
+
2902
+ // roundKey[i] = x[i + 4] = x[i] ^ l2(byteSub(x[i + 1] ^ x[i + 2] ^ x[i + 3] ^ CK[i]))
2903
+ for (var r = 0, mid; r < 32; r += 4) {
2904
+ mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
2905
+ roundKey[r + 0] = x[0] ^= l2(byteSub(mid)); // x[4]
2906
+
2907
+ mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
2908
+ roundKey[r + 1] = x[1] ^= l2(byteSub(mid)); // x[5]
2909
+
2910
+ mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
2911
+ roundKey[r + 2] = x[2] ^= l2(byteSub(mid)); // x[6]
2912
+
2913
+ mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
2914
+ roundKey[r + 3] = x[3] ^= l2(byteSub(mid)); // x[7]
2915
+ }
2916
+
2917
+ // 解密时使用反序的轮密钥
2918
+ if (cryptFlag === DECRYPT) {
2919
+ for (var _r = 0, _mid; _r < 16; _r++) {
2920
+ _mid = roundKey[_r];
2921
+ roundKey[_r] = roundKey[31 - _r];
2922
+ roundKey[31 - _r] = _mid;
2923
+ }
2924
+ }
2925
+ }
2926
+ function sm4(inArray, key, cryptFlag) {
2927
+ var _ref = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
2928
+ _ref$padding = _ref.padding,
2929
+ padding = _ref$padding === void 0 ? 'pkcs#7' : _ref$padding,
2930
+ mode = _ref.mode,
2931
+ _ref$iv = _ref.iv,
2932
+ iv = _ref$iv === void 0 ? [] : _ref$iv,
2933
+ _ref$output = _ref.output,
2934
+ output = _ref$output === void 0 ? 'string' : _ref$output;
2935
+ if (mode === 'cbc') {
2936
+ // CBC 模式,默认走 ECB 模式
2937
+ if (typeof iv === 'string') iv = hexToArray(iv);
2938
+ if (iv.length !== 128 / 8) {
2939
+ // iv 不是 128 比特
2940
+ throw new Error('iv is invalid');
2941
+ }
2942
+ }
2943
+
2944
+ // 检查 key
2945
+ if (typeof key === 'string') key = hexToArray(key);
2946
+ if (key.length !== 128 / 8) {
2947
+ // key 不是 128 比特
2948
+ throw new Error('key is invalid');
2949
+ }
2950
+
2951
+ // 检查输入
2952
+ if (typeof inArray === 'string') {
2953
+ if (cryptFlag !== DECRYPT) {
2954
+ // 加密,输入为 utf8 串
2955
+ inArray = utf8ToArray(inArray);
2956
+ } else {
2957
+ // 解密,输入为 16 进制串
2958
+ inArray = hexToArray(inArray);
2959
+ }
2960
+ } else {
2961
+ inArray = _toConsumableArray(inArray);
2962
+ }
2963
+
2964
+ // 新增填充,sm4 是 16 个字节一个分组,所以统一走到 pkcs#7
2965
+ if ((padding === 'pkcs#5' || padding === 'pkcs#7') && cryptFlag !== DECRYPT) {
2966
+ var paddingCount = BLOCK - inArray.length % BLOCK;
2967
+ for (var i = 0; i < paddingCount; i++) inArray.push(paddingCount);
2968
+ }
2969
+
2970
+ // 生成轮密钥
2971
+ var roundKey = new Array(ROUND);
2972
+ sms4KeyExt(key, roundKey, cryptFlag);
2973
+ var outArray = [];
2974
+ var lastVector = iv;
2975
+ var restLen = inArray.length;
2976
+ var point = 0;
2977
+ while (restLen >= BLOCK) {
2978
+ var input = inArray.slice(point, point + 16);
2979
+ var _output = new Array(16);
2980
+ if (mode === 'cbc') {
2981
+ for (var _i = 0; _i < BLOCK; _i++) {
2982
+ if (cryptFlag !== DECRYPT) {
2983
+ // 加密过程在组加密前进行异或
2984
+ input[_i] ^= lastVector[_i];
2985
+ }
2986
+ }
2987
+ }
2988
+ sms4Crypt(input, _output, roundKey);
2989
+ for (var _i2 = 0; _i2 < BLOCK; _i2++) {
2990
+ if (mode === 'cbc') {
2991
+ if (cryptFlag === DECRYPT) {
2992
+ // 解密过程在组解密后进行异或
2993
+ _output[_i2] ^= lastVector[_i2];
2994
+ }
2995
+ }
2996
+ outArray[point + _i2] = _output[_i2];
2997
+ }
2998
+ if (mode === 'cbc') {
2999
+ if (cryptFlag !== DECRYPT) {
3000
+ // 使用上一次输出作为加密向量
3001
+ lastVector = _output;
3002
+ } else {
3003
+ // 使用上一次输入作为解密向量
3004
+ lastVector = input;
3005
+ }
3006
+ }
3007
+ restLen -= BLOCK;
3008
+ point += BLOCK;
3009
+ }
3010
+
3011
+ // 去除填充,sm4 是 16 个字节一个分组,所以统一走到 pkcs#7
3012
+ if ((padding === 'pkcs#5' || padding === 'pkcs#7') && cryptFlag === DECRYPT) {
3013
+ var len = outArray.length;
3014
+ var _paddingCount = outArray[len - 1];
3015
+ for (var _i3 = 1; _i3 <= _paddingCount; _i3++) {
3016
+ if (outArray[len - _i3] !== _paddingCount) throw new Error('padding is invalid');
3017
+ }
3018
+ outArray.splice(len - _paddingCount, _paddingCount);
3019
+ }
3020
+
3021
+ // 调整输出
3022
+ if (output !== 'array') {
3023
+ if (cryptFlag !== DECRYPT) {
3024
+ // 加密,输出转 16 进制串
3025
+ return ArrayToHex(outArray);
3026
+ } else {
3027
+ // 解密,输出转 utf8 串
3028
+ return arrayToUtf8(outArray);
3029
+ }
3030
+ } else {
3031
+ return outArray;
3032
+ }
3033
+ }
3034
+ sm4_1 = {
3035
+ encrypt: function encrypt(inArray, key, options) {
3036
+ return sm4(inArray, key, 1, options);
3037
+ },
3038
+ decrypt: function decrypt(inArray, key, options) {
3039
+ return sm4(inArray, key, 0, options);
3040
+ }
3041
+ };
3042
+ return sm4_1;
3043
+ }
3044
+
3045
+ var src;
3046
+ var hasRequiredSrc;
3047
+ function requireSrc() {
3048
+ if (hasRequiredSrc) return src;
3049
+ hasRequiredSrc = 1;
3050
+ src = {
3051
+ sm2: requireSm2(),
3052
+ sm3: requireSm3(),
3053
+ sm4: requireSm4()
3054
+ };
3055
+ return src;
3056
+ }
3057
+
3058
+ var srcExports = requireSrc();
3059
+
3060
+ // globalThis polyfill for IE
3061
+ (function () {
3062
+ if (typeof globalThis === 'undefined') {
3063
+ if (typeof window !== 'undefined') {
3064
+ window.globalThis = window;
3065
+ } else if (typeof global !== 'undefined') {
3066
+ global.globalThis = global;
3067
+ } else if (typeof self !== 'undefined') {
3068
+ self.globalThis = self;
3069
+ } else {
3070
+ this.globalThis = this;
3071
+ }
3072
+ }
3073
+ })();
3074
+
3075
+ // IE浏览器检测(IE11兼容)
3076
+ function isIE() {
3077
+ var ua = window.navigator.userAgent;
3078
+ var msie = ua.indexOf("MSIE ");
3079
+ var trident = ua.indexOf("Trident/");
3080
+ var edge = ua.indexOf("Edge/");
3081
+ return (msie > 0 || trident > 0) && edge < 0;
3082
+ }
3083
+
3084
+ // 兼容的事件监听器添加函数(IE11支持addEventListener)
3085
+ function addEventListenerCompat(element, event, handler) {
3086
+ if (element.addEventListener) {
3087
+ element.addEventListener(event, handler, false);
3088
+ } else if (element.attachEvent) {
3089
+ element.attachEvent("on" + event, handler);
3090
+ }
3091
+ }
3092
+
3093
+ // 兼容的事件监听器移除函数
3094
+ function removeEventListenerCompat(element, event, handler) {
3095
+ if (element.removeEventListener) {
3096
+ element.removeEventListener(event, handler, false);
3097
+ } else if (element.detachEvent) {
3098
+ element.detachEvent("on" + event, handler);
3099
+ }
3100
+ }
3101
+
3102
+ // 兼容的stopImmediatePropagation
3103
+ function stopImmediatePropagationCompat(e) {
3104
+ if (e.stopImmediatePropagation) {
3105
+ e.stopImmediatePropagation();
3106
+ } else {
3107
+ e.stopPropagation();
3108
+ if (e.cancelBubble !== undefined) {
3109
+ e.cancelBubble = true;
3110
+ }
3111
+ }
3112
+ }
3113
+
3114
+ // 兼容的URL解析函数(IE不支持URL构造函数)
3115
+ function getOriginFromUrl(url) {
3116
+ if (!isIE() && window.URL) {
3117
+ try {
3118
+ return new URL(url).origin;
3119
+ } catch (e) {
3120
+ // fallback
3121
+ }
3122
+ }
3123
+ // IE兼容方案:手动解析URL
3124
+ var link = document.createElement("a");
3125
+ link.href = url;
3126
+ return link.protocol + "//" + link.host;
3127
+ }
3128
+
3129
+ // Object.entries polyfill(IE11不支持)
3130
+ function objectEntries(obj) {
3131
+ if (Object.entries) {
3132
+ return Object.entries(obj);
3133
+ }
3134
+ // IE11兼容方案
3135
+ var keys = Object.keys(obj);
3136
+ var result = [];
3137
+ for (var i = 0; i < keys.length; i++) {
3138
+ result.push([keys[i], obj[keys[i]]]);
3139
+ }
3140
+ return result;
3141
+ }
3142
+
3143
+ // 兼容的样式设置(userSelect)
3144
+ function setUserSelect(element, value) {
3145
+ if (isIE()) {
3146
+ element.style.msUserSelect = value;
3147
+ element.style.userSelect = value;
3148
+ } else {
3149
+ element.style.userSelect = value;
3150
+ }
3151
+ }
3152
+
3153
+ // 兼容的cursor设置(IE不支持grabbing)
3154
+ function setCursor(element, value) {
3155
+ if (isIE() && value === "grabbing") {
3156
+ element.style.cursor = "move";
3157
+ } else {
3158
+ element.style.cursor = value;
3159
+ }
3160
+ }
3161
+ var DEFAULT_WIDTH = 80;
3162
+ var DEFAULT_HEIGHT = 80;
3163
+ var DEFAULT_RIGHT = 40;
3164
+ var DEFAULT_BOTTOM = 40;
3165
+ var bindDraggable = function bindDraggable(instance, dom, callback) {
3166
+ var isDragging = false;
3167
+ var moved = false;
3168
+ var onMouseMove = function onMouseMove(e) {
3169
+ if (!isDragging) return;
3170
+ moved = true;
3171
+ var rect = dom.getBoundingClientRect();
3172
+ var widthDis = e.clientX - rect.width / 2;
3173
+ var heightDis = e.clientY - rect.height / 2;
3174
+ widthDis = widthDis < 0 ? 0 : widthDis;
3175
+ heightDis = heightDis < 0 ? 0 : heightDis;
3176
+ instance.container.style.left = widthDis + "px";
3177
+ instance.container.style.top = heightDis + "px";
3178
+ instance.left = widthDis;
3179
+ instance.top = e.clientY - heightDis;
3180
+ };
3181
+ var _onMouseUp = function onMouseUp(e) {
3182
+ setUserSelect(document.body, "");
3183
+ if (!isDragging) return;
3184
+ isDragging = false;
3185
+ setCursor(dom, "pointer");
3186
+ removeEventListenerCompat(document, "mousemove", onMouseMove);
3187
+ removeEventListenerCompat(document, "mouseup", _onMouseUp);
3188
+ };
3189
+ addEventListenerCompat(dom, "mousedown", function (e) {
3190
+ setUserSelect(document.body, "none");
3191
+ isDragging = true;
3192
+ moved = false;
3193
+ var rect = dom.getBoundingClientRect ? dom.getBoundingClientRect() : {
3194
+ width: dom.offsetWidth || 0,
3195
+ height: dom.offsetHeight || 0,
3196
+ left: dom.offsetLeft || 0,
3197
+ top: dom.offsetTop || 0
3198
+ };
3199
+ e.clientX - (rect.left + rect.width / 2);
3200
+ e.clientY - (rect.top + rect.height / 2);
3201
+ e.clientX;
3202
+ e.clientY;
3203
+ instance.container.style.left = e.clientX - rect.width / 2 + "px";
3204
+ instance.container.style.top = e.clientY - rect.height / 2 + "px";
3205
+ instance.top = e.clientY - rect.height / 2;
3206
+ instance.left = e.clientX - rect.width / 2;
3207
+ setCursor(dom, "grabbing");
3208
+ addEventListenerCompat(document, "mousemove", onMouseMove);
3209
+ addEventListenerCompat(document, "mouseup", _onMouseUp);
3210
+ });
3211
+ addEventListenerCompat(dom, "click", function (e) {
3212
+ if (moved) {
3213
+ stopImmediatePropagationCompat(e);
3214
+ if (e.preventDefault) {
3215
+ e.preventDefault();
3216
+ } else {
3217
+ e.returnValue = false;
3218
+ }
3219
+ moved = false;
3220
+ } else {
3221
+ if (callback) {
3222
+ callback();
3223
+ }
3224
+ }
3225
+ });
3226
+ };
3227
+
3228
+ // 客服SDK类(使用函数构造函数以兼容IE)
3229
+ function WkCustomerServiceSDK() {
3230
+ this.options = {};
3231
+ this.container = null;
3232
+ this.iframe = null;
3233
+ this.overlay = null;
3234
+ this.eventListeners = {};
3235
+ }
3236
+
3237
+ // DOM ready 兼容函数(IE11兼容)
3238
+ function domReady(fn) {
3239
+ if (document.readyState === 'complete' || document.readyState === 'interactive') {
3240
+ setTimeout(fn, 1);
3241
+ } else {
3242
+ document.addEventListener('DOMContentLoaded', fn, false);
3243
+ }
3244
+ }
3245
+
3246
+ // 获取body元素的兼容函数
3247
+ function getBody() {
3248
+ if (document.body) {
3249
+ return document.body;
3250
+ }
3251
+ if (document.getElementsByTagName('body')[0]) {
3252
+ return document.getElementsByTagName('body')[0];
3253
+ }
3254
+ return null;
3255
+ }
3256
+
3257
+ // 初始化iframe
3258
+ WkCustomerServiceSDK.prototype.init = function (options) {
3259
+ var self = this;
3260
+ this.options = options;
3261
+ var initContainer = function initContainer() {
3262
+ var body = getBody();
3263
+ if (!body) {
3264
+ return;
3265
+ }
3266
+ var isLoadCus = document.getElementById("wk-ai-customer-container");
3267
+ if (isLoadCus) {
3268
+ return;
3269
+ }
3270
+
3271
+ // 计算位置
3272
+ var innerWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 1024;
3273
+ var innerHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 768;
3274
+ self.options.height = options.height || DEFAULT_HEIGHT;
3275
+ self.options.width = options.width || DEFAULT_WIDTH;
3276
+ self.left = innerWidth - self.options.width - DEFAULT_RIGHT;
3277
+ self.top = innerHeight - self.options.height - DEFAULT_BOTTOM;
3278
+ if (options.right !== undefined && options.right > 0) {
3279
+ self.left = innerWidth - options.right - self.options.width;
3280
+ } else if (options.left !== undefined && options.left >= 0) {
3281
+ self.left = options.left;
3282
+ }
3283
+ if (options.bottom !== undefined && options.bottom > 0) {
3284
+ self.top = innerHeight - options.bottom - self.options.height;
3285
+ } else if (options.top !== undefined && options.top >= 0) {
3286
+ self.top = options.top;
3287
+ }
3288
+
3289
+ // 确保容器在可见区域内
3290
+ if (self.left < 0) self.left = 10;
3291
+ if (self.top < 0) self.top = 10;
3292
+ if (self.left + self.options.width > innerWidth) {
3293
+ self.left = innerWidth - self.options.width - 10;
3294
+ }
3295
+ if (self.top + self.options.height > innerHeight) {
3296
+ self.top = innerHeight - self.options.height - 10;
3297
+ }
3298
+
3299
+ // IE中如果位置超出屏幕,强制设置为可见位置
3300
+ if (isIE()) {
3301
+ if (self.left > innerWidth - 100) {
3302
+ self.left = innerWidth - self.options.width - 20;
3303
+ }
3304
+ if (self.top > innerHeight - 100) {
3305
+ self.top = innerHeight - self.options.height - 20;
3306
+ }
3307
+ // 确保至少有一部分在可见区域内
3308
+ if (self.left < 0) self.left = 100;
3309
+ if (self.top < 0) self.top = 100;
3310
+ }
3311
+
3312
+ // 创建容器 - 使用最简单可靠的方式
3313
+ self.container = document.createElement("div");
3314
+ self.container.id = "wk-ai-customer-container";
3315
+
3316
+ // 设置样式 - 使用fixed定位,确保IE兼容
3317
+ self.container.style.position = "fixed";
3318
+ self.container.style.width = self.options.width + "px";
3319
+ self.container.style.height = self.options.height + "px";
3320
+ self.container.style.top = self.top + "px";
3321
+ self.container.style.left = self.left + "px";
3322
+ self.container.style.display = "block";
3323
+ self.container.style.visibility = "visible";
3324
+ self.container.style.zIndex = "999999";
3325
+ self.container.style.overflow = "hidden";
3326
+ self.container.style.margin = "0";
3327
+ self.container.style.padding = "0";
3328
+ // 确保容器有背景色,即使iframe还没加载也能看到
3329
+ self.container.style.backgroundColor = "transparent";
3330
+
3331
+ // IE特定处理
3332
+ if (isIE()) {
3333
+ self.container.style.filter = "alpha(opacity=100)";
3334
+ self.container.style.zoom = "1";
3335
+ // IE中确保容器有最小尺寸
3336
+ self.container.style.minWidth = self.options.width + "px";
3337
+ self.container.style.minHeight = self.options.height + "px";
3338
+ // IE中空容器可能不显示,添加一个占位元素
3339
+ var placeholder = document.createElement("div");
3340
+ placeholder.style.cssText = "width:100%;height:100%;position:absolute;top:0;left:0;background-color:#f0f0f0;z-index:1;";
3341
+ self.container.appendChild(placeholder);
3342
+ }
3343
+
3344
+ // 添加到body
3345
+ body.appendChild(self.container);
3346
+
3347
+ // IE兼容:强制重绘
3348
+ if (isIE()) {
3349
+ self.container.style.display = "none";
3350
+ self.container.offsetHeight; // 触发重排
3351
+ self.container.style.display = "block";
3352
+ }
3353
+
3354
+ // 创建iframe
3355
+ try {
3356
+ var baseUrl = self.options.baseUrl || "https://ai.vcolco.com/chat/";
3357
+ var queryParams = self.buildQueryParams();
3358
+ self.iframe = document.createElement("iframe");
3359
+ self.iframe.src = baseUrl + "?" + queryParams;
3360
+ self.iframe.width = "100%";
3361
+ self.iframe.height = "100%";
3362
+ self.iframe.frameBorder = "0";
3363
+ self.iframe.scrolling = "no";
3364
+ self.iframe.style.border = "none";
3365
+ self.iframe.style.position = "absolute";
3366
+ self.iframe.style.zIndex = "2024";
3367
+ self.iframe.style.left = "0";
3368
+ self.iframe.style.top = "0";
3369
+ self.iframe.style.width = "100%";
3370
+ self.iframe.style.height = "100%";
3371
+ self.iframe.style.backgroundColor = "#ffffff";
3372
+ self.container.appendChild(self.iframe);
3373
+
3374
+ // IE兼容:iframe加载后移除占位元素
3375
+ if (isIE()) {
3376
+ addEventListenerCompat(self.iframe, "load", function () {
3377
+ var placeholder = self.container.querySelector("div[style*='background-color:#f0f0f0']");
3378
+ if (placeholder && placeholder.parentNode) {
3379
+ placeholder.parentNode.removeChild(placeholder);
3380
+ }
3381
+ });
3382
+ }
3383
+ } catch (e) {
3384
+ // 如果创建iframe失败,至少容器已经显示了
3385
+ }
3386
+
3387
+ // 创建覆盖元素
3388
+ self.overlay = document.createElement("div");
3389
+ self.overlay.style.position = "absolute";
3390
+ self.overlay.style.width = "100%";
3391
+ self.overlay.style.height = "100%";
3392
+ self.overlay.style.cursor = "pointer";
3393
+ self.overlay.style.opacity = "0";
3394
+ self.overlay.style.zIndex = "2025";
3395
+ self.overlay.style.left = "0";
3396
+ self.overlay.style.top = "0";
3397
+ self.container.appendChild(self.overlay);
3398
+ bindDraggable(self, self.overlay, function () {
3399
+ self.container.style.width = "500px";
3400
+ self.container.style.height = "100%";
3401
+ self.container.style.right = "0";
3402
+ self.container.style.top = "0";
3403
+ self.container.style.bottom = "auto";
3404
+ self.container.style.left = "auto";
3405
+ self.container.style.position = "fixed";
3406
+ self.container.style.zIndex = "999999";
3407
+ self.overlay.style.zIndex = "2024";
3408
+ if (self.iframe) {
3409
+ self.iframe.style.zIndex = "2025";
3410
+ }
3411
+ self.postMessage({
3412
+ type: "clickIcon"
3413
+ });
3414
+ });
3415
+
3416
+ // 设置消息监听
3417
+ var boundHandleMessage = function boundHandleMessage(event) {
3418
+ self.handleMessage(event);
3419
+ };
3420
+ self._boundHandleMessage = boundHandleMessage;
3421
+ addEventListenerCompat(window, "message", boundHandleMessage);
3422
+
3423
+ // 发送初始化消息
3424
+ if (self.iframe) {
3425
+ self.postMessage({
3426
+ type: "init"
3427
+ });
3428
+ }
3429
+ };
3430
+
3431
+ // 等待DOM ready后初始化
3432
+ var _tryInit = function tryInit() {
3433
+ try {
3434
+ var body = getBody();
3435
+ if (!body) {
3436
+ setTimeout(_tryInit, 50);
3437
+ return;
3438
+ }
3439
+ initContainer();
3440
+ } catch (err) {
3441
+ // 静默失败
3442
+ }
3443
+ };
3444
+
3445
+ // 立即尝试一次
3446
+ _tryInit();
3447
+
3448
+ // 也使用domReady作为备用
3449
+ domReady(function () {
3450
+ try {
3451
+ var body = getBody();
3452
+ if (!body) {
3453
+ setTimeout(function () {
3454
+ body = getBody();
3455
+ if (body) {
3456
+ initContainer();
3457
+ }
3458
+ }, 100);
3459
+ return;
3460
+ }
3461
+ if (!document.getElementById("wk-ai-customer-container")) {
3462
+ initContainer();
3463
+ }
3464
+ } catch (e) {
3465
+ // 静默失败
3466
+ }
3467
+ });
3468
+ };
3469
+
3470
+ // 构建URL查询参数
3471
+ WkCustomerServiceSDK.prototype.buildQueryParams = function () {
3472
+ var Enterprise = this.options.Enterprise;
3473
+ var Externalid = this.options.Externalid;
3474
+ var Role = this.options.Role;
3475
+ var Userid = this.options.Userid;
3476
+ var Username = this.options.Username;
3477
+ var Knowledge = this.options.Knowledge;
3478
+ var Privatekey = this.options.Privatekey;
3479
+ var Usetype = this.options.Usetype;
3480
+ if (!Enterprise || !Externalid || !Role || !Userid || !Username) {
3481
+ throw new Error("Missing required options: Enterprise, Externalid, Role, Userid, Username");
3482
+ }
3483
+ var params = {
3484
+ Enterprise: Enterprise,
3485
+ Externalid: Externalid,
3486
+ Knowledge: Knowledge || "",
3487
+ Role: Role,
3488
+ Userid: Userid,
3489
+ Username: Username
3490
+ };
3491
+ if (!params.Knowledge) {
3492
+ delete params.Knowledge;
3493
+ }
3494
+ var entries = objectEntries(params);
3495
+ var signString = entries.map(function (entry) {
3496
+ return entry[0] + "=" + entry[1];
3497
+ }).join("&");
3498
+ params.Sign = srcExports.sm2.doSignature(signString, Privatekey, {
3499
+ der: true,
3500
+ hash: true
3501
+ });
3502
+ if (Usetype) {
3503
+ params.Usetype = Usetype;
3504
+ }
3505
+ params.Iframe = "PC";
3506
+ var allEntries = objectEntries(params);
3507
+ return allEntries.map(function (entry) {
3508
+ return entry[0] + "=" + entry[1];
3509
+ }).join("&");
3510
+ };
3511
+
3512
+ // 处理来自iframe的消息
3513
+ WkCustomerServiceSDK.prototype.handleMessage = function (event) {
3514
+ var baseUrl = this.options.baseUrl || "https://ai.vcolco.com/chat/";
3515
+ var origin = getOriginFromUrl(baseUrl);
3516
+ if (event.origin !== origin) return;
3517
+ if (event.data && event.data.type && this.eventListeners[event.data.type]) {
3518
+ var callbacks = this.eventListeners[event.data.type];
3519
+ for (var i = 0; i < callbacks.length; i++) {
3520
+ callbacks[i](event.data);
3521
+ }
3522
+ }
3523
+ if (event.data && event.data.type) {
3524
+ switch (event.data.type) {
3525
+ case "showChat":
3526
+ break;
3527
+ case "hideChat":
3528
+ this.container.style.width = this.options.width + "px";
3529
+ this.container.style.height = this.options.height + "px";
3530
+ this.container.style.top = this.top + "px";
3531
+ this.container.style.left = this.left + "px";
3532
+ this.container.style.bottom = "auto";
3533
+ this.container.style.right = "auto";
3534
+ this.container.style.position = "fixed";
3535
+ this.overlay.style.zIndex = "2025";
3536
+ if (this.iframe) {
3537
+ this.iframe.style.zIndex = "2024";
3538
+ }
3539
+ break;
3540
+ }
3541
+ }
3542
+ };
3543
+
3544
+ // 向iframe发送消息
3545
+ WkCustomerServiceSDK.prototype.postMessage = function (message) {
3546
+ if (this.iframe && this.iframe.contentWindow) {
3547
+ var targetOrigin = this.options.baseUrl || "https://ai.vcolco.com/chat/";
3548
+ this.iframe.contentWindow.postMessage(message, targetOrigin);
3549
+ }
3550
+ };
3551
+
3552
+ // 注册事件监听器
3553
+ WkCustomerServiceSDK.prototype.on = function (eventType, callback) {
3554
+ if (!this.eventListeners[eventType]) {
3555
+ this.eventListeners[eventType] = [];
3556
+ }
3557
+ this.eventListeners[eventType].push(callback);
3558
+ return this;
3559
+ };
3560
+
3561
+ // 移除事件监听器
3562
+ WkCustomerServiceSDK.prototype.off = function (eventType, callback) {};
3563
+
3564
+ // 显示客服窗口
3565
+ WkCustomerServiceSDK.prototype.show = function () {};
3566
+
3567
+ // 隐藏客服窗口
3568
+ WkCustomerServiceSDK.prototype.hide = function () {};
3569
+
3570
+ // 销毁实例
3571
+ WkCustomerServiceSDK.prototype.destroy = function () {
3572
+ if (this._boundHandleMessage) {
3573
+ removeEventListenerCompat(window, "message", this._boundHandleMessage);
3574
+ }
3575
+ var container = document.getElementById("wk-ai-customer-container");
3576
+ if (container && container.parentNode) {
3577
+ container.parentNode.removeChild(container);
3578
+ }
3579
+ };
3580
+
3581
+ // 创建SDK实例
3582
+ var wkAiCustomerService = new WkCustomerServiceSDK();
3583
+
3584
+ export { wkAiCustomerService as default };