sm-crypto-v2 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,1292 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/sm2/index.ts
8
+ var sm2_exports = {};
9
+ __export(sm2_exports, {
10
+ arrayToHex: () => arrayToHex,
11
+ arrayToUtf8: () => arrayToUtf8,
12
+ comparePublicKeyHex: () => comparePublicKeyHex,
13
+ compressPublicKeyHex: () => compressPublicKeyHex,
14
+ concatArray: () => concatArray,
15
+ doDecrypt: () => doDecrypt,
16
+ doEncrypt: () => doEncrypt,
17
+ doSignature: () => doSignature,
18
+ doVerifySignature: () => doVerifySignature,
19
+ generateEcparam: () => generateEcparam,
20
+ generateKeyPairHex: () => generateKeyPairHex,
21
+ getGlobalCurve: () => getGlobalCurve,
22
+ getHash: () => getHash,
23
+ getPoint: () => getPoint,
24
+ getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
25
+ hexToArray: () => hexToArray,
26
+ leftPad: () => leftPad,
27
+ utf8ToHex: () => utf8ToHex,
28
+ verifyPublicKey: () => verifyPublicKey
29
+ });
30
+ import { BigInteger as BigInteger4 } from "jsbn";
31
+
32
+ // src/sm2/asn1.ts
33
+ import { BigInteger } from "jsbn";
34
+ function bigintToValue(bigint) {
35
+ let h = bigint.toString(16);
36
+ if (h[0] !== "-") {
37
+ if (h.length % 2 === 1)
38
+ h = "0" + h;
39
+ else if (!h.match(/^[0-7]/))
40
+ h = "00" + h;
41
+ } else {
42
+ h = h.substr(1);
43
+ let len = h.length;
44
+ if (len % 2 === 1)
45
+ len += 1;
46
+ else if (!h.match(/^[0-7]/))
47
+ len += 2;
48
+ let maskString = "";
49
+ for (let i = 0; i < len; i++)
50
+ maskString += "f";
51
+ let mask = new BigInteger(maskString, 16);
52
+ let output = mask.xor(bigint).add(BigInteger.ONE);
53
+ h = output.toString(16).replace(/^-/, "");
54
+ }
55
+ return h;
56
+ }
57
+ var ASN1Object = class {
58
+ constructor(tlv = null, t = "00", l = "00", v = "") {
59
+ this.tlv = tlv;
60
+ this.t = t;
61
+ this.l = l;
62
+ this.v = v;
63
+ }
64
+ getEncodedHex() {
65
+ if (!this.tlv) {
66
+ this.v = this.getValue();
67
+ this.l = this.getLength();
68
+ this.tlv = this.t + this.l + this.v;
69
+ }
70
+ return this.tlv;
71
+ }
72
+ getLength() {
73
+ const n3 = this.v.length / 2;
74
+ let nHex = n3.toString(16);
75
+ if (nHex.length % 2 === 1)
76
+ nHex = "0" + nHex;
77
+ if (n3 < 128) {
78
+ return nHex;
79
+ } else {
80
+ const head = 128 + nHex.length / 2;
81
+ return head.toString(16) + nHex;
82
+ }
83
+ }
84
+ getValue() {
85
+ return "";
86
+ }
87
+ };
88
+ var DERInteger = class extends ASN1Object {
89
+ constructor(bigint) {
90
+ super();
91
+ this.t = "02";
92
+ if (bigint)
93
+ this.v = bigintToValue(bigint);
94
+ }
95
+ getValue() {
96
+ return this.v;
97
+ }
98
+ };
99
+ var DERSequence = class extends ASN1Object {
100
+ constructor(asn1Array) {
101
+ super();
102
+ this.asn1Array = asn1Array;
103
+ }
104
+ t = "30";
105
+ getValue() {
106
+ this.v = this.asn1Array.map((asn1Object) => asn1Object.getEncodedHex()).join("");
107
+ return this.v;
108
+ }
109
+ };
110
+ function getLenOfL(str, start) {
111
+ if (+str[start + 2] < 8)
112
+ return 1;
113
+ return +str.substring(start + 2, start + 4) & 127 + 1;
114
+ }
115
+ function getL(str, start) {
116
+ const len = getLenOfL(str, start);
117
+ const l = str.substring(start + 2, start + 2 + len * 2);
118
+ if (!l)
119
+ return -1;
120
+ const bigint = +l[0] < 8 ? new BigInteger(l, 16) : new BigInteger(l.substring(2), 16);
121
+ return bigint.intValue();
122
+ }
123
+ function getStartOfV(str, start) {
124
+ const len = getLenOfL(str, start);
125
+ return start + (len + 1) * 2;
126
+ }
127
+ function encodeDer(r, s) {
128
+ const derR = new DERInteger(r);
129
+ const derS = new DERInteger(s);
130
+ const derSeq = new DERSequence([derR, derS]);
131
+ return derSeq.getEncodedHex();
132
+ }
133
+ function decodeDer(input) {
134
+ const start = getStartOfV(input, 0);
135
+ const vIndexR = getStartOfV(input, start);
136
+ const lR = getL(input, start);
137
+ const vR = input.substr(vIndexR, lR * 2);
138
+ const nextStart = vIndexR + vR.length;
139
+ const vIndexS = getStartOfV(input, nextStart);
140
+ const lS = getL(input, nextStart);
141
+ const vS = input.substring(vIndexS, vIndexS + lS * 2);
142
+ const r = new BigInteger(vR, 16);
143
+ const s = new BigInteger(vS, 16);
144
+ return { r, s };
145
+ }
146
+
147
+ // src/sm2/utils.ts
148
+ import { BigInteger as BigInteger3, SecureRandom } from "jsbn";
149
+
150
+ // src/sm2/ec.ts
151
+ import { BigInteger as BigInteger2 } from "jsbn";
152
+ var TWO = new BigInteger2("2");
153
+ var THREE = new BigInteger2("3");
154
+ var ECFieldElementFp = class {
155
+ constructor(q, x) {
156
+ this.q = q;
157
+ this.x = x;
158
+ }
159
+ equals(other) {
160
+ if (other === this)
161
+ return true;
162
+ return this.q.equals(other.q) && this.x.equals(other.x);
163
+ }
164
+ toBigInteger() {
165
+ return this.x;
166
+ }
167
+ negate() {
168
+ return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
169
+ }
170
+ add(b) {
171
+ return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
172
+ }
173
+ subtract(b) {
174
+ return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
175
+ }
176
+ multiply(b) {
177
+ return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
178
+ }
179
+ divide(b) {
180
+ return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
181
+ }
182
+ square() {
183
+ return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
184
+ }
185
+ };
186
+ var ECPointFp = class {
187
+ constructor(curve3, x, y, z) {
188
+ this.curve = curve3;
189
+ this.x = x;
190
+ this.y = y;
191
+ this.z = z == null ? BigInteger2.ONE : z;
192
+ this.zinv = null;
193
+ }
194
+ zinv;
195
+ z;
196
+ getX() {
197
+ if (this.zinv === null)
198
+ this.zinv = this.z.modInverse(this.curve.q);
199
+ return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
200
+ }
201
+ getY() {
202
+ if (this.zinv === null)
203
+ this.zinv = this.z.modInverse(this.curve.q);
204
+ return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
205
+ }
206
+ equals(other) {
207
+ if (other === this)
208
+ return true;
209
+ if (this.isInfinity())
210
+ return other.isInfinity();
211
+ if (other.isInfinity())
212
+ return this.isInfinity();
213
+ const u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
214
+ if (!u.equals(BigInteger2.ZERO))
215
+ return false;
216
+ const v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
217
+ return v.equals(BigInteger2.ZERO);
218
+ }
219
+ isInfinity() {
220
+ if (this.x === null && this.y === null)
221
+ return true;
222
+ return this.z.equals(BigInteger2.ZERO) && !this.y.toBigInteger().equals(BigInteger2.ZERO);
223
+ }
224
+ negate() {
225
+ return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
226
+ }
227
+ add(b) {
228
+ if (this.isInfinity())
229
+ return b;
230
+ if (b.isInfinity())
231
+ return this;
232
+ const x1 = this.x.toBigInteger();
233
+ const y1 = this.y.toBigInteger();
234
+ const z1 = this.z;
235
+ const x2 = b.x.toBigInteger();
236
+ const y2 = b.y.toBigInteger();
237
+ const z2 = b.z;
238
+ const q = this.curve.q;
239
+ const w1 = x1.multiply(z2).mod(q);
240
+ const w2 = x2.multiply(z1).mod(q);
241
+ const w3 = w1.subtract(w2);
242
+ const w4 = y1.multiply(z2).mod(q);
243
+ const w5 = y2.multiply(z1).mod(q);
244
+ const w6 = w4.subtract(w5);
245
+ if (BigInteger2.ZERO.equals(w3)) {
246
+ if (BigInteger2.ZERO.equals(w6)) {
247
+ return this.twice();
248
+ }
249
+ return this.curve.infinity;
250
+ }
251
+ const w7 = w1.add(w2);
252
+ const w8 = z1.multiply(z2).mod(q);
253
+ const w9 = w3.square().mod(q);
254
+ const w10 = w3.multiply(w9).mod(q);
255
+ const w11 = w8.multiply(w6.square()).subtract(w7.multiply(w9)).mod(q);
256
+ const x3 = w3.multiply(w11).mod(q);
257
+ const y3 = w6.multiply(w9.multiply(w1).subtract(w11)).subtract(w4.multiply(w10)).mod(q);
258
+ const z3 = w10.multiply(w8).mod(q);
259
+ return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
260
+ }
261
+ twice() {
262
+ if (this.isInfinity())
263
+ return this;
264
+ if (!this.y.toBigInteger().signum())
265
+ return this.curve.infinity;
266
+ const x1 = this.x.toBigInteger();
267
+ const y1 = this.y.toBigInteger();
268
+ const z1 = this.z;
269
+ const q = this.curve.q;
270
+ const a = this.curve.a.toBigInteger();
271
+ const w1 = x1.square().multiply(THREE).add(a.multiply(z1.square())).mod(q);
272
+ const w2 = y1.shiftLeft(1).multiply(z1).mod(q);
273
+ const w3 = y1.square().mod(q);
274
+ const w4 = w3.multiply(x1).multiply(z1).mod(q);
275
+ const w5 = w2.square().mod(q);
276
+ const w6 = w1.square().subtract(w4.shiftLeft(3)).mod(q);
277
+ const x3 = w2.multiply(w6).mod(q);
278
+ const y3 = w1.multiply(w4.shiftLeft(2).subtract(w6)).subtract(w5.shiftLeft(1).multiply(w3)).mod(q);
279
+ const z3 = w2.multiply(w5).mod(q);
280
+ return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
281
+ }
282
+ multiply(k) {
283
+ if (this.isInfinity())
284
+ return this;
285
+ if (!k.signum())
286
+ return this.curve.infinity;
287
+ const k3 = k.multiply(THREE);
288
+ const neg = this.negate();
289
+ let Q = this;
290
+ for (let i = k3.bitLength() - 2; i > 0; i--) {
291
+ Q = Q.twice();
292
+ const k3Bit = k3.testBit(i);
293
+ const kBit = k.testBit(i);
294
+ if (k3Bit !== kBit) {
295
+ Q = Q.add(k3Bit ? this : neg);
296
+ }
297
+ }
298
+ return Q;
299
+ }
300
+ };
301
+ var ECCurveFp = class {
302
+ constructor(q, a, b) {
303
+ this.q = q;
304
+ this.q = q;
305
+ this.a = this.fromBigInteger(a);
306
+ this.b = this.fromBigInteger(b);
307
+ this.infinity = new ECPointFp(this, null, null);
308
+ }
309
+ infinity;
310
+ a;
311
+ b;
312
+ equals(other) {
313
+ if (other === this)
314
+ return true;
315
+ return this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b);
316
+ }
317
+ fromBigInteger(x) {
318
+ return new ECFieldElementFp(this.q, x);
319
+ }
320
+ decodePointHex(s) {
321
+ switch (parseInt(s.substring(0, 2), 16)) {
322
+ case 0:
323
+ return this.infinity;
324
+ case 2:
325
+ case 3:
326
+ const x = this.fromBigInteger(new BigInteger2(s.substring(2), 16));
327
+ let y = this.fromBigInteger(x.multiply(x.square()).add(
328
+ x.multiply(this.a)
329
+ ).add(this.b).toBigInteger().modPow(
330
+ this.q.divide(new BigInteger2("4")).add(BigInteger2.ONE),
331
+ this.q
332
+ ));
333
+ if (!y.toBigInteger().mod(TWO).equals(new BigInteger2(s.substring(0, 2), 16).subtract(TWO))) {
334
+ y = y.negate();
335
+ }
336
+ return new ECPointFp(this, x, y);
337
+ case 4:
338
+ case 6:
339
+ case 7:
340
+ const len = (s.length - 2) / 2;
341
+ const xHex = s.substring(2, 2 + len);
342
+ const yHex = s.substring(len + 2, len + 2 + len);
343
+ return new ECPointFp(this, this.fromBigInteger(new BigInteger2(xHex, 16)), this.fromBigInteger(new BigInteger2(yHex, 16)));
344
+ default:
345
+ return null;
346
+ }
347
+ }
348
+ };
349
+
350
+ // src/sm2/utils.ts
351
+ var rng = new SecureRandom();
352
+ var { curve, G, n } = generateEcparam();
353
+ function getGlobalCurve() {
354
+ return curve;
355
+ }
356
+ function generateEcparam() {
357
+ const p = new BigInteger3("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16);
358
+ const a = new BigInteger3("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16);
359
+ const b = new BigInteger3("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16);
360
+ const curve3 = new ECCurveFp(p, a, b);
361
+ const gxHex = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7";
362
+ const gyHex = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0";
363
+ const G3 = curve3.decodePointHex("04" + gxHex + gyHex);
364
+ const n3 = new BigInteger3("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16);
365
+ return { curve: curve3, G: G3, n: n3 };
366
+ }
367
+ function generateKeyPairHex(a, b, c) {
368
+ const random = typeof a === "string" ? new BigInteger3(a, b) : a ? new BigInteger3(a, b, c) : new BigInteger3(n.bitLength(), rng);
369
+ const d = random.mod(n.subtract(BigInteger3.ONE)).add(BigInteger3.ONE);
370
+ const privateKey = leftPad(d.toString(16), 64);
371
+ const P = G.multiply(d);
372
+ const Px = leftPad(P.getX().toBigInteger().toString(16), 64);
373
+ const Py = leftPad(P.getY().toBigInteger().toString(16), 64);
374
+ const publicKey = "04" + Px + Py;
375
+ return { privateKey, publicKey };
376
+ }
377
+ function compressPublicKeyHex(s) {
378
+ if (s.length !== 130)
379
+ throw new Error("Invalid public key to compress");
380
+ const len = (s.length - 2) / 2;
381
+ const xHex = s.substring(2, 2 + len);
382
+ const y = new BigInteger3(s.substring(len + 2, len + len + 2), 16);
383
+ let prefix = "03";
384
+ if (y.mod(new BigInteger3("2")).equals(BigInteger3.ZERO))
385
+ prefix = "02";
386
+ return prefix + xHex;
387
+ }
388
+ function utf8ToHex(input) {
389
+ input = decodeURIComponent(encodeURIComponent(input));
390
+ const length = input.length;
391
+ const words = new Uint32Array((length >>> 2) + 1);
392
+ for (let i = 0; i < length; i++) {
393
+ words[i >>> 2] |= (input.charCodeAt(i) & 255) << 24 - i % 4 * 8;
394
+ }
395
+ const hexChars = [];
396
+ for (let i = 0; i < length; i++) {
397
+ const bite = words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
398
+ hexChars.push((bite >>> 4).toString(16));
399
+ hexChars.push((bite & 15).toString(16));
400
+ }
401
+ return hexChars.join("");
402
+ }
403
+ function leftPad(input, num) {
404
+ if (input.length >= num)
405
+ return input;
406
+ return new Array(num - input.length + 1).join("0") + input;
407
+ }
408
+ function arrayToHex(arr) {
409
+ return arr.map((item) => {
410
+ const hex = item.toString(16);
411
+ return hex.length === 1 ? "0" + hex : hex;
412
+ }).join("");
413
+ }
414
+ function arrayToUtf8(arr) {
415
+ const str = [];
416
+ for (let i = 0, len = arr.length; i < len; i++) {
417
+ if (arr[i] >= 240 && arr[i] <= 247) {
418
+ str.push(String.fromCodePoint(((arr[i] & 7) << 18) + ((arr[i + 1] & 63) << 12) + ((arr[i + 2] & 63) << 6) + (arr[i + 3] & 63)));
419
+ i += 3;
420
+ } else if (arr[i] >= 224 && arr[i] <= 239) {
421
+ str.push(String.fromCodePoint(((arr[i] & 15) << 12) + ((arr[i + 1] & 63) << 6) + (arr[i + 2] & 63)));
422
+ i += 2;
423
+ } else if (arr[i] >= 192 && arr[i] <= 223) {
424
+ str.push(String.fromCodePoint(((arr[i] & 31) << 6) + (arr[i + 1] & 63)));
425
+ i++;
426
+ } else {
427
+ str.push(String.fromCodePoint(arr[i]));
428
+ }
429
+ }
430
+ return str.join("");
431
+ }
432
+ function hexToArray(hexStr) {
433
+ let hexStrLength = hexStr.length;
434
+ if (hexStrLength % 2 !== 0) {
435
+ hexStr = leftPad(hexStr, hexStrLength + 1);
436
+ }
437
+ hexStrLength = hexStr.length;
438
+ const wordLength = hexStrLength / 2;
439
+ const words = new Uint8Array(wordLength);
440
+ for (let i = 0; i < wordLength; i++) {
441
+ words[i] = parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
442
+ }
443
+ return words;
444
+ }
445
+ function verifyPublicKey(publicKey) {
446
+ const point = curve.decodePointHex(publicKey);
447
+ if (!point)
448
+ return false;
449
+ const x = point.getX();
450
+ const y = point.getY();
451
+ return y.square().equals(x.multiply(x.square()).add(x.multiply(curve.a)).add(curve.b));
452
+ }
453
+ function comparePublicKeyHex(publicKey1, publicKey2) {
454
+ const point1 = curve.decodePointHex(publicKey1);
455
+ if (!point1)
456
+ return false;
457
+ const point2 = curve.decodePointHex(publicKey2);
458
+ if (!point2)
459
+ return false;
460
+ return point1.equals(point2);
461
+ }
462
+ function concatArray(...arrays) {
463
+ let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
464
+ if (!arrays.length)
465
+ return new Uint8Array();
466
+ let result = new Uint8Array(totalLength);
467
+ let length = 0;
468
+ for (let array of arrays) {
469
+ result.set(array, length);
470
+ length += array.length;
471
+ }
472
+ return result;
473
+ }
474
+
475
+ // src/sm2/sm3.ts
476
+ var W = new Uint32Array(68);
477
+ var M = new Uint32Array(64);
478
+ function rotl(x, n3) {
479
+ const s = n3 & 31;
480
+ return x << s | x >>> 32 - s;
481
+ }
482
+ function xor(x, y) {
483
+ const result = new Uint8Array(x.length);
484
+ for (let i = x.length - 1; i >= 0; i--)
485
+ result[i] = (x[i] ^ y[i]) & 255;
486
+ return result;
487
+ }
488
+ function P0(X) {
489
+ return X ^ rotl(X, 9) ^ rotl(X, 17);
490
+ }
491
+ function P1(X) {
492
+ return X ^ rotl(X, 15) ^ rotl(X, 23);
493
+ }
494
+ function sm3(array) {
495
+ let len = array.length * 8;
496
+ let k = len % 512;
497
+ k = k >= 448 ? 512 - k % 448 - 1 : 448 - k - 1;
498
+ const kArr = new Array((k - 7) / 8);
499
+ const lenArr = new Array(8);
500
+ for (let i = 0, len2 = kArr.length; i < len2; i++)
501
+ kArr[i] = 0;
502
+ for (let i = 0, len2 = lenArr.length; i < len2; i++)
503
+ lenArr[i] = 0;
504
+ let lenString = len.toString(2);
505
+ for (let i = 7; i >= 0; i--) {
506
+ if (lenString.length > 8) {
507
+ const start = lenString.length - 8;
508
+ lenArr[i] = parseInt(lenString.substring(start), 2);
509
+ lenString = lenString.substring(0, start);
510
+ } else if (lenString.length > 0) {
511
+ lenArr[i] = parseInt(lenString, 2);
512
+ lenString = "";
513
+ }
514
+ }
515
+ const m = Uint8Array.from([...array, 128, ...kArr, ...lenArr]);
516
+ const dataView = new DataView(m.buffer, 0);
517
+ const n3 = m.length / 64;
518
+ const V = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
519
+ for (let i = 0; i < n3; i++) {
520
+ W.fill(0);
521
+ M.fill(0);
522
+ const start = 16 * i;
523
+ for (let j = 0; j < 16; j++) {
524
+ W[j] = dataView.getUint32((start + j) * 4, false);
525
+ }
526
+ for (let j = 16; j < 68; j++) {
527
+ W[j] = P1(W[j - 16] ^ W[j - 9] ^ rotl(W[j - 3], 15)) ^ rotl(W[j - 13], 7) ^ W[j - 6];
528
+ }
529
+ for (let j = 0; j < 64; j++) {
530
+ M[j] = W[j] ^ W[j + 4];
531
+ }
532
+ const T1 = 2043430169;
533
+ const T2 = 2055708042;
534
+ let A = V[0];
535
+ let B = V[1];
536
+ let C = V[2];
537
+ let D = V[3];
538
+ let E = V[4];
539
+ let F = V[5];
540
+ let G3 = V[6];
541
+ let H = V[7];
542
+ let SS1;
543
+ let SS2;
544
+ let TT1;
545
+ let TT2;
546
+ let T;
547
+ for (let j = 0; j < 64; j++) {
548
+ T = j >= 0 && j <= 15 ? T1 : T2;
549
+ SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
550
+ SS2 = SS1 ^ rotl(A, 12);
551
+ TT1 = (j >= 0 && j <= 15 ? A ^ B ^ C : A & B | A & C | B & C) + D + SS2 + M[j];
552
+ TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G3 : E & F | ~E & G3) + H + SS1 + W[j];
553
+ D = C;
554
+ C = rotl(B, 9);
555
+ B = A;
556
+ A = TT1;
557
+ H = G3;
558
+ G3 = rotl(F, 19);
559
+ F = E;
560
+ E = P0(TT2);
561
+ }
562
+ V[0] ^= A;
563
+ V[1] ^= B;
564
+ V[2] ^= C;
565
+ V[3] ^= D;
566
+ V[4] ^= E;
567
+ V[5] ^= F;
568
+ V[6] ^= G3;
569
+ V[7] ^= H;
570
+ }
571
+ const result = new Uint8Array(V.length * 4);
572
+ for (let i = 0, len2 = V.length; i < len2; i++) {
573
+ const word = V[i];
574
+ result[i * 4] = (word & 4278190080) >>> 24;
575
+ result[i * 4 + 1] = (word & 16711680) >>> 16;
576
+ result[i * 4 + 2] = (word & 65280) >>> 8;
577
+ result[i * 4 + 3] = word & 255;
578
+ }
579
+ return result;
580
+ }
581
+ var blockLen = 64;
582
+ var iPad = new Uint8Array(blockLen);
583
+ var oPad = new Uint8Array(blockLen);
584
+ for (let i = 0; i < blockLen; i++) {
585
+ iPad[i] = 54;
586
+ oPad[i] = 92;
587
+ }
588
+ function hmac(input, key) {
589
+ if (key.length > blockLen)
590
+ key = sm3(key);
591
+ while (key.length < blockLen) {
592
+ const padKey = new Uint8Array(blockLen);
593
+ padKey.set(key);
594
+ key = padKey;
595
+ }
596
+ const iPadKey = xor(key, iPad);
597
+ const oPadKey = xor(key, oPad);
598
+ const hash = sm3(concatArray(iPadKey, input));
599
+ return sm3(concatArray(oPadKey, hash));
600
+ }
601
+
602
+ // src/sm2/index.ts
603
+ var { G: G2, curve: curve2, n: n2 } = generateEcparam();
604
+ var C1C2C3 = 0;
605
+ function doEncrypt(msg, publicKey, cipherMode = 1) {
606
+ const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
607
+ const publicKeyPoint = getGlobalCurve().decodePointHex(publicKey);
608
+ const keypair = generateKeyPairHex();
609
+ const k = new BigInteger4(keypair.privateKey, 16);
610
+ let c1 = keypair.publicKey;
611
+ if (c1.length > 128)
612
+ c1 = c1.substring(c1.length - 128);
613
+ const p = publicKeyPoint.multiply(k);
614
+ const x2 = hexToArray(leftPad(p.getX().toBigInteger().toRadix(16), 64));
615
+ const y2 = hexToArray(leftPad(p.getY().toBigInteger().toRadix(16), 64));
616
+ const c3 = arrayToHex(Array.from(sm3(concatArray(x2, msgArr, y2))));
617
+ let ct = 1;
618
+ let offset = 0;
619
+ let t = new Uint8Array();
620
+ const z = concatArray(x2, y2);
621
+ const nextT = () => {
622
+ t = sm3(Uint8Array.from([...z, ct >> 24 & 255, ct >> 16 & 255, ct >> 8 & 255, ct & 255]));
623
+ ct++;
624
+ offset = 0;
625
+ };
626
+ nextT();
627
+ for (let i = 0, len = msgArr.length; i < len; i++) {
628
+ if (offset === t.length)
629
+ nextT();
630
+ msgArr[i] ^= t[offset++] & 255;
631
+ }
632
+ const c2 = arrayToHex(Array.from(msgArr));
633
+ return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
634
+ }
635
+ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
636
+ output = "string"
637
+ } = {}) {
638
+ const privateKeyInteger = new BigInteger4(privateKey, 16);
639
+ let c3 = encryptData.substring(128, 128 + 64);
640
+ let c2 = encryptData.substring(128 + 64);
641
+ if (cipherMode === C1C2C3) {
642
+ c3 = encryptData.substring(encryptData.length - 64);
643
+ c2 = encryptData.substring(128, encryptData.length - 64);
644
+ }
645
+ const msg = hexToArray(c2);
646
+ const c1 = getGlobalCurve().decodePointHex("04" + encryptData.substring(0, 128));
647
+ const p = c1.multiply(privateKeyInteger);
648
+ const x2 = hexToArray(leftPad(p.getX().toBigInteger().toRadix(16), 64));
649
+ const y2 = hexToArray(leftPad(p.getY().toBigInteger().toRadix(16), 64));
650
+ let ct = 1;
651
+ let offset = 0;
652
+ let t = new Uint8Array();
653
+ const z = concatArray(x2, y2);
654
+ const nextT = () => {
655
+ t = sm3(Uint8Array.from([...z, ct >> 24 & 255, ct >> 16 & 255, ct >> 8 & 255, ct & 255]));
656
+ ct++;
657
+ offset = 0;
658
+ };
659
+ nextT();
660
+ for (let i = 0, len = msg.length; i < len; i++) {
661
+ if (offset === t.length)
662
+ nextT();
663
+ msg[i] ^= t[offset++] & 255;
664
+ }
665
+ const checkC3 = arrayToHex(Array.from(sm3(concatArray(x2, msg, y2))));
666
+ if (checkC3 === c3.toLowerCase()) {
667
+ return output === "array" ? msg : arrayToUtf8(msg);
668
+ } else {
669
+ return output === "array" ? [] : "";
670
+ }
671
+ }
672
+ function doSignature(msg, privateKey, options = {}) {
673
+ let {
674
+ pointPool,
675
+ der,
676
+ hash,
677
+ publicKey,
678
+ userId
679
+ } = options;
680
+ let hashHex = typeof msg === "string" ? utf8ToHex(msg) : arrayToHex(Array.from(msg));
681
+ if (hash) {
682
+ publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
683
+ hashHex = getHash(hashHex, publicKey, userId);
684
+ }
685
+ const dA = new BigInteger4(privateKey, 16);
686
+ const e = new BigInteger4(hashHex, 16);
687
+ let k = null;
688
+ let r = null;
689
+ let s = null;
690
+ do {
691
+ do {
692
+ let point;
693
+ if (pointPool && pointPool.length) {
694
+ point = pointPool.pop();
695
+ } else {
696
+ point = getPoint();
697
+ }
698
+ k = point.k;
699
+ r = e.add(point.x1).mod(n2);
700
+ } while (r.equals(BigInteger4.ZERO) || r.add(k).equals(n2));
701
+ s = dA.add(BigInteger4.ONE).modInverse(n2).multiply(k.subtract(r.multiply(dA))).mod(n2);
702
+ } while (s.equals(BigInteger4.ZERO));
703
+ if (der)
704
+ return encodeDer(r, s);
705
+ return leftPad(r.toString(16), 64) + leftPad(s.toString(16), 64);
706
+ }
707
+ function doVerifySignature(msg, signHex, publicKey, options = {}) {
708
+ let hashHex;
709
+ const {
710
+ hash,
711
+ der,
712
+ userId
713
+ } = options;
714
+ if (hash) {
715
+ hashHex = getHash(typeof msg === "string" ? utf8ToHex(msg) : msg, publicKey, userId);
716
+ } else {
717
+ hashHex = typeof msg === "string" ? utf8ToHex(msg) : arrayToHex(Array.from(msg));
718
+ }
719
+ let r;
720
+ let s;
721
+ if (der) {
722
+ const decodeDerObj = decodeDer(signHex);
723
+ r = decodeDerObj.r;
724
+ s = decodeDerObj.s;
725
+ } else {
726
+ r = new BigInteger4(signHex.substring(0, 64), 16);
727
+ s = new BigInteger4(signHex.substring(64), 16);
728
+ }
729
+ const PA = curve2.decodePointHex(publicKey);
730
+ const e = new BigInteger4(hashHex, 16);
731
+ const t = r.add(s).mod(n2);
732
+ if (t.equals(BigInteger4.ZERO))
733
+ return false;
734
+ const x1y1 = G2.multiply(s).add(PA.multiply(t));
735
+ const R = e.add(x1y1.getX().toBigInteger()).mod(n2);
736
+ return r.equals(R);
737
+ }
738
+ function getHash(hashHex, publicKey, userId = "1234567812345678") {
739
+ userId = utf8ToHex(userId);
740
+ const a = leftPad(G2.curve.a.toBigInteger().toRadix(16), 64);
741
+ const b = leftPad(G2.curve.b.toBigInteger().toRadix(16), 64);
742
+ const gx = leftPad(G2.getX().toBigInteger().toRadix(16), 64);
743
+ const gy = leftPad(G2.getY().toBigInteger().toRadix(16), 64);
744
+ let px;
745
+ let py;
746
+ if (publicKey.length === 128) {
747
+ px = publicKey.substring(0, 64);
748
+ py = publicKey.substring(64, 128);
749
+ } else {
750
+ const point = G2.curve.decodePointHex(publicKey);
751
+ px = leftPad(point.getX().toBigInteger().toRadix(16), 64);
752
+ py = leftPad(point.getY().toBigInteger().toRadix(16), 64);
753
+ }
754
+ const data = hexToArray(userId + a + b + gx + gy + px + py);
755
+ const entl = userId.length * 4;
756
+ const z = sm3(concatArray(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
757
+ return arrayToHex(Array.from(sm3(concatArray(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex))));
758
+ }
759
+ function getPublicKeyFromPrivateKey(privateKey) {
760
+ const PA = G2.multiply(new BigInteger4(privateKey, 16));
761
+ const x = leftPad(PA.getX().toBigInteger().toString(16), 64);
762
+ const y = leftPad(PA.getY().toBigInteger().toString(16), 64);
763
+ return "04" + x + y;
764
+ }
765
+ function getPoint() {
766
+ const keypair = generateKeyPairHex();
767
+ const PA = curve2.decodePointHex(keypair.publicKey);
768
+ return {
769
+ ...keypair,
770
+ k: new BigInteger4(keypair.privateKey, 16),
771
+ x1: PA.getX().toBigInteger()
772
+ };
773
+ }
774
+
775
+ // src/sm3/index.ts
776
+ var sm3_exports = {};
777
+ __export(sm3_exports, {
778
+ sm3: () => sm32,
779
+ utf8ToArray: () => utf8ToArray
780
+ });
781
+ function utf8ToArray(str) {
782
+ const arr = [];
783
+ for (let i = 0, len = str.length; i < len; i++) {
784
+ const point = str.codePointAt(i);
785
+ if (point <= 127) {
786
+ arr.push(point);
787
+ } else if (point <= 2047) {
788
+ arr.push(192 | point >>> 6);
789
+ arr.push(128 | point & 63);
790
+ } else if (point <= 55295 || point >= 57344 && point <= 65535) {
791
+ arr.push(224 | point >>> 12);
792
+ arr.push(128 | point >>> 6 & 63);
793
+ arr.push(128 | point & 63);
794
+ } else if (point >= 65536 && point <= 1114111) {
795
+ i++;
796
+ arr.push(240 | point >>> 18 & 28);
797
+ arr.push(128 | point >>> 12 & 63);
798
+ arr.push(128 | point >>> 6 & 63);
799
+ arr.push(128 | point & 63);
800
+ } else {
801
+ arr.push(point);
802
+ throw new Error("input is not supported");
803
+ }
804
+ }
805
+ return new Uint8Array(arr);
806
+ }
807
+ function sm32(input, options) {
808
+ input = typeof input === "string" ? utf8ToArray(input) : input;
809
+ if (options) {
810
+ const mode = options.mode || "hmac";
811
+ if (mode !== "hmac")
812
+ throw new Error("invalid mode");
813
+ let key = options.key;
814
+ if (!key)
815
+ throw new Error("invalid key");
816
+ key = typeof key === "string" ? hexToArray(key) : key;
817
+ return arrayToHex(Array.from(hmac(input, key)));
818
+ }
819
+ return arrayToHex(Array.from(sm3(input)));
820
+ }
821
+
822
+ // src/sm4/index.ts
823
+ var sm4_exports = {};
824
+ __export(sm4_exports, {
825
+ decrypt: () => decrypt,
826
+ encrypt: () => encrypt,
827
+ sm4: () => sm4
828
+ });
829
+ var DECRYPT = 0;
830
+ var ROUND = 32;
831
+ var BLOCK = 16;
832
+ var Sbox = Uint8Array.from([
833
+ 214,
834
+ 144,
835
+ 233,
836
+ 254,
837
+ 204,
838
+ 225,
839
+ 61,
840
+ 183,
841
+ 22,
842
+ 182,
843
+ 20,
844
+ 194,
845
+ 40,
846
+ 251,
847
+ 44,
848
+ 5,
849
+ 43,
850
+ 103,
851
+ 154,
852
+ 118,
853
+ 42,
854
+ 190,
855
+ 4,
856
+ 195,
857
+ 170,
858
+ 68,
859
+ 19,
860
+ 38,
861
+ 73,
862
+ 134,
863
+ 6,
864
+ 153,
865
+ 156,
866
+ 66,
867
+ 80,
868
+ 244,
869
+ 145,
870
+ 239,
871
+ 152,
872
+ 122,
873
+ 51,
874
+ 84,
875
+ 11,
876
+ 67,
877
+ 237,
878
+ 207,
879
+ 172,
880
+ 98,
881
+ 228,
882
+ 179,
883
+ 28,
884
+ 169,
885
+ 201,
886
+ 8,
887
+ 232,
888
+ 149,
889
+ 128,
890
+ 223,
891
+ 148,
892
+ 250,
893
+ 117,
894
+ 143,
895
+ 63,
896
+ 166,
897
+ 71,
898
+ 7,
899
+ 167,
900
+ 252,
901
+ 243,
902
+ 115,
903
+ 23,
904
+ 186,
905
+ 131,
906
+ 89,
907
+ 60,
908
+ 25,
909
+ 230,
910
+ 133,
911
+ 79,
912
+ 168,
913
+ 104,
914
+ 107,
915
+ 129,
916
+ 178,
917
+ 113,
918
+ 100,
919
+ 218,
920
+ 139,
921
+ 248,
922
+ 235,
923
+ 15,
924
+ 75,
925
+ 112,
926
+ 86,
927
+ 157,
928
+ 53,
929
+ 30,
930
+ 36,
931
+ 14,
932
+ 94,
933
+ 99,
934
+ 88,
935
+ 209,
936
+ 162,
937
+ 37,
938
+ 34,
939
+ 124,
940
+ 59,
941
+ 1,
942
+ 33,
943
+ 120,
944
+ 135,
945
+ 212,
946
+ 0,
947
+ 70,
948
+ 87,
949
+ 159,
950
+ 211,
951
+ 39,
952
+ 82,
953
+ 76,
954
+ 54,
955
+ 2,
956
+ 231,
957
+ 160,
958
+ 196,
959
+ 200,
960
+ 158,
961
+ 234,
962
+ 191,
963
+ 138,
964
+ 210,
965
+ 64,
966
+ 199,
967
+ 56,
968
+ 181,
969
+ 163,
970
+ 247,
971
+ 242,
972
+ 206,
973
+ 249,
974
+ 97,
975
+ 21,
976
+ 161,
977
+ 224,
978
+ 174,
979
+ 93,
980
+ 164,
981
+ 155,
982
+ 52,
983
+ 26,
984
+ 85,
985
+ 173,
986
+ 147,
987
+ 50,
988
+ 48,
989
+ 245,
990
+ 140,
991
+ 177,
992
+ 227,
993
+ 29,
994
+ 246,
995
+ 226,
996
+ 46,
997
+ 130,
998
+ 102,
999
+ 202,
1000
+ 96,
1001
+ 192,
1002
+ 41,
1003
+ 35,
1004
+ 171,
1005
+ 13,
1006
+ 83,
1007
+ 78,
1008
+ 111,
1009
+ 213,
1010
+ 219,
1011
+ 55,
1012
+ 69,
1013
+ 222,
1014
+ 253,
1015
+ 142,
1016
+ 47,
1017
+ 3,
1018
+ 255,
1019
+ 106,
1020
+ 114,
1021
+ 109,
1022
+ 108,
1023
+ 91,
1024
+ 81,
1025
+ 141,
1026
+ 27,
1027
+ 175,
1028
+ 146,
1029
+ 187,
1030
+ 221,
1031
+ 188,
1032
+ 127,
1033
+ 17,
1034
+ 217,
1035
+ 92,
1036
+ 65,
1037
+ 31,
1038
+ 16,
1039
+ 90,
1040
+ 216,
1041
+ 10,
1042
+ 193,
1043
+ 49,
1044
+ 136,
1045
+ 165,
1046
+ 205,
1047
+ 123,
1048
+ 189,
1049
+ 45,
1050
+ 116,
1051
+ 208,
1052
+ 18,
1053
+ 184,
1054
+ 229,
1055
+ 180,
1056
+ 176,
1057
+ 137,
1058
+ 105,
1059
+ 151,
1060
+ 74,
1061
+ 12,
1062
+ 150,
1063
+ 119,
1064
+ 126,
1065
+ 101,
1066
+ 185,
1067
+ 241,
1068
+ 9,
1069
+ 197,
1070
+ 110,
1071
+ 198,
1072
+ 132,
1073
+ 24,
1074
+ 240,
1075
+ 125,
1076
+ 236,
1077
+ 58,
1078
+ 220,
1079
+ 77,
1080
+ 32,
1081
+ 121,
1082
+ 238,
1083
+ 95,
1084
+ 62,
1085
+ 215,
1086
+ 203,
1087
+ 57,
1088
+ 72
1089
+ ]);
1090
+ var CK = new Uint32Array([
1091
+ 462357,
1092
+ 472066609,
1093
+ 943670861,
1094
+ 1415275113,
1095
+ 1886879365,
1096
+ 2358483617,
1097
+ 2830087869,
1098
+ 3301692121,
1099
+ 3773296373,
1100
+ 4228057617,
1101
+ 404694573,
1102
+ 876298825,
1103
+ 1347903077,
1104
+ 1819507329,
1105
+ 2291111581,
1106
+ 2762715833,
1107
+ 3234320085,
1108
+ 3705924337,
1109
+ 4177462797,
1110
+ 337322537,
1111
+ 808926789,
1112
+ 1280531041,
1113
+ 1752135293,
1114
+ 2223739545,
1115
+ 2695343797,
1116
+ 3166948049,
1117
+ 3638552301,
1118
+ 4110090761,
1119
+ 269950501,
1120
+ 741554753,
1121
+ 1213159005,
1122
+ 1684763257
1123
+ ]);
1124
+ function byteSub(a) {
1125
+ return (Sbox[a >>> 24 & 255] & 255) << 24 | (Sbox[a >>> 16 & 255] & 255) << 16 | (Sbox[a >>> 8 & 255] & 255) << 8 | Sbox[a & 255] & 255;
1126
+ }
1127
+ function l1(b) {
1128
+ return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
1129
+ }
1130
+ function l2(b) {
1131
+ return b ^ rotl(b, 13) ^ rotl(b, 23);
1132
+ }
1133
+ function sms4Crypt(input, output, roundKey) {
1134
+ const x = new Uint32Array(4);
1135
+ const tmp = new Uint32Array(4);
1136
+ for (let i = 0; i < 4; i++) {
1137
+ tmp[0] = input[4 * i] & 255;
1138
+ tmp[1] = input[4 * i + 1] & 255;
1139
+ tmp[2] = input[4 * i + 2] & 255;
1140
+ tmp[3] = input[4 * i + 3] & 255;
1141
+ x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
1142
+ }
1143
+ for (let r = 0, mid; r < 32; r += 4) {
1144
+ mid = x[1] ^ x[2] ^ x[3] ^ roundKey[r + 0];
1145
+ x[0] ^= l1(byteSub(mid));
1146
+ mid = x[2] ^ x[3] ^ x[0] ^ roundKey[r + 1];
1147
+ x[1] ^= l1(byteSub(mid));
1148
+ mid = x[3] ^ x[0] ^ x[1] ^ roundKey[r + 2];
1149
+ x[2] ^= l1(byteSub(mid));
1150
+ mid = x[0] ^ x[1] ^ x[2] ^ roundKey[r + 3];
1151
+ x[3] ^= l1(byteSub(mid));
1152
+ }
1153
+ for (let j = 0; j < 16; j += 4) {
1154
+ output[j] = x[3 - j / 4] >>> 24 & 255;
1155
+ output[j + 1] = x[3 - j / 4] >>> 16 & 255;
1156
+ output[j + 2] = x[3 - j / 4] >>> 8 & 255;
1157
+ output[j + 3] = x[3 - j / 4] & 255;
1158
+ }
1159
+ }
1160
+ function sms4KeyExt(key, roundKey, cryptFlag) {
1161
+ const x = new Uint32Array(4);
1162
+ const tmp = new Uint32Array(4);
1163
+ for (let i = 0; i < 4; i++) {
1164
+ tmp[0] = key[0 + 4 * i] & 255;
1165
+ tmp[1] = key[1 + 4 * i] & 255;
1166
+ tmp[2] = key[2 + 4 * i] & 255;
1167
+ tmp[3] = key[3 + 4 * i] & 255;
1168
+ x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
1169
+ }
1170
+ x[0] ^= 2746333894;
1171
+ x[1] ^= 1453994832;
1172
+ x[2] ^= 1736282519;
1173
+ x[3] ^= 2993693404;
1174
+ for (let r = 0, mid; r < 32; r += 4) {
1175
+ mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
1176
+ roundKey[r + 0] = x[0] ^= l2(byteSub(mid));
1177
+ mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
1178
+ roundKey[r + 1] = x[1] ^= l2(byteSub(mid));
1179
+ mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
1180
+ roundKey[r + 2] = x[2] ^= l2(byteSub(mid));
1181
+ mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
1182
+ roundKey[r + 3] = x[3] ^= l2(byteSub(mid));
1183
+ }
1184
+ if (cryptFlag === DECRYPT) {
1185
+ for (let r = 0, mid; r < 16; r++) {
1186
+ mid = roundKey[r];
1187
+ roundKey[r] = roundKey[31 - r];
1188
+ roundKey[31 - r] = mid;
1189
+ }
1190
+ }
1191
+ }
1192
+ function sm4(inArray, key, cryptFlag, options = {}) {
1193
+ let {
1194
+ padding = "pkcs#7",
1195
+ mode,
1196
+ iv = new Uint8Array(16),
1197
+ output
1198
+ } = options;
1199
+ if (mode === "cbc") {
1200
+ if (typeof iv === "string")
1201
+ iv = hexToArray(iv);
1202
+ if (iv.length !== 128 / 8) {
1203
+ throw new Error("iv is invalid");
1204
+ }
1205
+ }
1206
+ if (typeof key === "string")
1207
+ key = hexToArray(key);
1208
+ if (key.length !== 128 / 8) {
1209
+ throw new Error("key is invalid");
1210
+ }
1211
+ if (typeof inArray === "string") {
1212
+ if (cryptFlag !== DECRYPT) {
1213
+ inArray = utf8ToArray(inArray);
1214
+ } else {
1215
+ inArray = hexToArray(inArray);
1216
+ }
1217
+ } else {
1218
+ inArray = Uint8Array.from(inArray);
1219
+ }
1220
+ if ((padding === "pkcs#5" || padding === "pkcs#7") && cryptFlag !== DECRYPT) {
1221
+ const paddingCount = BLOCK - inArray.length % BLOCK;
1222
+ const newArray = new Uint8Array(inArray.length + paddingCount);
1223
+ newArray.set(inArray, 0);
1224
+ for (let i = 0; i < paddingCount; i++)
1225
+ newArray[inArray.length + i] = paddingCount;
1226
+ inArray = newArray;
1227
+ }
1228
+ const roundKey = new Uint32Array(ROUND);
1229
+ sms4KeyExt(key, roundKey, cryptFlag);
1230
+ let outArray = new Uint8Array(inArray.length);
1231
+ let lastVector = iv;
1232
+ let restLen = inArray.length;
1233
+ let point = 0;
1234
+ while (restLen >= BLOCK) {
1235
+ const input = inArray.slice(point, point + 16);
1236
+ const output2 = new Uint8Array(16);
1237
+ if (mode === "cbc") {
1238
+ for (let i = 0; i < BLOCK; i++) {
1239
+ if (cryptFlag !== DECRYPT) {
1240
+ input[i] ^= lastVector[i];
1241
+ }
1242
+ }
1243
+ }
1244
+ sms4Crypt(input, output2, roundKey);
1245
+ for (let i = 0; i < BLOCK; i++) {
1246
+ if (mode === "cbc") {
1247
+ if (cryptFlag === DECRYPT) {
1248
+ output2[i] ^= lastVector[i];
1249
+ }
1250
+ }
1251
+ outArray[point + i] = output2[i];
1252
+ }
1253
+ if (mode === "cbc") {
1254
+ if (cryptFlag !== DECRYPT) {
1255
+ lastVector = output2;
1256
+ } else {
1257
+ lastVector = input;
1258
+ }
1259
+ }
1260
+ restLen -= BLOCK;
1261
+ point += BLOCK;
1262
+ }
1263
+ if ((padding === "pkcs#5" || padding === "pkcs#7") && cryptFlag === DECRYPT) {
1264
+ const len = outArray.length;
1265
+ const paddingCount = outArray[len - 1];
1266
+ for (let i = 1; i <= paddingCount; i++) {
1267
+ if (outArray[len - i] !== paddingCount)
1268
+ throw new Error("padding is invalid");
1269
+ }
1270
+ outArray = outArray.slice(0, len - paddingCount);
1271
+ }
1272
+ if (output !== "array") {
1273
+ if (cryptFlag !== DECRYPT) {
1274
+ return arrayToHex(Array.from(outArray));
1275
+ } else {
1276
+ return arrayToUtf8(outArray);
1277
+ }
1278
+ } else {
1279
+ return outArray;
1280
+ }
1281
+ }
1282
+ function encrypt(inArray, key, options = {}) {
1283
+ return sm4(inArray, key, 1, options);
1284
+ }
1285
+ function decrypt(inArray, key, options = {}) {
1286
+ return sm4(inArray, key, 0, options);
1287
+ }
1288
+ export {
1289
+ sm2_exports as sm2,
1290
+ sm3_exports as sm3,
1291
+ sm4_exports as sm4
1292
+ };