sm-crypto-v2 1.10.1 → 1.12.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/CHANGELOG.md +14 -0
- package/README.md +4 -1
- package/dist/index.d.mts +15 -7
- package/dist/index.d.ts +15 -7
- package/dist/index.js +136 -110
- package/dist/index.mjs +129 -104
- package/miniprogram_dist/index.d.ts +15 -7
- package/miniprogram_dist/index.js +87 -71
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,20 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
4
4
|
|
5
|
+
## [1.12.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.11.0...v1.12.0) (2025-06-05)
|
6
|
+
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* **utils:** ensure sm2 runs in environment don't have TextEncoder ([6b74b8d](https://github.com/Cubelrti/sm-crypto-v2/commit/6b74b8d4df3ead8e2c01587e3c13cdbbf9a27701))
|
11
|
+
|
12
|
+
## [1.11.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.1...v1.11.0) (2025-04-28)
|
13
|
+
|
14
|
+
|
15
|
+
### Features
|
16
|
+
|
17
|
+
* **sm3, kx:** expose kdf in sm3, added test case for parity ([f0091ca](https://github.com/Cubelrti/sm-crypto-v2/commit/f0091ca4a58a3369a83f274023268dfe2bda2794))
|
18
|
+
|
5
19
|
### [1.10.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.0...v1.10.1) (2025-04-27)
|
6
20
|
|
7
21
|
|
package/README.md
CHANGED
@@ -150,13 +150,16 @@ let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, precomputedPublicKe
|
|
150
150
|
## sm3
|
151
151
|
|
152
152
|
```js
|
153
|
-
import { sm3 } from 'sm-crypto-v2'
|
153
|
+
import { sm3, kdf } from 'sm-crypto-v2'
|
154
154
|
let hashData = sm3('abc') // 杂凑
|
155
155
|
|
156
156
|
// hmac
|
157
157
|
hashData = sm3('abc', {
|
158
158
|
key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9', // 要求为 16 进制串或字节数组
|
159
159
|
})
|
160
|
+
|
161
|
+
// kdf,注意这是 GM/T 0003-2012 中的 SM3 KDF(密钥派生函数),不是 RFC5869 的 HKDF
|
162
|
+
kdfData = kdf('abc', 32 /* 输出长度 */)
|
160
163
|
```
|
161
164
|
|
162
165
|
## sm4
|
package/dist/index.d.mts
CHANGED
@@ -31,7 +31,7 @@ declare function arrayToUtf8(arr: Uint8Array): string;
|
|
31
31
|
/**
|
32
32
|
* 转成字节数组
|
33
33
|
*/
|
34
|
-
declare function hexToArray(hexStr: string): Uint8Array
|
34
|
+
declare function hexToArray(hexStr: string): Uint8Array<ArrayBuffer>;
|
35
35
|
/**
|
36
36
|
* 验证公钥是否为椭圆曲线上的点
|
37
37
|
*/
|
@@ -43,9 +43,9 @@ declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): bo
|
|
43
43
|
|
44
44
|
declare function initRNGPool(): Promise<void>;
|
45
45
|
|
46
|
-
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array
|
46
|
+
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array<ArrayBuffer>;
|
47
47
|
|
48
|
-
declare const EmptyArray: Uint8Array
|
48
|
+
declare const EmptyArray: Uint8Array<ArrayBuffer>;
|
49
49
|
/**
|
50
50
|
* 加密
|
51
51
|
*/
|
@@ -85,7 +85,7 @@ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, pu
|
|
85
85
|
hash?: boolean;
|
86
86
|
userId?: string;
|
87
87
|
}): boolean;
|
88
|
-
declare function getZ(publicKey: string, userId?: string): Uint8Array
|
88
|
+
declare function getZ(publicKey: string, userId?: string): Uint8Array<ArrayBufferLike>;
|
89
89
|
/**
|
90
90
|
* sm3杂凑算法
|
91
91
|
*/
|
@@ -139,6 +139,14 @@ declare namespace index$1 {
|
|
139
139
|
export { index$1_EmptyArray as EmptyArray, type index$1_KeyPair as KeyPair, type index$1_SignaturePoint as SignaturePoint, index$1_arrayToHex as arrayToHex, index$1_arrayToUtf8 as arrayToUtf8, index$1_calculateSharedKey as calculateSharedKey, index$1_comparePublicKeyHex as comparePublicKeyHex, index$1_compressPublicKeyHex as compressPublicKeyHex, index$1_doDecrypt as doDecrypt, index$1_doEncrypt as doEncrypt, index$1_doSignature as doSignature, index$1_doVerifySignature as doVerifySignature, index$1_generateKeyPairHex as generateKeyPairHex, index$1_getHash as getHash, index$1_getPoint as getPoint, index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey, index$1_getZ as getZ, index$1_hexToArray as hexToArray, index$1_initRNGPool as initRNGPool, index$1_leftPad as leftPad, index$1_precomputePublicKey as precomputePublicKey, index$1_utf8ToHex as utf8ToHex, index$1_verifyPublicKey as verifyPublicKey };
|
140
140
|
}
|
141
141
|
|
142
|
+
/**
|
143
|
+
* SM3 Key derivation function used in SM2 encryption and key exchange, specified in GM/T 0003-2012
|
144
|
+
* @param z Input data (string or Uint8Array)
|
145
|
+
* @param keylen Desired key length in bytes
|
146
|
+
* @returns Derived key as Uint8Array
|
147
|
+
*/
|
148
|
+
declare function kdf(z: string | Uint8Array, keylen: number): Uint8Array<ArrayBuffer>;
|
149
|
+
|
142
150
|
declare function sm3(input: string | Uint8Array, options?: {
|
143
151
|
key: Uint8Array | string;
|
144
152
|
mode?: 'hmac' | 'mac';
|
@@ -153,9 +161,9 @@ interface SM4Options {
|
|
153
161
|
outputTag?: boolean;
|
154
162
|
tag?: Uint8Array | string;
|
155
163
|
}
|
156
|
-
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array | {
|
164
|
+
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array<ArrayBufferLike> | {
|
157
165
|
output: Uint8Array;
|
158
|
-
tag?: Uint8Array
|
166
|
+
tag?: Uint8Array;
|
159
167
|
} | {
|
160
168
|
output: string;
|
161
169
|
tag: string | undefined;
|
@@ -194,4 +202,4 @@ declare namespace index {
|
|
194
202
|
export { type index_GCMResult as GCMResult, type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
|
195
203
|
}
|
196
204
|
|
197
|
-
export { index$1 as sm2, sm3, index as sm4 };
|
205
|
+
export { kdf, index$1 as sm2, sm3, index as sm4 };
|
package/dist/index.d.ts
CHANGED
@@ -31,7 +31,7 @@ declare function arrayToUtf8(arr: Uint8Array): string;
|
|
31
31
|
/**
|
32
32
|
* 转成字节数组
|
33
33
|
*/
|
34
|
-
declare function hexToArray(hexStr: string): Uint8Array
|
34
|
+
declare function hexToArray(hexStr: string): Uint8Array<ArrayBuffer>;
|
35
35
|
/**
|
36
36
|
* 验证公钥是否为椭圆曲线上的点
|
37
37
|
*/
|
@@ -43,9 +43,9 @@ declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): bo
|
|
43
43
|
|
44
44
|
declare function initRNGPool(): Promise<void>;
|
45
45
|
|
46
|
-
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array
|
46
|
+
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array<ArrayBuffer>;
|
47
47
|
|
48
|
-
declare const EmptyArray: Uint8Array
|
48
|
+
declare const EmptyArray: Uint8Array<ArrayBuffer>;
|
49
49
|
/**
|
50
50
|
* 加密
|
51
51
|
*/
|
@@ -85,7 +85,7 @@ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, pu
|
|
85
85
|
hash?: boolean;
|
86
86
|
userId?: string;
|
87
87
|
}): boolean;
|
88
|
-
declare function getZ(publicKey: string, userId?: string): Uint8Array
|
88
|
+
declare function getZ(publicKey: string, userId?: string): Uint8Array<ArrayBufferLike>;
|
89
89
|
/**
|
90
90
|
* sm3杂凑算法
|
91
91
|
*/
|
@@ -139,6 +139,14 @@ declare namespace index$1 {
|
|
139
139
|
export { index$1_EmptyArray as EmptyArray, type index$1_KeyPair as KeyPair, type index$1_SignaturePoint as SignaturePoint, index$1_arrayToHex as arrayToHex, index$1_arrayToUtf8 as arrayToUtf8, index$1_calculateSharedKey as calculateSharedKey, index$1_comparePublicKeyHex as comparePublicKeyHex, index$1_compressPublicKeyHex as compressPublicKeyHex, index$1_doDecrypt as doDecrypt, index$1_doEncrypt as doEncrypt, index$1_doSignature as doSignature, index$1_doVerifySignature as doVerifySignature, index$1_generateKeyPairHex as generateKeyPairHex, index$1_getHash as getHash, index$1_getPoint as getPoint, index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey, index$1_getZ as getZ, index$1_hexToArray as hexToArray, index$1_initRNGPool as initRNGPool, index$1_leftPad as leftPad, index$1_precomputePublicKey as precomputePublicKey, index$1_utf8ToHex as utf8ToHex, index$1_verifyPublicKey as verifyPublicKey };
|
140
140
|
}
|
141
141
|
|
142
|
+
/**
|
143
|
+
* SM3 Key derivation function used in SM2 encryption and key exchange, specified in GM/T 0003-2012
|
144
|
+
* @param z Input data (string or Uint8Array)
|
145
|
+
* @param keylen Desired key length in bytes
|
146
|
+
* @returns Derived key as Uint8Array
|
147
|
+
*/
|
148
|
+
declare function kdf(z: string | Uint8Array, keylen: number): Uint8Array<ArrayBuffer>;
|
149
|
+
|
142
150
|
declare function sm3(input: string | Uint8Array, options?: {
|
143
151
|
key: Uint8Array | string;
|
144
152
|
mode?: 'hmac' | 'mac';
|
@@ -153,9 +161,9 @@ interface SM4Options {
|
|
153
161
|
outputTag?: boolean;
|
154
162
|
tag?: Uint8Array | string;
|
155
163
|
}
|
156
|
-
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array | {
|
164
|
+
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array<ArrayBufferLike> | {
|
157
165
|
output: Uint8Array;
|
158
|
-
tag?: Uint8Array
|
166
|
+
tag?: Uint8Array;
|
159
167
|
} | {
|
160
168
|
output: string;
|
161
169
|
tag: string | undefined;
|
@@ -194,4 +202,4 @@ declare namespace index {
|
|
194
202
|
export { type index_GCMResult as GCMResult, type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
|
195
203
|
}
|
196
204
|
|
197
|
-
export { index$1 as sm2, sm3, index as sm4 };
|
205
|
+
export { kdf, index$1 as sm2, sm3, index as sm4 };
|
package/dist/index.js
CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: tr
|
|
30
30
|
// src/index.ts
|
31
31
|
var src_exports = {};
|
32
32
|
__export(src_exports, {
|
33
|
+
kdf: () => kdf,
|
33
34
|
sm2: () => sm2_exports,
|
34
35
|
sm3: () => sm32,
|
35
36
|
sm4: () => sm4_exports
|
@@ -303,7 +304,10 @@ var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLengt
|
|
303
304
|
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
304
305
|
if (!isLE)
|
305
306
|
throw new Error("Non little-endian hardware is not supported");
|
306
|
-
var hexes = Array.from(
|
307
|
+
var hexes = Array.from(
|
308
|
+
{ length: 256 },
|
309
|
+
(v, i) => i.toString(16).padStart(2, "0")
|
310
|
+
);
|
307
311
|
function bytesToHex(bytes) {
|
308
312
|
if (!u8a(bytes))
|
309
313
|
throw new Error("Uint8Array expected");
|
@@ -313,14 +317,44 @@ function bytesToHex(bytes) {
|
|
313
317
|
}
|
314
318
|
return hex;
|
315
319
|
}
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
+
var te = typeof TextEncoder != "undefined" && /* @__PURE__ */ new TextEncoder();
|
321
|
+
var slc = (v, s, e) => {
|
322
|
+
if (s == null || s < 0)
|
323
|
+
s = 0;
|
324
|
+
if (e == null || e > v.length)
|
325
|
+
e = v.length;
|
326
|
+
return new Uint8Array(v.subarray(s, e));
|
327
|
+
};
|
328
|
+
function strToU8(str) {
|
329
|
+
if (te)
|
330
|
+
return te.encode(str);
|
331
|
+
const l = str.length;
|
332
|
+
let ar = new Uint8Array(str.length + (str.length >> 1));
|
333
|
+
let ai = 0;
|
334
|
+
const w = (v) => {
|
335
|
+
ar[ai++] = v;
|
336
|
+
};
|
337
|
+
for (let i = 0; i < l; ++i) {
|
338
|
+
if (ai + 5 > ar.length) {
|
339
|
+
const n = new Uint8Array(ai + 8 + (l - i << 1));
|
340
|
+
n.set(ar);
|
341
|
+
ar = n;
|
342
|
+
}
|
343
|
+
let c = str.charCodeAt(i);
|
344
|
+
if (c < 128)
|
345
|
+
w(c);
|
346
|
+
else if (c < 2048)
|
347
|
+
w(192 | c >> 6), w(128 | c & 63);
|
348
|
+
else if (c > 55295 && c < 57344)
|
349
|
+
c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);
|
350
|
+
else
|
351
|
+
w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
|
352
|
+
}
|
353
|
+
return slc(ar, 0, ai);
|
320
354
|
}
|
321
355
|
function toBytes(data) {
|
322
356
|
if (typeof data === "string")
|
323
|
-
data =
|
357
|
+
data = strToU8(data);
|
324
358
|
if (!u8a(data))
|
325
359
|
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
326
360
|
return data;
|
@@ -636,7 +670,7 @@ function compressPublicKeyHex(s) {
|
|
636
670
|
return prefix + xHex;
|
637
671
|
}
|
638
672
|
function utf8ToHex(input) {
|
639
|
-
const bytes =
|
673
|
+
const bytes = strToU8(input);
|
640
674
|
return utils2.bytesToHex(bytes);
|
641
675
|
}
|
642
676
|
function leftPad(input, num) {
|
@@ -703,13 +737,56 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
703
737
|
}
|
704
738
|
|
705
739
|
// src/sm2/index.ts
|
706
|
-
var
|
740
|
+
var utils5 = __toESM(require("@noble/curves/abstract/utils"));
|
707
741
|
|
708
|
-
// src/sm2/
|
742
|
+
// src/sm2/kdf.ts
|
709
743
|
var utils3 = __toESM(require("@noble/curves/abstract/utils"));
|
710
|
-
|
711
|
-
|
712
|
-
function
|
744
|
+
|
745
|
+
// src/sm3/index.ts
|
746
|
+
function utf8ToArray(str) {
|
747
|
+
const arr = [];
|
748
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
749
|
+
const point = str.codePointAt(i);
|
750
|
+
if (point <= 127) {
|
751
|
+
arr.push(point);
|
752
|
+
} else if (point <= 2047) {
|
753
|
+
arr.push(192 | point >>> 6);
|
754
|
+
arr.push(128 | point & 63);
|
755
|
+
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
756
|
+
arr.push(224 | point >>> 12);
|
757
|
+
arr.push(128 | point >>> 6 & 63);
|
758
|
+
arr.push(128 | point & 63);
|
759
|
+
} else if (point >= 65536 && point <= 1114111) {
|
760
|
+
i++;
|
761
|
+
arr.push(240 | point >>> 18 & 28);
|
762
|
+
arr.push(128 | point >>> 12 & 63);
|
763
|
+
arr.push(128 | point >>> 6 & 63);
|
764
|
+
arr.push(128 | point & 63);
|
765
|
+
} else {
|
766
|
+
arr.push(point);
|
767
|
+
throw new Error("input is not supported");
|
768
|
+
}
|
769
|
+
}
|
770
|
+
return new Uint8Array(arr);
|
771
|
+
}
|
772
|
+
function sm32(input, options) {
|
773
|
+
input = typeof input === "string" ? utf8ToArray(input) : input;
|
774
|
+
if (options) {
|
775
|
+
const mode = options.mode || "hmac";
|
776
|
+
if (mode !== "hmac")
|
777
|
+
throw new Error("invalid mode");
|
778
|
+
let key = options.key;
|
779
|
+
if (!key)
|
780
|
+
throw new Error("invalid key");
|
781
|
+
key = typeof key === "string" ? hexToArray(key) : key;
|
782
|
+
return bytesToHex(hmac(sm3, key, input));
|
783
|
+
}
|
784
|
+
return bytesToHex(sm3(input));
|
785
|
+
}
|
786
|
+
|
787
|
+
// src/sm2/kdf.ts
|
788
|
+
function kdf(z, keylen) {
|
789
|
+
z = typeof z === "string" ? utf8ToArray(z) : z;
|
713
790
|
let msg = new Uint8Array(keylen);
|
714
791
|
let ct = 1;
|
715
792
|
let offset = 0;
|
@@ -732,6 +809,11 @@ function hkdf(z, keylen) {
|
|
732
809
|
}
|
733
810
|
return msg;
|
734
811
|
}
|
812
|
+
|
813
|
+
// src/sm2/kx.ts
|
814
|
+
var utils4 = __toESM(require("@noble/curves/abstract/utils"));
|
815
|
+
var wPow2 = utils4.hexToNumber("80000000000000000000000000000000");
|
816
|
+
var wPow2Sub1 = utils4.hexToNumber("7fffffffffffffffffffffffffffffff");
|
735
817
|
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
736
818
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
737
819
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
@@ -741,35 +823,41 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
|
|
741
823
|
if (isRecipient) {
|
742
824
|
[ZA, ZB] = [ZB, ZA];
|
743
825
|
}
|
744
|
-
const rA =
|
745
|
-
const dA =
|
826
|
+
const rA = utils4.hexToNumber(ephemeralKeypairA.privateKey);
|
827
|
+
const dA = utils4.hexToNumber(keypairA.privateKey);
|
746
828
|
const x1 = RA.x;
|
747
829
|
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
748
830
|
const tA = field.add(dA, field.mulN(x1_, rA));
|
749
831
|
const x2 = RB.x;
|
750
832
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
751
833
|
const U = RB.multiply(x2_).add(PB).multiply(tA);
|
752
|
-
const xU = hexToArray(leftPad(
|
753
|
-
const yU = hexToArray(leftPad(
|
754
|
-
const KA =
|
834
|
+
const xU = hexToArray(leftPad(utils4.numberToHexUnpadded(U.x), 64));
|
835
|
+
const yU = hexToArray(leftPad(utils4.numberToHexUnpadded(U.y), 64));
|
836
|
+
const KA = kdf(utils4.concatBytes(xU, yU, ZA, ZB), sharedKeyLength);
|
755
837
|
return KA;
|
756
838
|
}
|
757
839
|
|
758
840
|
// src/sm2/index.ts
|
841
|
+
function xorCipherStream(x2, y2, msg) {
|
842
|
+
const stream = kdf(utils5.concatBytes(x2, y2), msg.length);
|
843
|
+
for (let i = 0, len = msg.length; i < len; i++) {
|
844
|
+
msg[i] ^= stream[i] & 255;
|
845
|
+
}
|
846
|
+
}
|
759
847
|
var C1C2C3 = 0;
|
760
848
|
var EmptyArray = new Uint8Array();
|
761
849
|
function doEncrypt(msg, publicKey, cipherMode = 1, options) {
|
762
850
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
763
851
|
const publicKeyPoint = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
|
764
852
|
const keypair = generateKeyPairHex();
|
765
|
-
const k =
|
853
|
+
const k = utils5.hexToNumber(keypair.privateKey);
|
766
854
|
let c1 = keypair.publicKey;
|
767
855
|
if (c1.length > 128)
|
768
856
|
c1 = c1.substring(c1.length - 128);
|
769
857
|
const p = publicKeyPoint.multiply(k);
|
770
|
-
const x2 = hexToArray(leftPad(
|
771
|
-
const y2 = hexToArray(leftPad(
|
772
|
-
const c3 = bytesToHex(sm3(
|
858
|
+
const x2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.x), 64));
|
859
|
+
const y2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.y), 64));
|
860
|
+
const c3 = bytesToHex(sm3(utils5.concatBytes(x2, msgArr, y2)));
|
773
861
|
xorCipherStream(x2, y2, msgArr);
|
774
862
|
const c2 = bytesToHex(msgArr);
|
775
863
|
if (options?.asn1) {
|
@@ -779,30 +867,9 @@ function doEncrypt(msg, publicKey, cipherMode = 1, options) {
|
|
779
867
|
}
|
780
868
|
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
781
869
|
}
|
782
|
-
function xorCipherStream(x2, y2, msg) {
|
783
|
-
let ct = 1;
|
784
|
-
let offset = 0;
|
785
|
-
let t = EmptyArray;
|
786
|
-
const ctShift = new Uint8Array(4);
|
787
|
-
const nextT = () => {
|
788
|
-
ctShift[0] = ct >> 24 & 255;
|
789
|
-
ctShift[1] = ct >> 16 & 255;
|
790
|
-
ctShift[2] = ct >> 8 & 255;
|
791
|
-
ctShift[3] = ct & 255;
|
792
|
-
t = sm3(utils4.concatBytes(x2, y2, ctShift));
|
793
|
-
ct++;
|
794
|
-
offset = 0;
|
795
|
-
};
|
796
|
-
nextT();
|
797
|
-
for (let i = 0, len = msg.length; i < len; i++) {
|
798
|
-
if (offset === t.length)
|
799
|
-
nextT();
|
800
|
-
msg[i] ^= t[offset++] & 255;
|
801
|
-
}
|
802
|
-
}
|
803
870
|
function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
804
871
|
const { output = "string", asn1 = false } = options || {};
|
805
|
-
const privateKeyInteger =
|
872
|
+
const privateKeyInteger = utils5.hexToNumber(privateKey);
|
806
873
|
let c1;
|
807
874
|
let c2;
|
808
875
|
let c3;
|
@@ -825,10 +892,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
|
825
892
|
}
|
826
893
|
const msg = hexToArray(c2);
|
827
894
|
const p = c1.multiply(privateKeyInteger);
|
828
|
-
const x2 = hexToArray(leftPad(
|
829
|
-
const y2 = hexToArray(leftPad(
|
895
|
+
const x2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.x), 64));
|
896
|
+
const y2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.y), 64));
|
830
897
|
xorCipherStream(x2, y2, msg);
|
831
|
-
const checkC3 = arrayToHex(Array.from(sm3(
|
898
|
+
const checkC3 = arrayToHex(Array.from(sm3(utils5.concatBytes(x2, msg, y2))));
|
832
899
|
if (checkC3 === c3.toLowerCase()) {
|
833
900
|
return output === "array" ? msg : arrayToUtf8(msg);
|
834
901
|
} else {
|
@@ -848,8 +915,8 @@ function doSignature(msg, privateKey, options = {}) {
|
|
848
915
|
publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
|
849
916
|
hashHex = getHash(hashHex, publicKey, userId);
|
850
917
|
}
|
851
|
-
const dA =
|
852
|
-
const e =
|
918
|
+
const dA = utils5.hexToNumber(privateKey);
|
919
|
+
const e = utils5.hexToNumber(hashHex);
|
853
920
|
let k = null;
|
854
921
|
let r = null;
|
855
922
|
let s = null;
|
@@ -868,7 +935,7 @@ function doSignature(msg, privateKey, options = {}) {
|
|
868
935
|
} while (s === ZERO);
|
869
936
|
if (der)
|
870
937
|
return encodeDer(r, s);
|
871
|
-
return leftPad(
|
938
|
+
return leftPad(utils5.numberToHexUnpadded(r), 64) + leftPad(utils5.numberToHexUnpadded(s), 64);
|
872
939
|
}
|
873
940
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
874
941
|
let hashHex;
|
@@ -890,11 +957,11 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
890
957
|
r = decodeDerObj.r;
|
891
958
|
s = decodeDerObj.s;
|
892
959
|
} else {
|
893
|
-
r =
|
894
|
-
s =
|
960
|
+
r = utils5.hexToNumber(signHex.substring(0, 64));
|
961
|
+
s = utils5.hexToNumber(signHex.substring(64));
|
895
962
|
}
|
896
963
|
const PA = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
|
897
|
-
const e =
|
964
|
+
const e = utils5.hexToNumber(hashHex);
|
898
965
|
const t = field.add(r, s);
|
899
966
|
if (t === ZERO)
|
900
967
|
return false;
|
@@ -904,10 +971,10 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
904
971
|
}
|
905
972
|
function getZ(publicKey, userId = "1234567812345678") {
|
906
973
|
userId = utf8ToHex(userId);
|
907
|
-
const a = leftPad(
|
908
|
-
const b = leftPad(
|
909
|
-
const gx = leftPad(
|
910
|
-
const gy = leftPad(
|
974
|
+
const a = leftPad(utils5.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
975
|
+
const b = leftPad(utils5.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
976
|
+
const gx = leftPad(utils5.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
|
977
|
+
const gy = leftPad(utils5.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
|
911
978
|
let px;
|
912
979
|
let py;
|
913
980
|
if (publicKey.length === 128) {
|
@@ -915,17 +982,17 @@ function getZ(publicKey, userId = "1234567812345678") {
|
|
915
982
|
py = publicKey.substring(64, 128);
|
916
983
|
} else {
|
917
984
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
918
|
-
px = leftPad(
|
919
|
-
py = leftPad(
|
985
|
+
px = leftPad(utils5.numberToHexUnpadded(point.x), 64);
|
986
|
+
py = leftPad(utils5.numberToHexUnpadded(point.y), 64);
|
920
987
|
}
|
921
988
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
922
989
|
const entl = userId.length * 4;
|
923
|
-
const z = sm3(
|
990
|
+
const z = sm3(utils5.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
924
991
|
return z;
|
925
992
|
}
|
926
993
|
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
927
994
|
const z = getZ(publicKey, userId);
|
928
|
-
return bytesToHex(sm3(
|
995
|
+
return bytesToHex(sm3(utils5.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
929
996
|
}
|
930
997
|
function precomputePublicKey(publicKey, windowSize) {
|
931
998
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
@@ -933,13 +1000,13 @@ function precomputePublicKey(publicKey, windowSize) {
|
|
933
1000
|
}
|
934
1001
|
function getPublicKeyFromPrivateKey(privateKey) {
|
935
1002
|
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
936
|
-
const pubPad = leftPad(
|
1003
|
+
const pubPad = leftPad(utils5.bytesToHex(pubKey), 64);
|
937
1004
|
return pubPad;
|
938
1005
|
}
|
939
1006
|
function getPoint() {
|
940
1007
|
const keypair = generateKeyPairHex();
|
941
1008
|
const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey);
|
942
|
-
const k =
|
1009
|
+
const k = utils5.hexToNumber(keypair.privateKey);
|
943
1010
|
return {
|
944
1011
|
...keypair,
|
945
1012
|
k,
|
@@ -947,48 +1014,6 @@ function getPoint() {
|
|
947
1014
|
};
|
948
1015
|
}
|
949
1016
|
|
950
|
-
// src/sm3/index.ts
|
951
|
-
function utf8ToArray(str) {
|
952
|
-
const arr = [];
|
953
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
954
|
-
const point = str.codePointAt(i);
|
955
|
-
if (point <= 127) {
|
956
|
-
arr.push(point);
|
957
|
-
} else if (point <= 2047) {
|
958
|
-
arr.push(192 | point >>> 6);
|
959
|
-
arr.push(128 | point & 63);
|
960
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
961
|
-
arr.push(224 | point >>> 12);
|
962
|
-
arr.push(128 | point >>> 6 & 63);
|
963
|
-
arr.push(128 | point & 63);
|
964
|
-
} else if (point >= 65536 && point <= 1114111) {
|
965
|
-
i++;
|
966
|
-
arr.push(240 | point >>> 18 & 28);
|
967
|
-
arr.push(128 | point >>> 12 & 63);
|
968
|
-
arr.push(128 | point >>> 6 & 63);
|
969
|
-
arr.push(128 | point & 63);
|
970
|
-
} else {
|
971
|
-
arr.push(point);
|
972
|
-
throw new Error("input is not supported");
|
973
|
-
}
|
974
|
-
}
|
975
|
-
return new Uint8Array(arr);
|
976
|
-
}
|
977
|
-
function sm32(input, options) {
|
978
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
979
|
-
if (options) {
|
980
|
-
const mode = options.mode || "hmac";
|
981
|
-
if (mode !== "hmac")
|
982
|
-
throw new Error("invalid mode");
|
983
|
-
let key = options.key;
|
984
|
-
if (!key)
|
985
|
-
throw new Error("invalid key");
|
986
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
987
|
-
return bytesToHex(hmac(sm3, key, input));
|
988
|
-
}
|
989
|
-
return bytesToHex(sm3(input));
|
990
|
-
}
|
991
|
-
|
992
1017
|
// src/sm4/index.ts
|
993
1018
|
var sm4_exports = {};
|
994
1019
|
__export(sm4_exports, {
|
@@ -997,7 +1022,7 @@ __export(sm4_exports, {
|
|
997
1022
|
sm4: () => sm4
|
998
1023
|
});
|
999
1024
|
var import_polyval = require("@noble/ciphers/_polyval");
|
1000
|
-
var
|
1025
|
+
var import_utils12 = require("@noble/ciphers/utils");
|
1001
1026
|
var DECRYPT = 0;
|
1002
1027
|
var ROUND = 32;
|
1003
1028
|
var BLOCK = 16;
|
@@ -1409,8 +1434,8 @@ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
|
|
1409
1434
|
const g = import_polyval.ghash.create(h2);
|
1410
1435
|
g.update(ivArray);
|
1411
1436
|
const lenIv = new Uint8Array(16);
|
1412
|
-
const view = (0,
|
1413
|
-
(0,
|
1437
|
+
const view = (0, import_utils12.createView)(lenIv);
|
1438
|
+
(0, import_utils12.setBigUint64)(view, 8, BigInt(ivArray.length * 8), false);
|
1414
1439
|
g.update(lenIv);
|
1415
1440
|
j02 = g.digest();
|
1416
1441
|
}
|
@@ -1429,9 +1454,9 @@ function sm4Gcm(inArray, key, ivArray, aadArray, cryptFlag, tagArray) {
|
|
1429
1454
|
}
|
1430
1455
|
g.update(data);
|
1431
1456
|
const lenBlock = new Uint8Array(16);
|
1432
|
-
const view = (0,
|
1433
|
-
(0,
|
1434
|
-
(0,
|
1457
|
+
const view = (0, import_utils12.createView)(lenBlock);
|
1458
|
+
(0, import_utils12.setBigUint64)(view, 0, BigInt(aadLength * 8), false);
|
1459
|
+
(0, import_utils12.setBigUint64)(view, 8, BigInt(dataLength * 8), false);
|
1435
1460
|
g.update(lenBlock);
|
1436
1461
|
return g.digest();
|
1437
1462
|
}
|
@@ -1617,6 +1642,7 @@ function decrypt(inArray, key, options = {}) {
|
|
1617
1642
|
}
|
1618
1643
|
// Annotate the CommonJS export names for ESM import in node:
|
1619
1644
|
0 && (module.exports = {
|
1645
|
+
kdf,
|
1620
1646
|
sm2,
|
1621
1647
|
sm3,
|
1622
1648
|
sm4
|
package/dist/index.mjs
CHANGED
@@ -271,7 +271,10 @@ var createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLengt
|
|
271
271
|
var isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
272
272
|
if (!isLE)
|
273
273
|
throw new Error("Non little-endian hardware is not supported");
|
274
|
-
var hexes = Array.from(
|
274
|
+
var hexes = Array.from(
|
275
|
+
{ length: 256 },
|
276
|
+
(v, i) => i.toString(16).padStart(2, "0")
|
277
|
+
);
|
275
278
|
function bytesToHex(bytes) {
|
276
279
|
if (!u8a(bytes))
|
277
280
|
throw new Error("Uint8Array expected");
|
@@ -281,14 +284,44 @@ function bytesToHex(bytes) {
|
|
281
284
|
}
|
282
285
|
return hex;
|
283
286
|
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
287
|
+
var te = typeof TextEncoder != "undefined" && /* @__PURE__ */ new TextEncoder();
|
288
|
+
var slc = (v, s, e) => {
|
289
|
+
if (s == null || s < 0)
|
290
|
+
s = 0;
|
291
|
+
if (e == null || e > v.length)
|
292
|
+
e = v.length;
|
293
|
+
return new Uint8Array(v.subarray(s, e));
|
294
|
+
};
|
295
|
+
function strToU8(str) {
|
296
|
+
if (te)
|
297
|
+
return te.encode(str);
|
298
|
+
const l = str.length;
|
299
|
+
let ar = new Uint8Array(str.length + (str.length >> 1));
|
300
|
+
let ai = 0;
|
301
|
+
const w = (v) => {
|
302
|
+
ar[ai++] = v;
|
303
|
+
};
|
304
|
+
for (let i = 0; i < l; ++i) {
|
305
|
+
if (ai + 5 > ar.length) {
|
306
|
+
const n = new Uint8Array(ai + 8 + (l - i << 1));
|
307
|
+
n.set(ar);
|
308
|
+
ar = n;
|
309
|
+
}
|
310
|
+
let c = str.charCodeAt(i);
|
311
|
+
if (c < 128)
|
312
|
+
w(c);
|
313
|
+
else if (c < 2048)
|
314
|
+
w(192 | c >> 6), w(128 | c & 63);
|
315
|
+
else if (c > 55295 && c < 57344)
|
316
|
+
c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);
|
317
|
+
else
|
318
|
+
w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
|
319
|
+
}
|
320
|
+
return slc(ar, 0, ai);
|
288
321
|
}
|
289
322
|
function toBytes(data) {
|
290
323
|
if (typeof data === "string")
|
291
|
-
data =
|
324
|
+
data = strToU8(data);
|
292
325
|
if (!u8a(data))
|
293
326
|
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
294
327
|
return data;
|
@@ -604,7 +637,7 @@ function compressPublicKeyHex(s) {
|
|
604
637
|
return prefix + xHex;
|
605
638
|
}
|
606
639
|
function utf8ToHex(input) {
|
607
|
-
const bytes =
|
640
|
+
const bytes = strToU8(input);
|
608
641
|
return utils2.bytesToHex(bytes);
|
609
642
|
}
|
610
643
|
function leftPad(input, num) {
|
@@ -671,13 +704,56 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
671
704
|
}
|
672
705
|
|
673
706
|
// src/sm2/index.ts
|
674
|
-
import * as
|
707
|
+
import * as utils5 from "@noble/curves/abstract/utils";
|
675
708
|
|
676
|
-
// src/sm2/
|
709
|
+
// src/sm2/kdf.ts
|
677
710
|
import * as utils3 from "@noble/curves/abstract/utils";
|
678
|
-
|
679
|
-
|
680
|
-
function
|
711
|
+
|
712
|
+
// src/sm3/index.ts
|
713
|
+
function utf8ToArray(str) {
|
714
|
+
const arr = [];
|
715
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
716
|
+
const point = str.codePointAt(i);
|
717
|
+
if (point <= 127) {
|
718
|
+
arr.push(point);
|
719
|
+
} else if (point <= 2047) {
|
720
|
+
arr.push(192 | point >>> 6);
|
721
|
+
arr.push(128 | point & 63);
|
722
|
+
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
723
|
+
arr.push(224 | point >>> 12);
|
724
|
+
arr.push(128 | point >>> 6 & 63);
|
725
|
+
arr.push(128 | point & 63);
|
726
|
+
} else if (point >= 65536 && point <= 1114111) {
|
727
|
+
i++;
|
728
|
+
arr.push(240 | point >>> 18 & 28);
|
729
|
+
arr.push(128 | point >>> 12 & 63);
|
730
|
+
arr.push(128 | point >>> 6 & 63);
|
731
|
+
arr.push(128 | point & 63);
|
732
|
+
} else {
|
733
|
+
arr.push(point);
|
734
|
+
throw new Error("input is not supported");
|
735
|
+
}
|
736
|
+
}
|
737
|
+
return new Uint8Array(arr);
|
738
|
+
}
|
739
|
+
function sm32(input, options) {
|
740
|
+
input = typeof input === "string" ? utf8ToArray(input) : input;
|
741
|
+
if (options) {
|
742
|
+
const mode = options.mode || "hmac";
|
743
|
+
if (mode !== "hmac")
|
744
|
+
throw new Error("invalid mode");
|
745
|
+
let key = options.key;
|
746
|
+
if (!key)
|
747
|
+
throw new Error("invalid key");
|
748
|
+
key = typeof key === "string" ? hexToArray(key) : key;
|
749
|
+
return bytesToHex(hmac(sm3, key, input));
|
750
|
+
}
|
751
|
+
return bytesToHex(sm3(input));
|
752
|
+
}
|
753
|
+
|
754
|
+
// src/sm2/kdf.ts
|
755
|
+
function kdf(z, keylen) {
|
756
|
+
z = typeof z === "string" ? utf8ToArray(z) : z;
|
681
757
|
let msg = new Uint8Array(keylen);
|
682
758
|
let ct = 1;
|
683
759
|
let offset = 0;
|
@@ -700,6 +776,11 @@ function hkdf(z, keylen) {
|
|
700
776
|
}
|
701
777
|
return msg;
|
702
778
|
}
|
779
|
+
|
780
|
+
// src/sm2/kx.ts
|
781
|
+
import * as utils4 from "@noble/curves/abstract/utils";
|
782
|
+
var wPow2 = utils4.hexToNumber("80000000000000000000000000000000");
|
783
|
+
var wPow2Sub1 = utils4.hexToNumber("7fffffffffffffffffffffffffffffff");
|
703
784
|
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
704
785
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
705
786
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
@@ -709,35 +790,41 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
|
|
709
790
|
if (isRecipient) {
|
710
791
|
[ZA, ZB] = [ZB, ZA];
|
711
792
|
}
|
712
|
-
const rA =
|
713
|
-
const dA =
|
793
|
+
const rA = utils4.hexToNumber(ephemeralKeypairA.privateKey);
|
794
|
+
const dA = utils4.hexToNumber(keypairA.privateKey);
|
714
795
|
const x1 = RA.x;
|
715
796
|
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
716
797
|
const tA = field.add(dA, field.mulN(x1_, rA));
|
717
798
|
const x2 = RB.x;
|
718
799
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
719
800
|
const U = RB.multiply(x2_).add(PB).multiply(tA);
|
720
|
-
const xU = hexToArray(leftPad(
|
721
|
-
const yU = hexToArray(leftPad(
|
722
|
-
const KA =
|
801
|
+
const xU = hexToArray(leftPad(utils4.numberToHexUnpadded(U.x), 64));
|
802
|
+
const yU = hexToArray(leftPad(utils4.numberToHexUnpadded(U.y), 64));
|
803
|
+
const KA = kdf(utils4.concatBytes(xU, yU, ZA, ZB), sharedKeyLength);
|
723
804
|
return KA;
|
724
805
|
}
|
725
806
|
|
726
807
|
// src/sm2/index.ts
|
808
|
+
function xorCipherStream(x2, y2, msg) {
|
809
|
+
const stream = kdf(utils5.concatBytes(x2, y2), msg.length);
|
810
|
+
for (let i = 0, len = msg.length; i < len; i++) {
|
811
|
+
msg[i] ^= stream[i] & 255;
|
812
|
+
}
|
813
|
+
}
|
727
814
|
var C1C2C3 = 0;
|
728
815
|
var EmptyArray = new Uint8Array();
|
729
816
|
function doEncrypt(msg, publicKey, cipherMode = 1, options) {
|
730
817
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
731
818
|
const publicKeyPoint = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
|
732
819
|
const keypair = generateKeyPairHex();
|
733
|
-
const k =
|
820
|
+
const k = utils5.hexToNumber(keypair.privateKey);
|
734
821
|
let c1 = keypair.publicKey;
|
735
822
|
if (c1.length > 128)
|
736
823
|
c1 = c1.substring(c1.length - 128);
|
737
824
|
const p = publicKeyPoint.multiply(k);
|
738
|
-
const x2 = hexToArray(leftPad(
|
739
|
-
const y2 = hexToArray(leftPad(
|
740
|
-
const c3 = bytesToHex(sm3(
|
825
|
+
const x2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.x), 64));
|
826
|
+
const y2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.y), 64));
|
827
|
+
const c3 = bytesToHex(sm3(utils5.concatBytes(x2, msgArr, y2)));
|
741
828
|
xorCipherStream(x2, y2, msgArr);
|
742
829
|
const c2 = bytesToHex(msgArr);
|
743
830
|
if (options?.asn1) {
|
@@ -747,30 +834,9 @@ function doEncrypt(msg, publicKey, cipherMode = 1, options) {
|
|
747
834
|
}
|
748
835
|
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
749
836
|
}
|
750
|
-
function xorCipherStream(x2, y2, msg) {
|
751
|
-
let ct = 1;
|
752
|
-
let offset = 0;
|
753
|
-
let t = EmptyArray;
|
754
|
-
const ctShift = new Uint8Array(4);
|
755
|
-
const nextT = () => {
|
756
|
-
ctShift[0] = ct >> 24 & 255;
|
757
|
-
ctShift[1] = ct >> 16 & 255;
|
758
|
-
ctShift[2] = ct >> 8 & 255;
|
759
|
-
ctShift[3] = ct & 255;
|
760
|
-
t = sm3(utils4.concatBytes(x2, y2, ctShift));
|
761
|
-
ct++;
|
762
|
-
offset = 0;
|
763
|
-
};
|
764
|
-
nextT();
|
765
|
-
for (let i = 0, len = msg.length; i < len; i++) {
|
766
|
-
if (offset === t.length)
|
767
|
-
nextT();
|
768
|
-
msg[i] ^= t[offset++] & 255;
|
769
|
-
}
|
770
|
-
}
|
771
837
|
function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
772
838
|
const { output = "string", asn1 = false } = options || {};
|
773
|
-
const privateKeyInteger =
|
839
|
+
const privateKeyInteger = utils5.hexToNumber(privateKey);
|
774
840
|
let c1;
|
775
841
|
let c2;
|
776
842
|
let c3;
|
@@ -793,10 +859,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
|
793
859
|
}
|
794
860
|
const msg = hexToArray(c2);
|
795
861
|
const p = c1.multiply(privateKeyInteger);
|
796
|
-
const x2 = hexToArray(leftPad(
|
797
|
-
const y2 = hexToArray(leftPad(
|
862
|
+
const x2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.x), 64));
|
863
|
+
const y2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.y), 64));
|
798
864
|
xorCipherStream(x2, y2, msg);
|
799
|
-
const checkC3 = arrayToHex(Array.from(sm3(
|
865
|
+
const checkC3 = arrayToHex(Array.from(sm3(utils5.concatBytes(x2, msg, y2))));
|
800
866
|
if (checkC3 === c3.toLowerCase()) {
|
801
867
|
return output === "array" ? msg : arrayToUtf8(msg);
|
802
868
|
} else {
|
@@ -816,8 +882,8 @@ function doSignature(msg, privateKey, options = {}) {
|
|
816
882
|
publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
|
817
883
|
hashHex = getHash(hashHex, publicKey, userId);
|
818
884
|
}
|
819
|
-
const dA =
|
820
|
-
const e =
|
885
|
+
const dA = utils5.hexToNumber(privateKey);
|
886
|
+
const e = utils5.hexToNumber(hashHex);
|
821
887
|
let k = null;
|
822
888
|
let r = null;
|
823
889
|
let s = null;
|
@@ -836,7 +902,7 @@ function doSignature(msg, privateKey, options = {}) {
|
|
836
902
|
} while (s === ZERO);
|
837
903
|
if (der)
|
838
904
|
return encodeDer(r, s);
|
839
|
-
return leftPad(
|
905
|
+
return leftPad(utils5.numberToHexUnpadded(r), 64) + leftPad(utils5.numberToHexUnpadded(s), 64);
|
840
906
|
}
|
841
907
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
842
908
|
let hashHex;
|
@@ -858,11 +924,11 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
858
924
|
r = decodeDerObj.r;
|
859
925
|
s = decodeDerObj.s;
|
860
926
|
} else {
|
861
|
-
r =
|
862
|
-
s =
|
927
|
+
r = utils5.hexToNumber(signHex.substring(0, 64));
|
928
|
+
s = utils5.hexToNumber(signHex.substring(64));
|
863
929
|
}
|
864
930
|
const PA = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
|
865
|
-
const e =
|
931
|
+
const e = utils5.hexToNumber(hashHex);
|
866
932
|
const t = field.add(r, s);
|
867
933
|
if (t === ZERO)
|
868
934
|
return false;
|
@@ -872,10 +938,10 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
872
938
|
}
|
873
939
|
function getZ(publicKey, userId = "1234567812345678") {
|
874
940
|
userId = utf8ToHex(userId);
|
875
|
-
const a = leftPad(
|
876
|
-
const b = leftPad(
|
877
|
-
const gx = leftPad(
|
878
|
-
const gy = leftPad(
|
941
|
+
const a = leftPad(utils5.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
942
|
+
const b = leftPad(utils5.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
943
|
+
const gx = leftPad(utils5.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64);
|
944
|
+
const gy = leftPad(utils5.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64);
|
879
945
|
let px;
|
880
946
|
let py;
|
881
947
|
if (publicKey.length === 128) {
|
@@ -883,17 +949,17 @@ function getZ(publicKey, userId = "1234567812345678") {
|
|
883
949
|
py = publicKey.substring(64, 128);
|
884
950
|
} else {
|
885
951
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
886
|
-
px = leftPad(
|
887
|
-
py = leftPad(
|
952
|
+
px = leftPad(utils5.numberToHexUnpadded(point.x), 64);
|
953
|
+
py = leftPad(utils5.numberToHexUnpadded(point.y), 64);
|
888
954
|
}
|
889
955
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
890
956
|
const entl = userId.length * 4;
|
891
|
-
const z = sm3(
|
957
|
+
const z = sm3(utils5.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
892
958
|
return z;
|
893
959
|
}
|
894
960
|
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
895
961
|
const z = getZ(publicKey, userId);
|
896
|
-
return bytesToHex(sm3(
|
962
|
+
return bytesToHex(sm3(utils5.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
897
963
|
}
|
898
964
|
function precomputePublicKey(publicKey, windowSize) {
|
899
965
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
@@ -901,13 +967,13 @@ function precomputePublicKey(publicKey, windowSize) {
|
|
901
967
|
}
|
902
968
|
function getPublicKeyFromPrivateKey(privateKey) {
|
903
969
|
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
904
|
-
const pubPad = leftPad(
|
970
|
+
const pubPad = leftPad(utils5.bytesToHex(pubKey), 64);
|
905
971
|
return pubPad;
|
906
972
|
}
|
907
973
|
function getPoint() {
|
908
974
|
const keypair = generateKeyPairHex();
|
909
975
|
const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey);
|
910
|
-
const k =
|
976
|
+
const k = utils5.hexToNumber(keypair.privateKey);
|
911
977
|
return {
|
912
978
|
...keypair,
|
913
979
|
k,
|
@@ -915,48 +981,6 @@ function getPoint() {
|
|
915
981
|
};
|
916
982
|
}
|
917
983
|
|
918
|
-
// src/sm3/index.ts
|
919
|
-
function utf8ToArray(str) {
|
920
|
-
const arr = [];
|
921
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
922
|
-
const point = str.codePointAt(i);
|
923
|
-
if (point <= 127) {
|
924
|
-
arr.push(point);
|
925
|
-
} else if (point <= 2047) {
|
926
|
-
arr.push(192 | point >>> 6);
|
927
|
-
arr.push(128 | point & 63);
|
928
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
929
|
-
arr.push(224 | point >>> 12);
|
930
|
-
arr.push(128 | point >>> 6 & 63);
|
931
|
-
arr.push(128 | point & 63);
|
932
|
-
} else if (point >= 65536 && point <= 1114111) {
|
933
|
-
i++;
|
934
|
-
arr.push(240 | point >>> 18 & 28);
|
935
|
-
arr.push(128 | point >>> 12 & 63);
|
936
|
-
arr.push(128 | point >>> 6 & 63);
|
937
|
-
arr.push(128 | point & 63);
|
938
|
-
} else {
|
939
|
-
arr.push(point);
|
940
|
-
throw new Error("input is not supported");
|
941
|
-
}
|
942
|
-
}
|
943
|
-
return new Uint8Array(arr);
|
944
|
-
}
|
945
|
-
function sm32(input, options) {
|
946
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
947
|
-
if (options) {
|
948
|
-
const mode = options.mode || "hmac";
|
949
|
-
if (mode !== "hmac")
|
950
|
-
throw new Error("invalid mode");
|
951
|
-
let key = options.key;
|
952
|
-
if (!key)
|
953
|
-
throw new Error("invalid key");
|
954
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
955
|
-
return bytesToHex(hmac(sm3, key, input));
|
956
|
-
}
|
957
|
-
return bytesToHex(sm3(input));
|
958
|
-
}
|
959
|
-
|
960
984
|
// src/sm4/index.ts
|
961
985
|
var sm4_exports = {};
|
962
986
|
__export(sm4_exports, {
|
@@ -1584,6 +1608,7 @@ function decrypt(inArray, key, options = {}) {
|
|
1584
1608
|
return sm4(inArray, key, 0, options);
|
1585
1609
|
}
|
1586
1610
|
export {
|
1611
|
+
kdf,
|
1587
1612
|
sm2_exports as sm2,
|
1588
1613
|
sm32 as sm3,
|
1589
1614
|
sm4_exports as sm4
|
@@ -31,7 +31,7 @@ declare function arrayToUtf8(arr: Uint8Array): string;
|
|
31
31
|
/**
|
32
32
|
* 转成字节数组
|
33
33
|
*/
|
34
|
-
declare function hexToArray(hexStr: string): Uint8Array
|
34
|
+
declare function hexToArray(hexStr: string): Uint8Array<ArrayBuffer>;
|
35
35
|
/**
|
36
36
|
* 验证公钥是否为椭圆曲线上的点
|
37
37
|
*/
|
@@ -43,9 +43,9 @@ declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): bo
|
|
43
43
|
|
44
44
|
declare function initRNGPool(): Promise<void>;
|
45
45
|
|
46
|
-
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array
|
46
|
+
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array<ArrayBuffer>;
|
47
47
|
|
48
|
-
declare const EmptyArray: Uint8Array
|
48
|
+
declare const EmptyArray: Uint8Array<ArrayBuffer>;
|
49
49
|
/**
|
50
50
|
* 加密
|
51
51
|
*/
|
@@ -85,7 +85,7 @@ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, pu
|
|
85
85
|
hash?: boolean;
|
86
86
|
userId?: string;
|
87
87
|
}): boolean;
|
88
|
-
declare function getZ(publicKey: string, userId?: string): Uint8Array
|
88
|
+
declare function getZ(publicKey: string, userId?: string): Uint8Array<ArrayBufferLike>;
|
89
89
|
/**
|
90
90
|
* sm3杂凑算法
|
91
91
|
*/
|
@@ -139,6 +139,14 @@ declare namespace index$1 {
|
|
139
139
|
export { index$1_EmptyArray as EmptyArray, type index$1_KeyPair as KeyPair, type index$1_SignaturePoint as SignaturePoint, index$1_arrayToHex as arrayToHex, index$1_arrayToUtf8 as arrayToUtf8, index$1_calculateSharedKey as calculateSharedKey, index$1_comparePublicKeyHex as comparePublicKeyHex, index$1_compressPublicKeyHex as compressPublicKeyHex, index$1_doDecrypt as doDecrypt, index$1_doEncrypt as doEncrypt, index$1_doSignature as doSignature, index$1_doVerifySignature as doVerifySignature, index$1_generateKeyPairHex as generateKeyPairHex, index$1_getHash as getHash, index$1_getPoint as getPoint, index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey, index$1_getZ as getZ, index$1_hexToArray as hexToArray, index$1_initRNGPool as initRNGPool, index$1_leftPad as leftPad, index$1_precomputePublicKey as precomputePublicKey, index$1_utf8ToHex as utf8ToHex, index$1_verifyPublicKey as verifyPublicKey };
|
140
140
|
}
|
141
141
|
|
142
|
+
/**
|
143
|
+
* SM3 Key derivation function used in SM2 encryption and key exchange, specified in GM/T 0003-2012
|
144
|
+
* @param z Input data (string or Uint8Array)
|
145
|
+
* @param keylen Desired key length in bytes
|
146
|
+
* @returns Derived key as Uint8Array
|
147
|
+
*/
|
148
|
+
declare function kdf(z: string | Uint8Array, keylen: number): Uint8Array<ArrayBuffer>;
|
149
|
+
|
142
150
|
declare function sm3(input: string | Uint8Array, options?: {
|
143
151
|
key: Uint8Array | string;
|
144
152
|
mode?: 'hmac' | 'mac';
|
@@ -153,9 +161,9 @@ interface SM4Options {
|
|
153
161
|
outputTag?: boolean;
|
154
162
|
tag?: Uint8Array | string;
|
155
163
|
}
|
156
|
-
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array | {
|
164
|
+
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array<ArrayBufferLike> | {
|
157
165
|
output: Uint8Array;
|
158
|
-
tag?: Uint8Array
|
166
|
+
tag?: Uint8Array;
|
159
167
|
} | {
|
160
168
|
output: string;
|
161
169
|
tag: string | undefined;
|
@@ -194,4 +202,4 @@ declare namespace index {
|
|
194
202
|
export { type index_GCMResult as GCMResult, type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
|
195
203
|
}
|
196
204
|
|
197
|
-
export { index$1 as sm2, sm3, index as sm4 };
|
205
|
+
export { kdf, index$1 as sm2, sm3, index as sm4 };
|
@@ -444,6 +444,9 @@ var __publicField = function(obj, key, value) {
|
|
444
444
|
// src/index.ts
|
445
445
|
var src_exports = {};
|
446
446
|
__export(src_exports, {
|
447
|
+
kdf: function() {
|
448
|
+
return kdf;
|
449
|
+
},
|
447
450
|
sm2: function() {
|
448
451
|
return sm2_exports;
|
449
452
|
},
|
@@ -2496,12 +2499,36 @@ function bytesToHex2(bytes) {
|
|
2496
2499
|
}
|
2497
2500
|
return hex;
|
2498
2501
|
}
|
2499
|
-
|
2500
|
-
|
2501
|
-
|
2502
|
+
var te = typeof TextEncoder != "undefined" && /* @__PURE__ */ new TextEncoder();
|
2503
|
+
var slc = function(v, s, e) {
|
2504
|
+
if (s == null || s < 0) s = 0;
|
2505
|
+
if (e == null || e > v.length) e = v.length;
|
2506
|
+
return new Uint8Array(v.subarray(s, e));
|
2507
|
+
};
|
2508
|
+
function strToU8(str) {
|
2509
|
+
if (te) return te.encode(str);
|
2510
|
+
var l = str.length;
|
2511
|
+
var ar = new Uint8Array(str.length + (str.length >> 1));
|
2512
|
+
var ai = 0;
|
2513
|
+
var w = function(v) {
|
2514
|
+
ar[ai++] = v;
|
2515
|
+
};
|
2516
|
+
for(var i = 0; i < l; ++i){
|
2517
|
+
if (ai + 5 > ar.length) {
|
2518
|
+
var n = new Uint8Array(ai + 8 + (l - i << 1));
|
2519
|
+
n.set(ar);
|
2520
|
+
ar = n;
|
2521
|
+
}
|
2522
|
+
var c = str.charCodeAt(i);
|
2523
|
+
if (c < 128) w(c);
|
2524
|
+
else if (c < 2048) w(192 | c >> 6), w(128 | c & 63);
|
2525
|
+
else if (c > 55295 && c < 57344) c = 65536 + (c & 1023 << 10) | str.charCodeAt(++i) & 1023, w(240 | c >> 18), w(128 | c >> 12 & 63), w(128 | c >> 6 & 63), w(128 | c & 63);
|
2526
|
+
else w(224 | c >> 12), w(128 | c >> 6 & 63), w(128 | c & 63);
|
2527
|
+
}
|
2528
|
+
return slc(ar, 0, ai);
|
2502
2529
|
}
|
2503
2530
|
function toBytes(data) {
|
2504
|
-
if (typeof data === "string") data =
|
2531
|
+
if (typeof data === "string") data = strToU8(data);
|
2505
2532
|
if (!u8a2(data)) throw new Error("expected Uint8Array, got ".concat(typeof data === "undefined" ? "undefined" : _type_of(data)));
|
2506
2533
|
return data;
|
2507
2534
|
}
|
@@ -2915,7 +2942,7 @@ function compressPublicKeyHex(s) {
|
|
2915
2942
|
return prefix + xHex;
|
2916
2943
|
}
|
2917
2944
|
function utf8ToHex(input) {
|
2918
|
-
var bytes =
|
2945
|
+
var bytes = strToU8(input);
|
2919
2946
|
return bytesToHex(bytes);
|
2920
2947
|
}
|
2921
2948
|
function leftPad(input, num) {
|
@@ -2976,10 +3003,48 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
2976
3003
|
if (!point2) return false;
|
2977
3004
|
return point1.equals(point2);
|
2978
3005
|
}
|
2979
|
-
// src/
|
2980
|
-
|
2981
|
-
var
|
2982
|
-
|
3006
|
+
// src/sm3/index.ts
|
3007
|
+
function utf8ToArray(str) {
|
3008
|
+
var arr = [];
|
3009
|
+
for(var i = 0, len = str.length; i < len; i++){
|
3010
|
+
var point = str.codePointAt(i);
|
3011
|
+
if (point <= 127) {
|
3012
|
+
arr.push(point);
|
3013
|
+
} else if (point <= 2047) {
|
3014
|
+
arr.push(192 | point >>> 6);
|
3015
|
+
arr.push(128 | point & 63);
|
3016
|
+
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
3017
|
+
arr.push(224 | point >>> 12);
|
3018
|
+
arr.push(128 | point >>> 6 & 63);
|
3019
|
+
arr.push(128 | point & 63);
|
3020
|
+
} else if (point >= 65536 && point <= 1114111) {
|
3021
|
+
i++;
|
3022
|
+
arr.push(240 | point >>> 18 & 28);
|
3023
|
+
arr.push(128 | point >>> 12 & 63);
|
3024
|
+
arr.push(128 | point >>> 6 & 63);
|
3025
|
+
arr.push(128 | point & 63);
|
3026
|
+
} else {
|
3027
|
+
arr.push(point);
|
3028
|
+
throw new Error("input is not supported");
|
3029
|
+
}
|
3030
|
+
}
|
3031
|
+
return new Uint8Array(arr);
|
3032
|
+
}
|
3033
|
+
function sm32(input, options) {
|
3034
|
+
input = typeof input === "string" ? utf8ToArray(input) : input;
|
3035
|
+
if (options) {
|
3036
|
+
var mode = options.mode || "hmac";
|
3037
|
+
if (mode !== "hmac") throw new Error("invalid mode");
|
3038
|
+
var key = options.key;
|
3039
|
+
if (!key) throw new Error("invalid key");
|
3040
|
+
key = typeof key === "string" ? hexToArray(key) : key;
|
3041
|
+
return bytesToHex2(hmac(sm3, key, input));
|
3042
|
+
}
|
3043
|
+
return bytesToHex2(sm3(input));
|
3044
|
+
}
|
3045
|
+
// src/sm2/kdf.ts
|
3046
|
+
function kdf(z, keylen) {
|
3047
|
+
z = typeof z === "string" ? utf8ToArray(z) : z;
|
2983
3048
|
var msg = new Uint8Array(keylen);
|
2984
3049
|
var ct = 1;
|
2985
3050
|
var offset = 0;
|
@@ -3001,6 +3066,9 @@ function hkdf(z, keylen) {
|
|
3001
3066
|
}
|
3002
3067
|
return msg;
|
3003
3068
|
}
|
3069
|
+
// src/sm2/kx.ts
|
3070
|
+
var wPow2 = hexToNumber("80000000000000000000000000000000");
|
3071
|
+
var wPow2Sub1 = hexToNumber("7fffffffffffffffffffffffffffffff");
|
3004
3072
|
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength) {
|
3005
3073
|
var isRecipient = arguments.length > 5 && arguments[5] !== void 0 ? arguments[5] : false, idA = arguments.length > 6 && arguments[6] !== void 0 ? arguments[6] : "1234567812345678", idB = arguments.length > 7 && arguments[7] !== void 0 ? arguments[7] : "1234567812345678";
|
3006
3074
|
var RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
@@ -3025,10 +3093,16 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
|
|
3025
3093
|
var U = RB.multiply(x2_).add(PB).multiply(tA);
|
3026
3094
|
var xU = hexToArray(leftPad(numberToHexUnpadded(U.x), 64));
|
3027
3095
|
var yU = hexToArray(leftPad(numberToHexUnpadded(U.y), 64));
|
3028
|
-
var KA =
|
3096
|
+
var KA = kdf(concatBytes(xU, yU, ZA, ZB), sharedKeyLength);
|
3029
3097
|
return KA;
|
3030
3098
|
}
|
3031
3099
|
// src/sm2/index.ts
|
3100
|
+
function xorCipherStream(x2, y2, msg) {
|
3101
|
+
var stream = kdf(concatBytes(x2, y2), msg.length);
|
3102
|
+
for(var i = 0, len = msg.length; i < len; i++){
|
3103
|
+
msg[i] ^= stream[i] & 255;
|
3104
|
+
}
|
3105
|
+
}
|
3032
3106
|
var C1C2C3 = 0;
|
3033
3107
|
var EmptyArray = new Uint8Array();
|
3034
3108
|
function doEncrypt(msg, publicKey) {
|
@@ -3052,26 +3126,6 @@ function doEncrypt(msg, publicKey) {
|
|
3052
3126
|
}
|
3053
3127
|
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
3054
3128
|
}
|
3055
|
-
function xorCipherStream(x2, y2, msg) {
|
3056
|
-
var ct = 1;
|
3057
|
-
var offset = 0;
|
3058
|
-
var t = EmptyArray;
|
3059
|
-
var ctShift = new Uint8Array(4);
|
3060
|
-
var nextT = function() {
|
3061
|
-
ctShift[0] = ct >> 24 & 255;
|
3062
|
-
ctShift[1] = ct >> 16 & 255;
|
3063
|
-
ctShift[2] = ct >> 8 & 255;
|
3064
|
-
ctShift[3] = ct & 255;
|
3065
|
-
t = sm3(concatBytes(x2, y2, ctShift));
|
3066
|
-
ct++;
|
3067
|
-
offset = 0;
|
3068
|
-
};
|
3069
|
-
nextT();
|
3070
|
-
for(var i = 0, len = msg.length; i < len; i++){
|
3071
|
-
if (offset === t.length) nextT();
|
3072
|
-
msg[i] ^= t[offset++] & 255;
|
3073
|
-
}
|
3074
|
-
}
|
3075
3129
|
function doDecrypt(encryptData, privateKey) {
|
3076
3130
|
var cipherMode = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1, options = arguments.length > 3 ? arguments[3] : void 0;
|
3077
3131
|
var _ref = options || {}, _ref_output = _ref.output, output = _ref_output === void 0 ? "string" : _ref_output, _ref_asn1 = _ref.asn1, asn1 = _ref_asn1 === void 0 ? false : _ref_asn1;
|
@@ -3220,45 +3274,6 @@ function getPoint() {
|
|
3220
3274
|
x1: PA.x
|
3221
3275
|
});
|
3222
3276
|
}
|
3223
|
-
// src/sm3/index.ts
|
3224
|
-
function utf8ToArray(str) {
|
3225
|
-
var arr = [];
|
3226
|
-
for(var i = 0, len = str.length; i < len; i++){
|
3227
|
-
var point = str.codePointAt(i);
|
3228
|
-
if (point <= 127) {
|
3229
|
-
arr.push(point);
|
3230
|
-
} else if (point <= 2047) {
|
3231
|
-
arr.push(192 | point >>> 6);
|
3232
|
-
arr.push(128 | point & 63);
|
3233
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
3234
|
-
arr.push(224 | point >>> 12);
|
3235
|
-
arr.push(128 | point >>> 6 & 63);
|
3236
|
-
arr.push(128 | point & 63);
|
3237
|
-
} else if (point >= 65536 && point <= 1114111) {
|
3238
|
-
i++;
|
3239
|
-
arr.push(240 | point >>> 18 & 28);
|
3240
|
-
arr.push(128 | point >>> 12 & 63);
|
3241
|
-
arr.push(128 | point >>> 6 & 63);
|
3242
|
-
arr.push(128 | point & 63);
|
3243
|
-
} else {
|
3244
|
-
arr.push(point);
|
3245
|
-
throw new Error("input is not supported");
|
3246
|
-
}
|
3247
|
-
}
|
3248
|
-
return new Uint8Array(arr);
|
3249
|
-
}
|
3250
|
-
function sm32(input, options) {
|
3251
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
3252
|
-
if (options) {
|
3253
|
-
var mode = options.mode || "hmac";
|
3254
|
-
if (mode !== "hmac") throw new Error("invalid mode");
|
3255
|
-
var key = options.key;
|
3256
|
-
if (!key) throw new Error("invalid key");
|
3257
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
3258
|
-
return bytesToHex2(hmac(sm3, key, input));
|
3259
|
-
}
|
3260
|
-
return bytesToHex2(sm3(input));
|
3261
|
-
}
|
3262
3277
|
// src/sm4/index.ts
|
3263
3278
|
var sm4_exports = {};
|
3264
3279
|
__export(sm4_exports, {
|
@@ -3306,12 +3321,12 @@ var isLE2 = new Uint8Array(new Uint32Array([
|
|
3306
3321
|
287454020
|
3307
3322
|
]).buffer)[0] === 68;
|
3308
3323
|
if (!isLE2) throw new Error("Non little-endian hardware is not supported");
|
3309
|
-
function
|
3324
|
+
function utf8ToBytes2(str) {
|
3310
3325
|
if (typeof str !== "string") throw new Error("string expected");
|
3311
3326
|
return new Uint8Array(new TextEncoder().encode(str));
|
3312
3327
|
}
|
3313
3328
|
function toBytes2(data) {
|
3314
|
-
if (typeof data === "string") data =
|
3329
|
+
if (typeof data === "string") data = utf8ToBytes2(data);
|
3315
3330
|
else if (isBytes(data)) data = copyBytes(data);
|
3316
3331
|
else throw new Error("Uint8Array expected, got " + (typeof data === "undefined" ? "undefined" : _type_of(data)));
|
3317
3332
|
return data;
|
@@ -4227,6 +4242,7 @@ function decrypt(inArray, key) {
|
|
4227
4242
|
}
|
4228
4243
|
// Annotate the CommonJS export names for ESM import in node:
|
4229
4244
|
0 && (module.exports = {
|
4245
|
+
kdf: kdf,
|
4230
4246
|
sm2: sm2,
|
4231
4247
|
sm3: sm3,
|
4232
4248
|
sm4: sm4
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "sm-crypto-v2",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.12.0",
|
4
4
|
"description": "sm-crypto-v2",
|
5
5
|
"main": "dist/index.js",
|
6
6
|
"module": "dist/index.mjs",
|
@@ -58,7 +58,7 @@
|
|
58
58
|
"prettier": "^2.6.2",
|
59
59
|
"standard-version": "^9.5.0",
|
60
60
|
"tsup": "^8.0.1",
|
61
|
-
"typescript": "^
|
61
|
+
"typescript": "^5.8.3",
|
62
62
|
"vite": "^4.3.9",
|
63
63
|
"vitest": "^0.31.0"
|
64
64
|
}
|