sm-crypto-v2 0.3.12 → 1.2.0
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/.github/workflows/test.yml +17 -0
- package/CHANGELOG.md +49 -0
- package/README.md +13 -1
- package/dist/index.d.ts +5 -224
- package/dist/index.js +304 -435
- package/dist/index.mjs +297 -434
- package/package.json +8 -6
- package/pnpm-lock.yaml +20 -7
- package/src/sm2/asn1.ts +15 -12
- package/src/sm2/bn.ts +4 -0
- package/src/sm2/ec.ts +79 -317
- package/src/sm2/index.ts +83 -54
- package/src/sm2/utils.ts +29 -57
- package/vitest.config.ts +5 -0
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
|
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: () =>
|
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
|
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.
|
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 =
|
74
|
-
let output = mask
|
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
|
96
|
-
let nHex =
|
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 (
|
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 ?
|
143
|
-
return bigint.
|
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 =
|
165
|
-
const s =
|
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
|
181
|
+
var utils2 = __toESM(require("@noble/curves/abstract/utils"));
|
171
182
|
|
172
183
|
// src/sm2/ec.ts
|
173
|
-
var
|
174
|
-
var
|
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,
|
501
|
-
const s =
|
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
|
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 <
|
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
|
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 ^
|
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 =
|
580
|
-
|
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] ^=
|
280
|
+
V[6] ^= G;
|
591
281
|
V[7] ^= H;
|
592
282
|
}
|
593
283
|
const result = new Uint8Array(V.length * 4);
|
@@ -621,20 +311,246 @@ 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(DEFAULT_PRNG_POOL_SIZE);
|
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
|
+
return consumePool(length);
|
409
|
+
}
|
410
|
+
}
|
411
|
+
function createHash() {
|
412
|
+
const hashC = (msg) => sm3(typeof msg === "string" ? utf8ToArray(msg) : msg);
|
413
|
+
hashC.outputLen = 256;
|
414
|
+
hashC.blockLen = 512;
|
415
|
+
hashC.create = () => sm3(Uint8Array.from([]));
|
416
|
+
return hashC;
|
417
|
+
}
|
418
|
+
var sm2Fp = (0, import_modular.Field)(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
419
|
+
var sm2Curve = (0, import_weierstrass.weierstrass)({
|
420
|
+
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
421
|
+
b: BigInt("18505919022281880113072981827955639221458448578012075254857346196103069175443"),
|
422
|
+
Fp: sm2Fp,
|
423
|
+
h: ONE,
|
424
|
+
n: BigInt("115792089210356248756420345214020892766061623724957744567843809356293439045923"),
|
425
|
+
Gx: BigInt("22963146547237050559479531362550074578802567295341616970375194840604139615431"),
|
426
|
+
Gy: BigInt("85132369209828568825618990617112496413088388631904505083283536607588877201568"),
|
427
|
+
hash: createHash(),
|
428
|
+
hmac: (key, ...msgs) => hmac(concatArray(...msgs), key),
|
429
|
+
randomBytes
|
430
|
+
});
|
431
|
+
|
432
|
+
// src/sm2/utils.ts
|
433
|
+
var import_modular2 = require("@noble/curves/abstract/modular");
|
434
|
+
function generateKeyPairHex() {
|
435
|
+
const privateKey = sm2Curve.utils.randomPrivateKey();
|
436
|
+
const publicKey = sm2Curve.getPublicKey(privateKey, false);
|
437
|
+
const privPad = leftPad2(utils2.bytesToHex(privateKey), 64);
|
438
|
+
const pubPad = leftPad2(utils2.bytesToHex(publicKey), 64);
|
439
|
+
return { privateKey: privPad, publicKey: pubPad };
|
440
|
+
}
|
441
|
+
function compressPublicKeyHex(s) {
|
442
|
+
if (s.length !== 130)
|
443
|
+
throw new Error("Invalid public key to compress");
|
444
|
+
const len = (s.length - 2) / 2;
|
445
|
+
const xHex = s.substring(2, 2 + len);
|
446
|
+
const y = utils2.hexToNumber(s.substring(len + 2, len + len + 2));
|
447
|
+
let prefix = "03";
|
448
|
+
if ((0, import_modular2.mod)(y, TWO) === ZERO)
|
449
|
+
prefix = "02";
|
450
|
+
return prefix + xHex;
|
451
|
+
}
|
452
|
+
function utf8ToHex(input) {
|
453
|
+
input = decodeURIComponent(encodeURIComponent(input));
|
454
|
+
const length = input.length;
|
455
|
+
const words = new Uint32Array((length >>> 2) + 1);
|
456
|
+
for (let i = 0; i < length; i++) {
|
457
|
+
words[i >>> 2] |= (input.charCodeAt(i) & 255) << 24 - i % 4 * 8;
|
458
|
+
}
|
459
|
+
const hexChars = [];
|
460
|
+
for (let i = 0; i < length; i++) {
|
461
|
+
const bite = words[i >>> 2] >>> 24 - i % 4 * 8 & 255;
|
462
|
+
hexChars.push((bite >>> 4).toString(16));
|
463
|
+
hexChars.push((bite & 15).toString(16));
|
464
|
+
}
|
465
|
+
return hexChars.join("");
|
466
|
+
}
|
467
|
+
function leftPad2(input, num) {
|
468
|
+
if (input.length >= num)
|
469
|
+
return input;
|
470
|
+
return new Array(num - input.length + 1).join("0") + input;
|
471
|
+
}
|
472
|
+
function arrayToHex(arr) {
|
473
|
+
return arr.map((item) => {
|
474
|
+
const hex = item.toString(16);
|
475
|
+
return hex.length === 1 ? "0" + hex : hex;
|
476
|
+
}).join("");
|
477
|
+
}
|
478
|
+
function arrayToUtf8(arr) {
|
479
|
+
const str = [];
|
480
|
+
for (let i = 0, len = arr.length; i < len; i++) {
|
481
|
+
if (arr[i] >= 240 && arr[i] <= 247) {
|
482
|
+
str.push(String.fromCodePoint(((arr[i] & 7) << 18) + ((arr[i + 1] & 63) << 12) + ((arr[i + 2] & 63) << 6) + (arr[i + 3] & 63)));
|
483
|
+
i += 3;
|
484
|
+
} else if (arr[i] >= 224 && arr[i] <= 239) {
|
485
|
+
str.push(String.fromCodePoint(((arr[i] & 15) << 12) + ((arr[i + 1] & 63) << 6) + (arr[i + 2] & 63)));
|
486
|
+
i += 2;
|
487
|
+
} else if (arr[i] >= 192 && arr[i] <= 223) {
|
488
|
+
str.push(String.fromCodePoint(((arr[i] & 31) << 6) + (arr[i + 1] & 63)));
|
489
|
+
i++;
|
490
|
+
} else {
|
491
|
+
str.push(String.fromCodePoint(arr[i]));
|
492
|
+
}
|
493
|
+
}
|
494
|
+
return str.join("");
|
495
|
+
}
|
496
|
+
function hexToArray(hexStr) {
|
497
|
+
let hexStrLength = hexStr.length;
|
498
|
+
if (hexStrLength % 2 !== 0) {
|
499
|
+
hexStr = leftPad2(hexStr, hexStrLength + 1);
|
500
|
+
}
|
501
|
+
hexStrLength = hexStr.length;
|
502
|
+
const wordLength = hexStrLength / 2;
|
503
|
+
const words = new Uint8Array(wordLength);
|
504
|
+
for (let i = 0; i < wordLength; i++) {
|
505
|
+
words[i] = parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
|
506
|
+
}
|
507
|
+
return words;
|
508
|
+
}
|
509
|
+
function verifyPublicKey(publicKey) {
|
510
|
+
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
511
|
+
if (!point)
|
512
|
+
return false;
|
513
|
+
const x = point.x;
|
514
|
+
const y = point.y;
|
515
|
+
return sm2Fp.sqr(y) === sm2Fp.add(sm2Fp.add(sm2Fp.mul(x, sm2Fp.sqr(x)), sm2Fp.mul(x, sm2Curve.CURVE.a)), sm2Curve.CURVE.b);
|
516
|
+
}
|
517
|
+
function comparePublicKeyHex(publicKey1, publicKey2) {
|
518
|
+
const point1 = sm2Curve.ProjectivePoint.fromHex(publicKey1);
|
519
|
+
if (!point1)
|
520
|
+
return false;
|
521
|
+
const point2 = sm2Curve.ProjectivePoint.fromHex(publicKey2);
|
522
|
+
if (!point2)
|
523
|
+
return false;
|
524
|
+
return point1.equals(point2);
|
525
|
+
}
|
526
|
+
function concatArray(...arrays) {
|
527
|
+
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
|
528
|
+
if (!arrays.length)
|
529
|
+
return new Uint8Array();
|
530
|
+
let result = new Uint8Array(totalLength);
|
531
|
+
let length = 0;
|
532
|
+
for (let array of arrays) {
|
533
|
+
result.set(array, length);
|
534
|
+
length += array.length;
|
535
|
+
}
|
536
|
+
return result;
|
537
|
+
}
|
538
|
+
|
624
539
|
// src/sm2/index.ts
|
625
|
-
var
|
540
|
+
var mod2 = __toESM(require("@noble/curves/abstract/modular"));
|
541
|
+
var utils3 = __toESM(require("@noble/curves/abstract/utils"));
|
626
542
|
var C1C2C3 = 0;
|
627
543
|
function doEncrypt(msg, publicKey, cipherMode = 1) {
|
628
544
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
629
|
-
const publicKeyPoint =
|
545
|
+
const publicKeyPoint = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
630
546
|
const keypair = generateKeyPairHex();
|
631
|
-
const k =
|
547
|
+
const k = utils3.hexToNumber(keypair.privateKey);
|
632
548
|
let c1 = keypair.publicKey;
|
633
549
|
if (c1.length > 128)
|
634
550
|
c1 = c1.substring(c1.length - 128);
|
635
551
|
const p = publicKeyPoint.multiply(k);
|
636
|
-
const x2 = hexToArray(
|
637
|
-
const y2 = hexToArray(
|
552
|
+
const x2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.x), 64));
|
553
|
+
const y2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.y), 64));
|
638
554
|
const c3 = arrayToHex(Array.from(sm3(concatArray(x2, msgArr, y2))));
|
639
555
|
let ct = 1;
|
640
556
|
let offset = 0;
|
@@ -657,7 +573,7 @@ function doEncrypt(msg, publicKey, cipherMode = 1) {
|
|
657
573
|
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
658
574
|
output = "string"
|
659
575
|
} = {}) {
|
660
|
-
const privateKeyInteger =
|
576
|
+
const privateKeyInteger = utils3.hexToNumber(privateKey);
|
661
577
|
let c3 = encryptData.substring(128, 128 + 64);
|
662
578
|
let c2 = encryptData.substring(128 + 64);
|
663
579
|
if (cipherMode === C1C2C3) {
|
@@ -665,10 +581,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
|
665
581
|
c2 = encryptData.substring(128, encryptData.length - 64);
|
666
582
|
}
|
667
583
|
const msg = hexToArray(c2);
|
668
|
-
const c1 =
|
584
|
+
const c1 = sm2Curve.ProjectivePoint.fromHex("04" + encryptData.substring(0, 128));
|
669
585
|
const p = c1.multiply(privateKeyInteger);
|
670
|
-
const x2 = hexToArray(
|
671
|
-
const y2 = hexToArray(
|
586
|
+
const x2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.x), 64));
|
587
|
+
const y2 = hexToArray(leftPad2(utils3.numberToHexUnpadded(p.y), 64));
|
672
588
|
let ct = 1;
|
673
589
|
let offset = 0;
|
674
590
|
let t = new Uint8Array();
|
@@ -704,8 +620,8 @@ function doSignature(msg, privateKey, options = {}) {
|
|
704
620
|
publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
|
705
621
|
hashHex = getHash(hashHex, publicKey, userId);
|
706
622
|
}
|
707
|
-
const dA =
|
708
|
-
const e =
|
623
|
+
const dA = utils3.hexToNumber(privateKey);
|
624
|
+
const e = utils3.hexToNumber(hashHex);
|
709
625
|
let k = null;
|
710
626
|
let r = null;
|
711
627
|
let s = null;
|
@@ -718,13 +634,13 @@ function doSignature(msg, privateKey, options = {}) {
|
|
718
634
|
point = getPoint();
|
719
635
|
}
|
720
636
|
k = point.k;
|
721
|
-
r =
|
722
|
-
} while (r
|
723
|
-
s =
|
724
|
-
} while (s
|
637
|
+
r = mod2.mod(e + point.x1, sm2Curve.CURVE.n);
|
638
|
+
} while (r === ZERO || r + k === sm2Curve.CURVE.n);
|
639
|
+
s = mod2.mod(mod2.invert(dA + ONE, sm2Curve.CURVE.n) * (k - r * dA), sm2Curve.CURVE.n);
|
640
|
+
} while (s === ZERO);
|
725
641
|
if (der)
|
726
642
|
return encodeDer(r, s);
|
727
|
-
return
|
643
|
+
return leftPad2(utils3.numberToHexUnpadded(r), 64) + leftPad2(utils3.numberToHexUnpadded(s), 64);
|
728
644
|
}
|
729
645
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
730
646
|
let hashHex;
|
@@ -745,33 +661,33 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
745
661
|
r = decodeDerObj.r;
|
746
662
|
s = decodeDerObj.s;
|
747
663
|
} else {
|
748
|
-
r =
|
749
|
-
s =
|
664
|
+
r = utils3.hexToNumber(signHex.substring(0, 64));
|
665
|
+
s = utils3.hexToNumber(signHex.substring(64));
|
750
666
|
}
|
751
|
-
const PA =
|
752
|
-
const e =
|
753
|
-
const t =
|
754
|
-
if (t
|
667
|
+
const PA = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
668
|
+
const e = utils3.hexToNumber(hashHex);
|
669
|
+
const t = mod2.mod(r + s, sm2Curve.CURVE.n);
|
670
|
+
if (t === ZERO)
|
755
671
|
return false;
|
756
|
-
const x1y1 =
|
757
|
-
const R =
|
758
|
-
return r
|
672
|
+
const x1y1 = sm2Curve.ProjectivePoint.BASE.multiply(s).add(PA.multiply(t));
|
673
|
+
const R = mod2.mod(e + x1y1.x, sm2Curve.CURVE.n);
|
674
|
+
return r === R;
|
759
675
|
}
|
760
676
|
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
761
677
|
userId = utf8ToHex(userId);
|
762
|
-
const a =
|
763
|
-
const b =
|
764
|
-
const gx =
|
765
|
-
const gy =
|
678
|
+
const a = leftPad2(utils3.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
679
|
+
const b = leftPad2(utils3.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
680
|
+
const gx = leftPad2(utils3.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
|
681
|
+
const gy = leftPad2(utils3.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
|
766
682
|
let px;
|
767
683
|
let py;
|
768
684
|
if (publicKey.length === 128) {
|
769
685
|
px = publicKey.substring(0, 64);
|
770
686
|
py = publicKey.substring(64, 128);
|
771
687
|
} else {
|
772
|
-
const point =
|
773
|
-
px =
|
774
|
-
py =
|
688
|
+
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
689
|
+
px = leftPad2(utils3.numberToHexUnpadded(point.x), 64);
|
690
|
+
py = leftPad2(utils3.numberToHexUnpadded(point.y), 64);
|
775
691
|
}
|
776
692
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
777
693
|
const entl = userId.length * 4;
|
@@ -779,68 +695,21 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
|
779
695
|
return arrayToHex(Array.from(sm3(concatArray(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex))));
|
780
696
|
}
|
781
697
|
function getPublicKeyFromPrivateKey(privateKey) {
|
782
|
-
const
|
783
|
-
const
|
784
|
-
|
785
|
-
return "04" + x + y;
|
698
|
+
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
699
|
+
const pubPad = leftPad2(utils3.bytesToHex(pubKey), 64);
|
700
|
+
return pubPad;
|
786
701
|
}
|
787
702
|
function getPoint() {
|
788
703
|
const keypair = generateKeyPairHex();
|
789
|
-
const PA =
|
704
|
+
const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey);
|
705
|
+
const k = utils3.hexToNumber(keypair.privateKey);
|
790
706
|
return {
|
791
707
|
...keypair,
|
792
|
-
k
|
793
|
-
x1: PA.
|
708
|
+
k,
|
709
|
+
x1: PA.x
|
794
710
|
};
|
795
711
|
}
|
796
712
|
|
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
713
|
// src/sm4/index.ts
|
845
714
|
var sm4_exports = {};
|
846
715
|
__export(sm4_exports, {
|