sm-crypto-v2 1.10.0 → 1.11.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 +15 -0
- package/README.md +20 -1
- package/dist/index.d.mts +16 -8
- package/dist/index.d.ts +16 -8
- package/dist/index.js +92 -101
- package/dist/index.mjs +91 -101
- package/miniprogram_dist/index.d.ts +16 -8
- package/miniprogram_dist/index.js +417 -92
- package/package.json +5 -6
- package/tsup.config.miniprogram.ts +1 -1
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,21 @@
|
|
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.11.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.1...v1.11.0) (2025-04-28)
|
6
|
+
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* **sm3, kx:** expose kdf in sm3, added test case for parity ([f0091ca](https://github.com/Cubelrti/sm-crypto-v2/commit/f0091ca4a58a3369a83f274023268dfe2bda2794))
|
11
|
+
|
12
|
+
### [1.10.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.10.0...v1.10.1) (2025-04-27)
|
13
|
+
|
14
|
+
|
15
|
+
### Bug Fixes
|
16
|
+
|
17
|
+
* miniprogram build issue ([5e8ece4](https://github.com/Cubelrti/sm-crypto-v2/commit/5e8ece41dab69fc449675f9d46dfd25163538a78))
|
18
|
+
* **sm2:** typing issue ([f368a72](https://github.com/Cubelrti/sm-crypto-v2/commit/f368a72cc354d0ec9601dda8cbd2590eb9e6c144))
|
19
|
+
|
5
20
|
## [1.10.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.9.3...v1.10.0) (2025-04-14)
|
6
21
|
|
7
22
|
|
package/README.md
CHANGED
@@ -19,6 +19,7 @@ For WebAssembly-supported platform, see [sm-crypto-wasm](https://github.com/Cube
|
|
19
19
|
- 🎲 自动选择最优的安全随机数实现,避免使用 `Math.random()` 和 `Date.now()` 进行模拟
|
20
20
|
- 📚 同时导出 ES Module 和 CommonJS 两种格式,可按需使用
|
21
21
|
- 🔑 提供 SM2 密钥交换 API
|
22
|
+
- 🔒 提供 SM4 GCM 模式加密解密能力
|
22
23
|
- 🎒 未压缩大小 34kb,压缩后 17kb
|
23
24
|
|
24
25
|
## 安装
|
@@ -149,13 +150,16 @@ let verifyResult4 = sm2.doVerifySignature(msg, sigValueHex4, precomputedPublicKe
|
|
149
150
|
## sm3
|
150
151
|
|
151
152
|
```js
|
152
|
-
import { sm3 } from 'sm-crypto-v2'
|
153
|
+
import { sm3, kdf } from 'sm-crypto-v2'
|
153
154
|
let hashData = sm3('abc') // 杂凑
|
154
155
|
|
155
156
|
// hmac
|
156
157
|
hashData = sm3('abc', {
|
157
158
|
key: 'daac25c1512fe50f79b0e4526b93f5c0e1460cef40b6dd44af13caec62e8c60e0d885f3c6d6fb51e530889e6fd4ac743a6d332e68a0f2a3923f42585dceb93e9', // 要求为 16 进制串或字节数组
|
158
159
|
})
|
160
|
+
|
161
|
+
// kdf,注意这是 GM/T 0003-2012 中的 SM3 KDF(密钥派生函数),不是 RFC5869 的 HKDF
|
162
|
+
kdfData = kdf('abc', 32 /* 输出长度 */)
|
159
163
|
```
|
160
164
|
|
161
165
|
## sm4
|
@@ -171,6 +175,14 @@ let encryptData = sm4.encrypt(msg, key) // 加密,默认输出 16 进制字符
|
|
171
175
|
let encryptData = sm4.encrypt(msg, key, {padding: 'none'}) // 加密,不使用 padding
|
172
176
|
let encryptData = sm4.encrypt(msg, key, {padding: 'none', output: 'array'}) // 加密,不使用 padding,输出为字节数组
|
173
177
|
let encryptData = sm4.encrypt(msg, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 加密,cbc 模式
|
178
|
+
let encryptData = sm4.encrypt(msg, key, {
|
179
|
+
mode: 'gcm', // gcm 模式,必填 iv, 可选 aad
|
180
|
+
iv,
|
181
|
+
associatedData,
|
182
|
+
output: 'string',
|
183
|
+
outputTag: true, // 输出 aead tag,如果为 false 则不输出
|
184
|
+
}) // 输出格式 { output: T; tag?: T; } T 为 string/Uint8Array
|
185
|
+
|
174
186
|
```
|
175
187
|
|
176
188
|
### 解密
|
@@ -184,6 +196,13 @@ let decryptData = sm4.decrypt(encryptData, key) // 解密,默认输出 utf8
|
|
184
196
|
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none'}) // 解密,不使用 padding
|
185
197
|
let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array'}) // 解密,不使用 padding,输出为字节数组
|
186
198
|
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
|
199
|
+
let decryptData = sm4.decrypt(encryptData, key, {
|
200
|
+
mode: 'gcm', // gcm 模式必填 iv tag, 可选 aad
|
201
|
+
iv,
|
202
|
+
associatedData,
|
203
|
+
tag: expectedAuthTag,
|
204
|
+
output: 'array'
|
205
|
+
}) // 输出格式 string/Uint8Array
|
187
206
|
```
|
188
207
|
|
189
208
|
### 密钥交换
|
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
|
*/
|
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
|
|
60
60
|
asn1?: boolean;
|
61
61
|
}): Uint8Array;
|
62
62
|
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
-
output
|
63
|
+
output?: 'string';
|
64
64
|
asn1?: boolean;
|
65
65
|
}): string;
|
66
66
|
interface SignaturePoint {
|
@@ -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
|
*/
|
@@ -60,7 +60,7 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
|
|
60
60
|
asn1?: boolean;
|
61
61
|
}): Uint8Array;
|
62
62
|
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
-
output
|
63
|
+
output?: 'string';
|
64
64
|
asn1?: boolean;
|
65
65
|
}): string;
|
66
66
|
interface SignaturePoint {
|
@@ -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
|
@@ -703,13 +704,56 @@ function comparePublicKeyHex(publicKey1, publicKey2) {
|
|
703
704
|
}
|
704
705
|
|
705
706
|
// src/sm2/index.ts
|
706
|
-
var
|
707
|
+
var utils5 = __toESM(require("@noble/curves/abstract/utils"));
|
707
708
|
|
708
|
-
// src/sm2/
|
709
|
+
// src/sm2/kdf.ts
|
709
710
|
var utils3 = __toESM(require("@noble/curves/abstract/utils"));
|
710
|
-
|
711
|
-
|
712
|
-
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;
|
713
757
|
let msg = new Uint8Array(keylen);
|
714
758
|
let ct = 1;
|
715
759
|
let offset = 0;
|
@@ -732,6 +776,11 @@ function hkdf(z, keylen) {
|
|
732
776
|
}
|
733
777
|
return msg;
|
734
778
|
}
|
779
|
+
|
780
|
+
// src/sm2/kx.ts
|
781
|
+
var utils4 = __toESM(require("@noble/curves/abstract/utils"));
|
782
|
+
var wPow2 = utils4.hexToNumber("80000000000000000000000000000000");
|
783
|
+
var wPow2Sub1 = utils4.hexToNumber("7fffffffffffffffffffffffffffffff");
|
735
784
|
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
736
785
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
737
786
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
@@ -741,35 +790,41 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
|
|
741
790
|
if (isRecipient) {
|
742
791
|
[ZA, ZB] = [ZB, ZA];
|
743
792
|
}
|
744
|
-
const rA =
|
745
|
-
const dA =
|
793
|
+
const rA = utils4.hexToNumber(ephemeralKeypairA.privateKey);
|
794
|
+
const dA = utils4.hexToNumber(keypairA.privateKey);
|
746
795
|
const x1 = RA.x;
|
747
796
|
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
748
797
|
const tA = field.add(dA, field.mulN(x1_, rA));
|
749
798
|
const x2 = RB.x;
|
750
799
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
751
800
|
const U = RB.multiply(x2_).add(PB).multiply(tA);
|
752
|
-
const xU = hexToArray(leftPad(
|
753
|
-
const yU = hexToArray(leftPad(
|
754
|
-
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);
|
755
804
|
return KA;
|
756
805
|
}
|
757
806
|
|
758
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
|
+
}
|
759
814
|
var C1C2C3 = 0;
|
760
815
|
var EmptyArray = new Uint8Array();
|
761
816
|
function doEncrypt(msg, publicKey, cipherMode = 1, options) {
|
762
817
|
const msgArr = typeof msg === "string" ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg);
|
763
818
|
const publicKeyPoint = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
|
764
819
|
const keypair = generateKeyPairHex();
|
765
|
-
const k =
|
820
|
+
const k = utils5.hexToNumber(keypair.privateKey);
|
766
821
|
let c1 = keypair.publicKey;
|
767
822
|
if (c1.length > 128)
|
768
823
|
c1 = c1.substring(c1.length - 128);
|
769
824
|
const p = publicKeyPoint.multiply(k);
|
770
|
-
const x2 = hexToArray(leftPad(
|
771
|
-
const y2 = hexToArray(leftPad(
|
772
|
-
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)));
|
773
828
|
xorCipherStream(x2, y2, msgArr);
|
774
829
|
const c2 = bytesToHex(msgArr);
|
775
830
|
if (options?.asn1) {
|
@@ -779,32 +834,9 @@ function doEncrypt(msg, publicKey, cipherMode = 1, options) {
|
|
779
834
|
}
|
780
835
|
return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2;
|
781
836
|
}
|
782
|
-
function
|
783
|
-
|
784
|
-
|
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
|
-
function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
804
|
-
output = "string",
|
805
|
-
asn1 = false
|
806
|
-
} = {}) {
|
807
|
-
const privateKeyInteger = utils4.hexToNumber(privateKey);
|
837
|
+
function doDecrypt(encryptData, privateKey, cipherMode = 1, options) {
|
838
|
+
const { output = "string", asn1 = false } = options || {};
|
839
|
+
const privateKeyInteger = utils5.hexToNumber(privateKey);
|
808
840
|
let c1;
|
809
841
|
let c2;
|
810
842
|
let c3;
|
@@ -827,10 +859,10 @@ function doDecrypt(encryptData, privateKey, cipherMode = 1, {
|
|
827
859
|
}
|
828
860
|
const msg = hexToArray(c2);
|
829
861
|
const p = c1.multiply(privateKeyInteger);
|
830
|
-
const x2 = hexToArray(leftPad(
|
831
|
-
const y2 = hexToArray(leftPad(
|
862
|
+
const x2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.x), 64));
|
863
|
+
const y2 = hexToArray(leftPad(utils5.numberToHexUnpadded(p.y), 64));
|
832
864
|
xorCipherStream(x2, y2, msg);
|
833
|
-
const checkC3 = arrayToHex(Array.from(sm3(
|
865
|
+
const checkC3 = arrayToHex(Array.from(sm3(utils5.concatBytes(x2, msg, y2))));
|
834
866
|
if (checkC3 === c3.toLowerCase()) {
|
835
867
|
return output === "array" ? msg : arrayToUtf8(msg);
|
836
868
|
} else {
|
@@ -850,8 +882,8 @@ function doSignature(msg, privateKey, options = {}) {
|
|
850
882
|
publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
|
851
883
|
hashHex = getHash(hashHex, publicKey, userId);
|
852
884
|
}
|
853
|
-
const dA =
|
854
|
-
const e =
|
885
|
+
const dA = utils5.hexToNumber(privateKey);
|
886
|
+
const e = utils5.hexToNumber(hashHex);
|
855
887
|
let k = null;
|
856
888
|
let r = null;
|
857
889
|
let s = null;
|
@@ -870,7 +902,7 @@ function doSignature(msg, privateKey, options = {}) {
|
|
870
902
|
} while (s === ZERO);
|
871
903
|
if (der)
|
872
904
|
return encodeDer(r, s);
|
873
|
-
return leftPad(
|
905
|
+
return leftPad(utils5.numberToHexUnpadded(r), 64) + leftPad(utils5.numberToHexUnpadded(s), 64);
|
874
906
|
}
|
875
907
|
function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
876
908
|
let hashHex;
|
@@ -892,11 +924,11 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
892
924
|
r = decodeDerObj.r;
|
893
925
|
s = decodeDerObj.s;
|
894
926
|
} else {
|
895
|
-
r =
|
896
|
-
s =
|
927
|
+
r = utils5.hexToNumber(signHex.substring(0, 64));
|
928
|
+
s = utils5.hexToNumber(signHex.substring(64));
|
897
929
|
}
|
898
930
|
const PA = typeof publicKey === "string" ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey;
|
899
|
-
const e =
|
931
|
+
const e = utils5.hexToNumber(hashHex);
|
900
932
|
const t = field.add(r, s);
|
901
933
|
if (t === ZERO)
|
902
934
|
return false;
|
@@ -906,10 +938,10 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
906
938
|
}
|
907
939
|
function getZ(publicKey, userId = "1234567812345678") {
|
908
940
|
userId = utf8ToHex(userId);
|
909
|
-
const a = leftPad(
|
910
|
-
const b = leftPad(
|
911
|
-
const gx = leftPad(
|
912
|
-
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);
|
913
945
|
let px;
|
914
946
|
let py;
|
915
947
|
if (publicKey.length === 128) {
|
@@ -917,17 +949,17 @@ function getZ(publicKey, userId = "1234567812345678") {
|
|
917
949
|
py = publicKey.substring(64, 128);
|
918
950
|
} else {
|
919
951
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
920
|
-
px = leftPad(
|
921
|
-
py = leftPad(
|
952
|
+
px = leftPad(utils5.numberToHexUnpadded(point.x), 64);
|
953
|
+
py = leftPad(utils5.numberToHexUnpadded(point.y), 64);
|
922
954
|
}
|
923
955
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
924
956
|
const entl = userId.length * 4;
|
925
|
-
const z = sm3(
|
957
|
+
const z = sm3(utils5.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
926
958
|
return z;
|
927
959
|
}
|
928
960
|
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
929
961
|
const z = getZ(publicKey, userId);
|
930
|
-
return bytesToHex(sm3(
|
962
|
+
return bytesToHex(sm3(utils5.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
931
963
|
}
|
932
964
|
function precomputePublicKey(publicKey, windowSize) {
|
933
965
|
const point = sm2Curve.ProjectivePoint.fromHex(publicKey);
|
@@ -935,13 +967,13 @@ function precomputePublicKey(publicKey, windowSize) {
|
|
935
967
|
}
|
936
968
|
function getPublicKeyFromPrivateKey(privateKey) {
|
937
969
|
const pubKey = sm2Curve.getPublicKey(privateKey, false);
|
938
|
-
const pubPad = leftPad(
|
970
|
+
const pubPad = leftPad(utils5.bytesToHex(pubKey), 64);
|
939
971
|
return pubPad;
|
940
972
|
}
|
941
973
|
function getPoint() {
|
942
974
|
const keypair = generateKeyPairHex();
|
943
975
|
const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey);
|
944
|
-
const k =
|
976
|
+
const k = utils5.hexToNumber(keypair.privateKey);
|
945
977
|
return {
|
946
978
|
...keypair,
|
947
979
|
k,
|
@@ -949,48 +981,6 @@ function getPoint() {
|
|
949
981
|
};
|
950
982
|
}
|
951
983
|
|
952
|
-
// src/sm3/index.ts
|
953
|
-
function utf8ToArray(str) {
|
954
|
-
const arr = [];
|
955
|
-
for (let i = 0, len = str.length; i < len; i++) {
|
956
|
-
const point = str.codePointAt(i);
|
957
|
-
if (point <= 127) {
|
958
|
-
arr.push(point);
|
959
|
-
} else if (point <= 2047) {
|
960
|
-
arr.push(192 | point >>> 6);
|
961
|
-
arr.push(128 | point & 63);
|
962
|
-
} else if (point <= 55295 || point >= 57344 && point <= 65535) {
|
963
|
-
arr.push(224 | point >>> 12);
|
964
|
-
arr.push(128 | point >>> 6 & 63);
|
965
|
-
arr.push(128 | point & 63);
|
966
|
-
} else if (point >= 65536 && point <= 1114111) {
|
967
|
-
i++;
|
968
|
-
arr.push(240 | point >>> 18 & 28);
|
969
|
-
arr.push(128 | point >>> 12 & 63);
|
970
|
-
arr.push(128 | point >>> 6 & 63);
|
971
|
-
arr.push(128 | point & 63);
|
972
|
-
} else {
|
973
|
-
arr.push(point);
|
974
|
-
throw new Error("input is not supported");
|
975
|
-
}
|
976
|
-
}
|
977
|
-
return new Uint8Array(arr);
|
978
|
-
}
|
979
|
-
function sm32(input, options) {
|
980
|
-
input = typeof input === "string" ? utf8ToArray(input) : input;
|
981
|
-
if (options) {
|
982
|
-
const mode = options.mode || "hmac";
|
983
|
-
if (mode !== "hmac")
|
984
|
-
throw new Error("invalid mode");
|
985
|
-
let key = options.key;
|
986
|
-
if (!key)
|
987
|
-
throw new Error("invalid key");
|
988
|
-
key = typeof key === "string" ? hexToArray(key) : key;
|
989
|
-
return bytesToHex(hmac(sm3, key, input));
|
990
|
-
}
|
991
|
-
return bytesToHex(sm3(input));
|
992
|
-
}
|
993
|
-
|
994
984
|
// src/sm4/index.ts
|
995
985
|
var sm4_exports = {};
|
996
986
|
__export(sm4_exports, {
|
@@ -1619,6 +1609,7 @@ function decrypt(inArray, key, options = {}) {
|
|
1619
1609
|
}
|
1620
1610
|
// Annotate the CommonJS export names for ESM import in node:
|
1621
1611
|
0 && (module.exports = {
|
1612
|
+
kdf,
|
1622
1613
|
sm2,
|
1623
1614
|
sm3,
|
1624
1615
|
sm4
|