sm-crypto-v2 0.3.13 → 1.2.2

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.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,7 +17,11 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __toESM = (mod4, isNodeMode, target) => (target = mod4 != null ? __create(__getProtoOf(mod4)) : {}, __copyProps(
21
+ isNodeMode || !mod4 || !mod4.__esModule ? __defProp(target, "default", { value: mod4, enumerable: true }) : target,
22
+ mod4
23
+ ));
24
+ var __toCommonJS = (mod4) => __copyProps(__defProp({}, "__esModule", { value: true }), mod4);
19
25
 
20
26
  // src/index.ts
21
27
  var src_exports = {};
@@ -38,21 +44,26 @@ __export(sm2_exports, {
38
44
  doEncrypt: () => doEncrypt,
39
45
  doSignature: () => doSignature,
40
46
  doVerifySignature: () => doVerifySignature,
41
- generateEcparam: () => generateEcparam,
42
47
  generateKeyPairHex: () => generateKeyPairHex,
43
- getGlobalCurve: () => getGlobalCurve,
44
48
  getHash: () => getHash,
45
49
  getPoint: () => getPoint,
46
50
  getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
47
51
  hexToArray: () => hexToArray,
48
- leftPad: () => leftPad,
52
+ leftPad: () => leftPad2,
49
53
  utf8ToHex: () => utf8ToHex,
50
54
  verifyPublicKey: () => verifyPublicKey
51
55
  });
52
- var import_jsbn4 = require("jsbn");
53
56
 
54
57
  // src/sm2/asn1.ts
55
- var import_jsbn = require("jsbn");
58
+ var utils = __toESM(require("@noble/curves/abstract/utils"));
59
+
60
+ // src/sm2/bn.ts
61
+ var ZERO = BigInt(0);
62
+ var ONE = BigInt(1);
63
+ var TWO = BigInt(2);
64
+ var THREE = BigInt(3);
65
+
66
+ // src/sm2/asn1.ts
56
67
  function bigintToValue(bigint) {
57
68
  let h = bigint.toString(16);
58
69
  if (h[0] !== "-") {
@@ -61,7 +72,7 @@ function bigintToValue(bigint) {
61
72
  else if (!h.match(/^[0-7]/))
62
73
  h = "00" + h;
63
74
  } else {
64
- h = h.substr(1);
75
+ h = h.substring(1);
65
76
  let len = h.length;
66
77
  if (len % 2 === 1)
67
78
  len += 1;
@@ -70,8 +81,8 @@ function bigintToValue(bigint) {
70
81
  let maskString = "";
71
82
  for (let i = 0; i < len; i++)
72
83
  maskString += "f";
73
- let mask = new import_jsbn.BigInteger(maskString, 16);
74
- let output = mask.xor(bigint).add(import_jsbn.BigInteger.ONE);
84
+ let mask = utils.hexToNumber(maskString);
85
+ let output = (mask ^ bigint) + ONE;
75
86
  h = output.toString(16).replace(/^-/, "");
76
87
  }
77
88
  return h;
@@ -92,11 +103,11 @@ var ASN1Object = class {
92
103
  return this.tlv;
93
104
  }
94
105
  getLength() {
95
- const n3 = this.v.length / 2;
96
- let nHex = n3.toString(16);
106
+ const n = this.v.length / 2;
107
+ let nHex = n.toString(16);
97
108
  if (nHex.length % 2 === 1)
98
109
  nHex = "0" + nHex;
99
- if (n3 < 128) {
110
+ if (n < 128) {
100
111
  return nHex;
101
112
  } else {
102
113
  const head = 128 + nHex.length / 2;
@@ -139,8 +150,8 @@ function getL(str, start) {
139
150
  const l = str.substring(start + 2, start + 2 + len * 2);
140
151
  if (!l)
141
152
  return -1;
142
- const bigint = +l[0] < 8 ? new import_jsbn.BigInteger(l, 16) : new import_jsbn.BigInteger(l.substring(2), 16);
143
- return bigint.intValue();
153
+ const bigint = +l[0] < 8 ? utils.hexToNumber(l) : utils.hexToNumber(l.substring(2));
154
+ return +bigint.toString();
144
155
  }
145
156
  function getStartOfV(str, start) {
146
157
  const len = getLenOfL(str, start);
@@ -161,344 +172,23 @@ function decodeDer(input) {
161
172
  const vIndexS = getStartOfV(input, nextStart);
162
173
  const lS = getL(input, nextStart);
163
174
  const vS = input.substring(vIndexS, vIndexS + lS * 2);
164
- const r = new import_jsbn.BigInteger(vR, 16);
165
- const s = new import_jsbn.BigInteger(vS, 16);
175
+ const r = utils.hexToNumber(vR);
176
+ const s = utils.hexToNumber(vS);
166
177
  return { r, s };
167
178
  }
168
179
 
169
180
  // src/sm2/utils.ts
170
- var import_jsbn3 = require("jsbn");
181
+ var utils2 = __toESM(require("@noble/curves/abstract/utils"));
171
182
 
172
183
  // src/sm2/ec.ts
173
- var import_jsbn2 = require("jsbn");
174
- var TWO = new import_jsbn2.BigInteger("2");
175
- var THREE = new import_jsbn2.BigInteger("3");
176
- var ECFieldElementFp = class {
177
- constructor(q, x) {
178
- this.q = q;
179
- this.x = x;
180
- }
181
- equals(other) {
182
- if (other === this)
183
- return true;
184
- return this.q.equals(other.q) && this.x.equals(other.x);
185
- }
186
- toBigInteger() {
187
- return this.x;
188
- }
189
- negate() {
190
- return new ECFieldElementFp(this.q, this.x.negate().mod(this.q));
191
- }
192
- add(b) {
193
- return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q));
194
- }
195
- subtract(b) {
196
- return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q));
197
- }
198
- multiply(b) {
199
- return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q));
200
- }
201
- divide(b) {
202
- return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q));
203
- }
204
- square() {
205
- return new ECFieldElementFp(this.q, this.x.square().mod(this.q));
206
- }
207
- };
208
- var ECPointFp = class {
209
- constructor(curve3, x, y, z) {
210
- this.curve = curve3;
211
- this.x = x;
212
- this.y = y;
213
- this.z = z == null ? import_jsbn2.BigInteger.ONE : z;
214
- this.zinv = null;
215
- }
216
- zinv;
217
- z;
218
- getX() {
219
- if (this.zinv === null)
220
- this.zinv = this.z.modInverse(this.curve.q);
221
- return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q));
222
- }
223
- getY() {
224
- if (this.zinv === null)
225
- this.zinv = this.z.modInverse(this.curve.q);
226
- return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q));
227
- }
228
- equals(other) {
229
- if (other === this)
230
- return true;
231
- if (this.isInfinity())
232
- return other.isInfinity();
233
- if (other.isInfinity())
234
- return this.isInfinity();
235
- const u = other.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(other.z)).mod(this.curve.q);
236
- if (!u.equals(import_jsbn2.BigInteger.ZERO))
237
- return false;
238
- const v = other.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(other.z)).mod(this.curve.q);
239
- return v.equals(import_jsbn2.BigInteger.ZERO);
240
- }
241
- isInfinity() {
242
- if (this.x === null && this.y === null)
243
- return true;
244
- return this.z.equals(import_jsbn2.BigInteger.ZERO) && !this.y.toBigInteger().equals(import_jsbn2.BigInteger.ZERO);
245
- }
246
- negate() {
247
- return new ECPointFp(this.curve, this.x, this.y.negate(), this.z);
248
- }
249
- add(b) {
250
- if (this.isInfinity())
251
- return b;
252
- if (b.isInfinity())
253
- return this;
254
- const x1 = this.x.toBigInteger();
255
- const y1 = this.y.toBigInteger();
256
- const z1 = this.z;
257
- const x2 = b.x.toBigInteger();
258
- const y2 = b.y.toBigInteger();
259
- const z2 = b.z;
260
- const q = this.curve.q;
261
- const w1 = x1.multiply(z2).mod(q);
262
- const w2 = x2.multiply(z1).mod(q);
263
- const w3 = w1.subtract(w2);
264
- const w4 = y1.multiply(z2).mod(q);
265
- const w5 = y2.multiply(z1).mod(q);
266
- const w6 = w4.subtract(w5);
267
- if (import_jsbn2.BigInteger.ZERO.equals(w3)) {
268
- if (import_jsbn2.BigInteger.ZERO.equals(w6)) {
269
- return this.twice();
270
- }
271
- return this.curve.infinity;
272
- }
273
- const w7 = w1.add(w2);
274
- const w8 = z1.multiply(z2).mod(q);
275
- const w9 = w3.square().mod(q);
276
- const w10 = w3.multiply(w9).mod(q);
277
- const w11 = w8.multiply(w6.square()).subtract(w7.multiply(w9)).mod(q);
278
- const x3 = w3.multiply(w11).mod(q);
279
- const y3 = w6.multiply(w9.multiply(w1).subtract(w11)).subtract(w4.multiply(w10)).mod(q);
280
- const z3 = w10.multiply(w8).mod(q);
281
- return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
282
- }
283
- twice() {
284
- if (this.isInfinity())
285
- return this;
286
- if (!this.y.toBigInteger().signum())
287
- return this.curve.infinity;
288
- const x1 = this.x.toBigInteger();
289
- const y1 = this.y.toBigInteger();
290
- const z1 = this.z;
291
- const q = this.curve.q;
292
- const a = this.curve.a.toBigInteger();
293
- const w1 = x1.square().multiply(THREE).add(a.multiply(z1.square())).mod(q);
294
- const w2 = y1.shiftLeft(1).multiply(z1).mod(q);
295
- const w3 = y1.square().mod(q);
296
- const w4 = w3.multiply(x1).multiply(z1).mod(q);
297
- const w5 = w2.square().mod(q);
298
- const w6 = w1.square().subtract(w4.shiftLeft(3)).mod(q);
299
- const x3 = w2.multiply(w6).mod(q);
300
- const y3 = w1.multiply(w4.shiftLeft(2).subtract(w6)).subtract(w5.shiftLeft(1).multiply(w3)).mod(q);
301
- const z3 = w2.multiply(w5).mod(q);
302
- return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
303
- }
304
- multiply(k) {
305
- if (this.isInfinity())
306
- return this;
307
- if (!k.signum())
308
- return this.curve.infinity;
309
- const k3 = k.multiply(THREE);
310
- const neg = this.negate();
311
- let Q = this;
312
- for (let i = k3.bitLength() - 2; i > 0; i--) {
313
- Q = Q.twice();
314
- const k3Bit = k3.testBit(i);
315
- const kBit = k.testBit(i);
316
- if (k3Bit !== kBit) {
317
- Q = Q.add(k3Bit ? this : neg);
318
- }
319
- }
320
- return Q;
321
- }
322
- };
323
- var ECCurveFp = class {
324
- constructor(q, a, b) {
325
- this.q = q;
326
- this.q = q;
327
- this.a = this.fromBigInteger(a);
328
- this.b = this.fromBigInteger(b);
329
- this.infinity = new ECPointFp(this, null, null);
330
- }
331
- infinity;
332
- a;
333
- b;
334
- equals(other) {
335
- if (other === this)
336
- return true;
337
- return this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b);
338
- }
339
- fromBigInteger(x) {
340
- return new ECFieldElementFp(this.q, x);
341
- }
342
- decodePointHex(s) {
343
- switch (parseInt(s.substring(0, 2), 16)) {
344
- case 0:
345
- return this.infinity;
346
- case 2:
347
- case 3:
348
- const x = this.fromBigInteger(new import_jsbn2.BigInteger(s.substring(2), 16));
349
- let y = this.fromBigInteger(x.multiply(x.square()).add(
350
- x.multiply(this.a)
351
- ).add(this.b).toBigInteger().modPow(
352
- this.q.divide(new import_jsbn2.BigInteger("4")).add(import_jsbn2.BigInteger.ONE),
353
- this.q
354
- ));
355
- if (!y.toBigInteger().mod(TWO).equals(new import_jsbn2.BigInteger(s.substring(0, 2), 16).subtract(TWO))) {
356
- y = y.negate();
357
- }
358
- return new ECPointFp(this, x, y);
359
- case 4:
360
- case 6:
361
- case 7:
362
- const len = (s.length - 2) / 2;
363
- const xHex = s.substring(2, 2 + len);
364
- const yHex = s.substring(len + 2, len + 2 + len);
365
- return new ECPointFp(this, this.fromBigInteger(new import_jsbn2.BigInteger(xHex, 16)), this.fromBigInteger(new import_jsbn2.BigInteger(yHex, 16)));
366
- default:
367
- return null;
368
- }
369
- }
370
- };
371
-
372
- // src/sm2/utils.ts
373
- var rng = new import_jsbn3.SecureRandom();
374
- var { curve, G, n } = generateEcparam();
375
- function getGlobalCurve() {
376
- return curve;
377
- }
378
- function generateEcparam() {
379
- const p = new import_jsbn3.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", 16);
380
- const a = new import_jsbn3.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", 16);
381
- const b = new import_jsbn3.BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", 16);
382
- const curve3 = new ECCurveFp(p, a, b);
383
- const gxHex = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7";
384
- const gyHex = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0";
385
- const G3 = curve3.decodePointHex("04" + gxHex + gyHex);
386
- const n3 = new import_jsbn3.BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16);
387
- return { curve: curve3, G: G3, n: n3 };
388
- }
389
- function generateKeyPairHex(a, b, c) {
390
- const random = typeof a === "string" ? new import_jsbn3.BigInteger(a, b) : a ? new import_jsbn3.BigInteger(a, b, c) : new import_jsbn3.BigInteger(n.bitLength(), rng);
391
- const d = random.mod(n.subtract(import_jsbn3.BigInteger.ONE)).add(import_jsbn3.BigInteger.ONE);
392
- const privateKey = leftPad(d.toString(16), 64);
393
- const P = G.multiply(d);
394
- const Px = leftPad(P.getX().toBigInteger().toString(16), 64);
395
- const Py = leftPad(P.getY().toBigInteger().toString(16), 64);
396
- const publicKey = "04" + Px + Py;
397
- return { privateKey, publicKey };
398
- }
399
- function compressPublicKeyHex(s) {
400
- if (s.length !== 130)
401
- throw new Error("Invalid public key to compress");
402
- const len = (s.length - 2) / 2;
403
- const xHex = s.substring(2, 2 + len);
404
- const y = new import_jsbn3.BigInteger(s.substring(len + 2, len + len + 2), 16);
405
- let prefix = "03";
406
- if (y.mod(new import_jsbn3.BigInteger("2")).equals(import_jsbn3.BigInteger.ZERO))
407
- prefix = "02";
408
- return prefix + xHex;
409
- }
410
- function utf8ToHex(input) {
411
- input = decodeURIComponent(encodeURIComponent(input));
412
- const length = input.length;
413
- const words = new Uint32Array((length >>> 2) + 1);
414
- for (let i = 0; i < length; i++) {
415
- words[i >>> 2] |= (input.charCodeAt(i) & 255) << 24 - i % 4 * 8;
416
- }
417
- const hexChars = [];
418
- for (let i = 0; i < length; i++) {
419
- const bite = words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
420
- hexChars.push((bite >>> 4).toString(16));
421
- hexChars.push((bite & 15).toString(16));
422
- }
423
- return hexChars.join("");
424
- }
425
- function leftPad(input, num) {
426
- if (input.length >= num)
427
- return input;
428
- return new Array(num - input.length + 1).join("0") + input;
429
- }
430
- function arrayToHex(arr) {
431
- return arr.map((item) => {
432
- const hex = item.toString(16);
433
- return hex.length === 1 ? "0" + hex : hex;
434
- }).join("");
435
- }
436
- function arrayToUtf8(arr) {
437
- const str = [];
438
- for (let i = 0, len = arr.length; i < len; i++) {
439
- if (arr[i] >= 240 && arr[i] <= 247) {
440
- str.push(String.fromCodePoint(((arr[i] & 7) << 18) + ((arr[i + 1] & 63) << 12) + ((arr[i + 2] & 63) << 6) + (arr[i + 3] & 63)));
441
- i += 3;
442
- } else if (arr[i] >= 224 && arr[i] <= 239) {
443
- str.push(String.fromCodePoint(((arr[i] & 15) << 12) + ((arr[i + 1] & 63) << 6) + (arr[i + 2] & 63)));
444
- i += 2;
445
- } else if (arr[i] >= 192 && arr[i] <= 223) {
446
- str.push(String.fromCodePoint(((arr[i] & 31) << 6) + (arr[i + 1] & 63)));
447
- i++;
448
- } else {
449
- str.push(String.fromCodePoint(arr[i]));
450
- }
451
- }
452
- return str.join("");
453
- }
454
- function hexToArray(hexStr) {
455
- let hexStrLength = hexStr.length;
456
- if (hexStrLength % 2 !== 0) {
457
- hexStr = leftPad(hexStr, hexStrLength + 1);
458
- }
459
- hexStrLength = hexStr.length;
460
- const wordLength = hexStrLength / 2;
461
- const words = new Uint8Array(wordLength);
462
- for (let i = 0; i < wordLength; i++) {
463
- words[i] = parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
464
- }
465
- return words;
466
- }
467
- function verifyPublicKey(publicKey) {
468
- const point = curve.decodePointHex(publicKey);
469
- if (!point)
470
- return false;
471
- const x = point.getX();
472
- const y = point.getY();
473
- return y.square().equals(x.multiply(x.square()).add(x.multiply(curve.a)).add(curve.b));
474
- }
475
- function comparePublicKeyHex(publicKey1, publicKey2) {
476
- const point1 = curve.decodePointHex(publicKey1);
477
- if (!point1)
478
- return false;
479
- const point2 = curve.decodePointHex(publicKey2);
480
- if (!point2)
481
- return false;
482
- return point1.equals(point2);
483
- }
484
- function concatArray(...arrays) {
485
- let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
486
- if (!arrays.length)
487
- return new Uint8Array();
488
- let result = new Uint8Array(totalLength);
489
- let length = 0;
490
- for (let array of arrays) {
491
- result.set(array, length);
492
- length += array.length;
493
- }
494
- return result;
495
- }
184
+ var import_weierstrass = require("@noble/curves/abstract/weierstrass");
185
+ var import_modular = require("@noble/curves/abstract/modular");
496
186
 
497
187
  // src/sm2/sm3.ts
498
188
  var W = new Uint32Array(68);
499
189
  var M = new Uint32Array(64);
500
- function rotl(x, n3) {
501
- const s = n3 & 31;
190
+ function rotl(x, n) {
191
+ const s = n & 31;
502
192
  return x << s | x >>> 32 - s;
503
193
  }
504
194
  function xor(x, y) {
@@ -536,9 +226,9 @@ function sm3(array) {
536
226
  }
537
227
  const m = Uint8Array.from([...array, 128, ...kArr, ...lenArr]);
538
228
  const dataView = new DataView(m.buffer, 0);
539
- const n3 = m.length / 64;
229
+ const n = m.length / 64;
540
230
  const V = new Uint32Array([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214]);
541
- for (let i = 0; i < n3; i++) {
231
+ for (let i = 0; i < n; i++) {
542
232
  W.fill(0);
543
233
  M.fill(0);
544
234
  const start = 16 * i;
@@ -559,7 +249,7 @@ function sm3(array) {
559
249
  let D = V[3];
560
250
  let E = V[4];
561
251
  let F = V[5];
562
- let G3 = V[6];
252
+ let G = V[6];
563
253
  let H = V[7];
564
254
  let SS1;
565
255
  let SS2;
@@ -571,13 +261,13 @@ function sm3(array) {
571
261
  SS1 = rotl(rotl(A, 12) + E + rotl(T, j), 7);
572
262
  SS2 = SS1 ^ rotl(A, 12);
573
263
  TT1 = (j >= 0 && j <= 15 ? A ^ B ^ C : A & B | A & C | B & C) + D + SS2 + M[j];
574
- TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G3 : E & F | ~E & G3) + H + SS1 + W[j];
264
+ TT2 = (j >= 0 && j <= 15 ? E ^ F ^ G : E & F | ~E & G) + H + SS1 + W[j];
575
265
  D = C;
576
266
  C = rotl(B, 9);
577
267
  B = A;
578
268
  A = TT1;
579
- H = G3;
580
- G3 = rotl(F, 19);
269
+ H = G;
270
+ G = rotl(F, 19);
581
271
  F = E;
582
272
  E = P0(TT2);
583
273
  }
@@ -587,7 +277,7 @@ function sm3(array) {
587
277
  V[3] ^= D;
588
278
  V[4] ^= E;
589
279
  V[5] ^= F;
590
- V[6] ^= G3;
280
+ V[6] ^= G;
591
281
  V[7] ^= H;
592
282
  }
593
283
  const result = new Uint8Array(V.length * 4);
@@ -621,20 +311,247 @@ function hmac(input, key) {
621
311
  return sm3(concatArray(oPadKey, hash));
622
312
  }
623
313
 
314
+ // src/sm3/index.ts
315
+ var sm3_exports = {};
316
+ __export(sm3_exports, {
317
+ sm3: () => sm32,
318
+ utf8ToArray: () => utf8ToArray
319
+ });
320
+ function utf8ToArray(str) {
321
+ const arr = [];
322
+ for (let i = 0, len = str.length; i < len; i++) {
323
+ const point = str.codePointAt(i);
324
+ if (point <= 127) {
325
+ arr.push(point);
326
+ } else if (point <= 2047) {
327
+ arr.push(192 | point >>> 6);
328
+ arr.push(128 | point & 63);
329
+ } else if (point <= 55295 || point >= 57344 && point <= 65535) {
330
+ arr.push(224 | point >>> 12);
331
+ arr.push(128 | point >>> 6 & 63);
332
+ arr.push(128 | point & 63);
333
+ } else if (point >= 65536 && point <= 1114111) {
334
+ i++;
335
+ arr.push(240 | point >>> 18 & 28);
336
+ arr.push(128 | point >>> 12 & 63);
337
+ arr.push(128 | point >>> 6 & 63);
338
+ arr.push(128 | point & 63);
339
+ } else {
340
+ arr.push(point);
341
+ throw new Error("input is not supported");
342
+ }
343
+ }
344
+ return new Uint8Array(arr);
345
+ }
346
+ function sm32(input, options) {
347
+ input = typeof input === "string" ? utf8ToArray(input) : input;
348
+ if (options) {
349
+ const mode = options.mode || "hmac";
350
+ if (mode !== "hmac")
351
+ throw new Error("invalid mode");
352
+ let key = options.key;
353
+ if (!key)
354
+ throw new Error("invalid key");
355
+ key = typeof key === "string" ? hexToArray(key) : key;
356
+ return arrayToHex(Array.from(hmac(input, key)));
357
+ }
358
+ return arrayToHex(Array.from(sm3(input)));
359
+ }
360
+
361
+ // src/sm2/ec.ts
362
+ var DEFAULT_PRNG_POOL_SIZE = 4096;
363
+ var prngPool = new Uint8Array(0);
364
+ async function FillPRNGPoolIfNeeded() {
365
+ if ("crypto" in globalThis)
366
+ return;
367
+ if (prngPool.length > DEFAULT_PRNG_POOL_SIZE / 2)
368
+ return;
369
+ if ("wx" in globalThis) {
370
+ prngPool = await new Promise((r) => {
371
+ wx.getRandomValues({
372
+ length: DEFAULT_PRNG_POOL_SIZE,
373
+ success(res) {
374
+ r(new Uint8Array(res.randomValues));
375
+ }
376
+ });
377
+ });
378
+ } else {
379
+ try {
380
+ const crypto = await import(
381
+ /* webpackIgnore: true */
382
+ "crypto"
383
+ );
384
+ const array = new Uint8Array(DEFAULT_PRNG_POOL_SIZE);
385
+ crypto.webcrypto.getRandomValues(array);
386
+ prngPool = array;
387
+ } catch (error) {
388
+ throw new Error("no available csprng, abort.");
389
+ }
390
+ }
391
+ }
392
+ FillPRNGPoolIfNeeded();
393
+ function consumePool(length) {
394
+ if (prngPool.length > length) {
395
+ const prng = prngPool.slice(0, length);
396
+ prngPool = prngPool.slice(length);
397
+ FillPRNGPoolIfNeeded();
398
+ return prng;
399
+ } else {
400
+ throw new Error("random number pool is insufficient, prevent getting too long random values or too often.");
401
+ }
402
+ }
403
+ function randomBytes(length = 0) {
404
+ const array = new Uint8Array(length);
405
+ if ("crypto" in globalThis) {
406
+ return globalThis.crypto.getRandomValues(array);
407
+ } else {
408
+ const result = consumePool(length);
409
+ return result;
410
+ }
411
+ }
412
+ function createHash() {
413
+ const hashC = (msg) => sm3(typeof msg === "string" ? utf8ToArray(msg) : msg);
414
+ hashC.outputLen = 256;
415
+ hashC.blockLen = 512;
416
+ hashC.create = () => sm3(Uint8Array.from([]));
417
+ return hashC;
418
+ }
419
+ var sm2Fp = (0, import_modular.Field)(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
420
+ var sm2Curve = (0, import_weierstrass.weierstrass)({
421
+ a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
422
+ b: BigInt("18505919022281880113072981827955639221458448578012075254857346196103069175443"),
423
+ Fp: sm2Fp,
424
+ h: ONE,
425
+ n: BigInt("115792089210356248756420345214020892766061623724957744567843809356293439045923"),
426
+ Gx: BigInt("22963146547237050559479531362550074578802567295341616970375194840604139615431"),
427
+ Gy: BigInt("85132369209828568825618990617112496413088388631904505083283536607588877201568"),
428
+ hash: createHash(),
429
+ hmac: (key, ...msgs) => hmac(concatArray(...msgs), key),
430
+ randomBytes
431
+ });
432
+
433
+ // src/sm2/utils.ts
434
+ var import_modular2 = require("@noble/curves/abstract/modular");
435
+ function generateKeyPairHex(str) {
436
+ const privateKey = str ? utils2.numberToBytesBE((0, import_modular2.mod)(BigInt(str), ONE) + ONE, 32) : sm2Curve.utils.randomPrivateKey();
437
+ const publicKey = sm2Curve.getPublicKey(privateKey, false);
438
+ const privPad = leftPad2(utils2.bytesToHex(privateKey), 64);
439
+ const pubPad = leftPad2(utils2.bytesToHex(publicKey), 64);
440
+ return { privateKey: privPad, publicKey: pubPad };
441
+ }
442
+ function compressPublicKeyHex(s) {
443
+ if (s.length !== 130)
444
+ throw new Error("Invalid public key to compress");
445
+ const len = (s.length - 2) / 2;
446
+ const xHex = s.substring(2, 2 + len);
447
+ const y = utils2.hexToNumber(s.substring(len + 2, len + len + 2));
448
+ let prefix = "03";
449
+ if ((0, import_modular2.mod)(y, TWO) === ZERO)
450
+ prefix = "02";
451
+ return prefix + xHex;
452
+ }
453
+ function utf8ToHex(input) {
454
+ input = decodeURIComponent(encodeURIComponent(input));
455
+ const length = input.length;
456
+ const words = new Uint32Array((length >>> 2) + 1);
457
+ for (let i = 0; i < length; i++) {
458
+ words[i >>> 2] |= (input.charCodeAt(i) & 255) << 24 - i % 4 * 8;
459
+ }
460
+ const hexChars = [];
461
+ for (let i = 0; i < length; i++) {
462
+ const bite = words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
463
+ hexChars.push((bite >>> 4).toString(16));
464
+ hexChars.push((bite & 15).toString(16));
465
+ }
466
+ return hexChars.join("");
467
+ }
468
+ function leftPad2(input, num) {
469
+ if (input.length >= num)
470
+ return input;
471
+ return new Array(num - input.length + 1).join("0") + input;
472
+ }
473
+ function arrayToHex(arr) {
474
+ return arr.map((item) => {
475
+ const hex = item.toString(16);
476
+ return hex.length === 1 ? "0" + hex : hex;
477
+ }).join("");
478
+ }
479
+ function arrayToUtf8(arr) {
480
+ const str = [];
481
+ for (let i = 0, len = arr.length; i < len; i++) {
482
+ if (arr[i] >= 240 && arr[i] <= 247) {
483
+ str.push(String.fromCodePoint(((arr[i] & 7) << 18) + ((arr[i + 1] & 63) << 12) + ((arr[i + 2] & 63) << 6) + (arr[i + 3] & 63)));
484
+ i += 3;
485
+ } else if (arr[i] >= 224 && arr[i] <= 239) {
486
+ str.push(String.fromCodePoint(((arr[i] & 15) << 12) + ((arr[i + 1] & 63) << 6) + (arr[i + 2] & 63)));
487
+ i += 2;
488
+ } else if (arr[i] >= 192 && arr[i] <= 223) {
489
+ str.push(String.fromCodePoint(((arr[i] & 31) << 6) + (arr[i + 1] & 63)));
490
+ i++;
491
+ } else {
492
+ str.push(String.fromCodePoint(arr[i]));
493
+ }
494
+ }
495
+ return str.join("");
496
+ }
497
+ function hexToArray(hexStr) {
498
+ let hexStrLength = hexStr.length;
499
+ if (hexStrLength % 2 !== 0) {
500
+ hexStr = leftPad2(hexStr, hexStrLength + 1);
501
+ }
502
+ hexStrLength = hexStr.length;
503
+ const wordLength = hexStrLength / 2;
504
+ const words = new Uint8Array(wordLength);
505
+ for (let i = 0; i < wordLength; i++) {
506
+ words[i] = parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
507
+ }
508
+ return words;
509
+ }
510
+ function verifyPublicKey(publicKey) {
511
+ const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
512
+ if (!point)
513
+ return false;
514
+ const x = point.x;
515
+ const y = point.y;
516
+ return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.add(sm2Fp.mul(x, sm2Fp.sqr(x)), sm2Fp.mul(x, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
517
+ }
518
+ function comparePublicKeyHex(publicKey1, publicKey2) {
519
+ const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
520
+ if (!point1)
521
+ return false;
522
+ const point2 = sm2Curve.ProjectivePoint.fromHex(publicKey2);
523
+ if (!point2)
524
+ return false;
525
+ return point1.equals(point2);
526
+ }
527
+ function concatArray(...arrays) {
528
+ let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
529
+ if (!arrays.length)
530
+ return new Uint8Array();
531
+ let result = new Uint8Array(totalLength);
532
+ let length = 0;
533
+ for (let array of arrays) {
534
+ result.set(array, length);
535
+ length += array.length;
536
+ }
537
+ return result;
538
+ }
539
+
624
540
  // src/sm2/index.ts
625
- var { G: G2, curve: curve2, n: n2 } = generateEcparam();
541
+ var mod2 = __toESM(require("@noble/curves/abstract/modular"));
542
+ var utils3 = __toESM(require("@noble/curves/abstract/utils"));
626
543
  var C1C2C3 = 0;
627
544
  function doEncrypt(msg, publicKey, cipherMode = 1) {
628
545
  const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
629
- const publicKeyPoint = getGlobalCurve().decodePointHex(publicKey);
546
+ const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
630
547
  const keypair = generateKeyPairHex();
631
- const k = new import_jsbn4.BigInteger(keypair.privateKey, 16);
548
+ const k = utils3.hexToNumber(keypair.privateKey);
632
549
  let c1 = keypair.publicKey;
633
550
  if (c1.length > 128)
634
551
  c1 = c1.substring(c1.length - 128);
635
552
  const p = publicKeyPoint.multiply(k);
636
- const x2 = hexToArray(leftPad(p.getX().toBigInteger().toRadix(16), 64));
637
- const y2 = hexToArray(leftPad(p.getY().toBigInteger().toRadix(16), 64));
553
+ const x2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.x), 64));
554
+ const y2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.y), 64));
638
555
  const c3 = arrayToHex(Array.from(sm3(concatArray(x2, msgArr, y2))));
639
556
  let ct = 1;
640
557
  let offset = 0;
@@ -657,7 +574,7 @@ function doEncrypt(msg, publicKey, cipherMode = 1) {
657
574
  function doDecrypt(encryptData, privateKey, cipherMode = 1, {
658
575
  output = "string"
659
576
  } = {}) {
660
- const privateKeyInteger = new import_jsbn4.BigInteger(privateKey, 16);
577
+ const privateKeyInteger = utils3.hexToNumber(privateKey);
661
578
  let c3 = encryptData.substring(128, 128 + 64);
662
579
  let c2 = encryptData.substring(128 + 64);
663
580
  if (cipherMode === C1C2C3) {
@@ -665,10 +582,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
665
582
  c2 = encryptData.substring(128, encryptData.length - 64);
666
583
  }
667
584
  const msg = hexToArray(c2);
668
- const c1 = getGlobalCurve().decodePointHex("04" + encryptData.substring(0, 128));
585
+ const c1 = sm2Curve.ProjectivePoint.fromHex("04" + encryptData.substring(0, 128));
669
586
  const p = c1.multiply(privateKeyInteger);
670
- const x2 = hexToArray(leftPad(p.getX().toBigInteger().toRadix(16), 64));
671
- const y2 = hexToArray(leftPad(p.getY().toBigInteger().toRadix(16), 64));
587
+ const x2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.x), 64));
588
+ const y2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.y), 64));
672
589
  let ct = 1;
673
590
  let offset = 0;
674
591
  let t = new Uint8Array();
@@ -704,8 +621,8 @@ function doSignature(msg, privateKey, options = {}) {
704
621
  publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
705
622
  hashHex = getHash(hashHex, publicKey, userId);
706
623
  }
707
- const dA = new import_jsbn4.BigInteger(privateKey, 16);
708
- const e = new import_jsbn4.BigInteger(hashHex, 16);
624
+ const dA = utils3.hexToNumber(privateKey);
625
+ const e = utils3.hexToNumber(hashHex);
709
626
  let k = null;
710
627
  let r = null;
711
628
  let s = null;
@@ -718,13 +635,13 @@ function doSignature(msg, privateKey, options = {}) {
718
635
  point = getPoint();
719
636
  }
720
637
  k = point.k;
721
- r = e.add(point.x1).mod(n2);
722
- } while (r.equals(import_jsbn4.BigInteger.ZERO) || r.add(k).equals(n2));
723
- s = dA.add(import_jsbn4.BigInteger.ONE).modInverse(n2).multiply(k.subtract(r.multiply(dA))).mod(n2);
724
- } while (s.equals(import_jsbn4.BigInteger.ZERO));
638
+ r = mod2.mod(e + point.x1, sm2Curve.CURVE.n);
639
+ } while (r === ZERO || r + k === sm2Curve.CURVE.n);
640
+ s = mod2.mod(mod2.invert(dA + ONE, sm2Curve.CURVE.n) * (k - r * dA), sm2Curve.CURVE.n);
641
+ } while (s === ZERO);
725
642
  if (der)
726
643
  return encodeDer(r, s);
727
- return leftPad(r.toString(16), 64) + leftPad(s.toString(16), 64);
644
+ return leftPad2(utils3.numberToHexUnpadded(r), 64) + leftPad2(utils3.numberToHexUnpadded(s), 64);
728
645
  }
729
646
  function doVerifySignature(msg, signHex, publicKey, options = {}) {
730
647
  let hashHex;
@@ -745,33 +662,33 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
745
662
  r = decodeDerObj.r;
746
663
  s = decodeDerObj.s;
747
664
  } else {
748
- r = new import_jsbn4.BigInteger(signHex.substring(0, 64), 16);
749
- s = new import_jsbn4.BigInteger(signHex.substring(64), 16);
665
+ r = utils3.hexToNumber(signHex.substring(0, 64));
666
+ s = utils3.hexToNumber(signHex.substring(64));
750
667
  }
751
- const PA = curve2.decodePointHex(publicKey);
752
- const e = new import_jsbn4.BigInteger(hashHex, 16);
753
- const t = r.add(s).mod(n2);
754
- if (t.equals(import_jsbn4.BigInteger.ZERO))
668
+ const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
669
+ const e = utils3.hexToNumber(hashHex);
670
+ const t = mod2.mod(r + s, sm2Curve.CURVE.n);
671
+ if (t === ZERO)
755
672
  return false;
756
- const x1y1 = G2.multiply(s).add(PA.multiply(t));
757
- const R = e.add(x1y1.getX().toBigInteger()).mod(n2);
758
- return r.equals(R);
673
+ const x1y1 = sm2Curve.ProjectivePoint.BASE.multiply(s).add(PA.multiply(t));
674
+ const R = mod2.mod(e + x1y1.x, sm2Curve.CURVE.n);
675
+ return r === R;
759
676
  }
760
677
  function getHash(hashHex, publicKey, userId = "1234567812345678") {
761
678
  userId = utf8ToHex(userId);
762
- const a = leftPad(G2.curve.a.toBigInteger().toRadix(16), 64);
763
- const b = leftPad(G2.curve.b.toBigInteger().toRadix(16), 64);
764
- const gx = leftPad(G2.getX().toBigInteger().toRadix(16), 64);
765
- const gy = leftPad(G2.getY().toBigInteger().toRadix(16), 64);
679
+ const a = leftPad2(utils3.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
680
+ const b = leftPad2(utils3.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
681
+ const gx = leftPad2(utils3.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
682
+ const gy = leftPad2(utils3.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
766
683
  let px;
767
684
  let py;
768
685
  if (publicKey.length === 128) {
769
686
  px = publicKey.substring(0, 64);
770
687
  py = publicKey.substring(64, 128);
771
688
  } else {
772
- const point = G2.curve.decodePointHex(publicKey);
773
- px = leftPad(point.getX().toBigInteger().toRadix(16), 64);
774
- py = leftPad(point.getY().toBigInteger().toRadix(16), 64);
689
+ const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
690
+ px = leftPad2(utils3.numberToHexUnpadded(point.x), 64);
691
+ py = leftPad2(utils3.numberToHexUnpadded(point.y), 64);
775
692
  }
776
693
  const data = hexToArray(userId + a + b + gx + gy + px + py);
777
694
  const entl = userId.length * 4;
@@ -779,68 +696,21 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
779
696
  return arrayToHex(Array.from(sm3(concatArray(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex))));
780
697
  }
781
698
  function getPublicKeyFromPrivateKey(privateKey) {
782
- const PA = G2.multiply(new import_jsbn4.BigInteger(privateKey, 16));
783
- const x = leftPad(PA.getX().toBigInteger().toString(16), 64);
784
- const y = leftPad(PA.getY().toBigInteger().toString(16), 64);
785
- return "04" + x + y;
699
+ const pubKey = sm2Curve.getPublicKey(privateKey, false);
700
+ const pubPad = leftPad2(utils3.bytesToHex(pubKey), 64);
701
+ return pubPad;
786
702
  }
787
703
  function getPoint() {
788
704
  const keypair = generateKeyPairHex();
789
- const PA = curve2.decodePointHex(keypair.publicKey);
705
+ const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey);
706
+ const k = utils3.hexToNumber(keypair.privateKey);
790
707
  return {
791
708
  ...keypair,
792
- k: new import_jsbn4.BigInteger(keypair.privateKey, 16),
793
- x1: PA.getX().toBigInteger()
709
+ k,
710
+ x1: PA.x
794
711
  };
795
712
  }
796
713
 
797
- // src/sm3/index.ts
798
- var sm3_exports = {};
799
- __export(sm3_exports, {
800
- sm3: () => sm32,
801
- utf8ToArray: () => utf8ToArray
802
- });
803
- function utf8ToArray(str) {
804
- const arr = [];
805
- for (let i = 0, len = str.length; i < len; i++) {
806
- const point = str.codePointAt(i);
807
- if (point <= 127) {
808
- arr.push(point);
809
- } else if (point <= 2047) {
810
- arr.push(192 | point >>> 6);
811
- arr.push(128 | point & 63);
812
- } else if (point <= 55295 || point >= 57344 && point <= 65535) {
813
- arr.push(224 | point >>> 12);
814
- arr.push(128 | point >>> 6 & 63);
815
- arr.push(128 | point & 63);
816
- } else if (point >= 65536 && point <= 1114111) {
817
- i++;
818
- arr.push(240 | point >>> 18 & 28);
819
- arr.push(128 | point >>> 12 & 63);
820
- arr.push(128 | point >>> 6 & 63);
821
- arr.push(128 | point & 63);
822
- } else {
823
- arr.push(point);
824
- throw new Error("input is not supported");
825
- }
826
- }
827
- return new Uint8Array(arr);
828
- }
829
- function sm32(input, options) {
830
- input = typeof input === "string" ? utf8ToArray(input) : input;
831
- if (options) {
832
- const mode = options.mode || "hmac";
833
- if (mode !== "hmac")
834
- throw new Error("invalid mode");
835
- let key = options.key;
836
- if (!key)
837
- throw new Error("invalid key");
838
- key = typeof key === "string" ? hexToArray(key) : key;
839
- return arrayToHex(Array.from(hmac(input, key)));
840
- }
841
- return arrayToHex(Array.from(sm3(input)));
842
- }
843
-
844
714
  // src/sm4/index.ts
845
715
  var sm4_exports = {};
846
716
  __export(sm4_exports, {