sm-crypto-v2 1.8.0 → 1.9.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 +8 -0
- package/dist/index.d.mts +175 -0
- package/dist/index.d.ts +18 -47
- package/dist/index.js +13 -0
- package/dist/index.mjs +9 -0
- package/miniprogram_dist/index.d.ts +175 -0
- package/miniprogram_dist/index.js +3780 -0
- package/package.json +8 -4
- package/{tsup.config.ts → tsup.config.miniprogram.ts} +9 -4
- package/src/index.ts +0 -3
- package/src/sm2/asn1.ts +0 -210
- package/src/sm2/bn.ts +0 -4
- package/src/sm2/ec.ts +0 -24
- package/src/sm2/hmac.ts +0 -76
- package/src/sm2/index.ts +0 -319
- package/src/sm2/kx.ts +0 -83
- package/src/sm2/rng.ts +0 -77
- package/src/sm2/sm3.ts +0 -241
- package/src/sm2/utils.ts +0 -164
- package/src/sm3/index.ts +0 -72
- package/src/sm3/utils.ts +0 -117
- package/src/sm4/_slow.ts +0 -286
- package/src/sm4/index.ts +0 -322
- package/tsconfig.json +0 -21
- package/vitest.config.ts +0 -22
- package/webpack.config.js +0 -26
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
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.9.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.7.0...v1.9.0) (2023-12-27)
|
6
|
+
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* **sm2:** support asn1 der encoded encryption/decryption ([f08b9fd](https://github.com/Cubelrti/sm-crypto-v2/commit/f08b9fd5b64a2a257d41694c1858d7a6b07326ae))
|
11
|
+
* support miniprogram build ([bb4dddc](https://github.com/Cubelrti/sm-crypto-v2/commit/bb4dddcb4935299fe6db82912f393aa427961e40))
|
12
|
+
|
5
13
|
## [1.8.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.7.0...v1.8.0) (2023-12-15)
|
6
14
|
|
7
15
|
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
|
2
|
+
|
3
|
+
interface KeyPair {
|
4
|
+
privateKey: string;
|
5
|
+
publicKey: string;
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* 生成密钥对:publicKey = privateKey * G
|
9
|
+
*/
|
10
|
+
declare function generateKeyPairHex(str?: string): KeyPair;
|
11
|
+
/**
|
12
|
+
* 生成压缩公钥
|
13
|
+
*/
|
14
|
+
declare function compressPublicKeyHex(s: string): string;
|
15
|
+
/**
|
16
|
+
* utf8串转16进制串
|
17
|
+
*/
|
18
|
+
declare function utf8ToHex(input: string): string;
|
19
|
+
/**
|
20
|
+
* 补全16进制字符串
|
21
|
+
*/
|
22
|
+
declare function leftPad(input: string, num: number): string;
|
23
|
+
/**
|
24
|
+
* 转成16进制串
|
25
|
+
*/
|
26
|
+
declare function arrayToHex(arr: number[]): string;
|
27
|
+
/**
|
28
|
+
* 转成utf8串
|
29
|
+
*/
|
30
|
+
declare function arrayToUtf8(arr: Uint8Array): string;
|
31
|
+
/**
|
32
|
+
* 转成字节数组
|
33
|
+
*/
|
34
|
+
declare function hexToArray(hexStr: string): Uint8Array;
|
35
|
+
/**
|
36
|
+
* 验证公钥是否为椭圆曲线上的点
|
37
|
+
*/
|
38
|
+
declare function verifyPublicKey(publicKey: string): boolean;
|
39
|
+
/**
|
40
|
+
* 验证公钥是否等价,等价返回true
|
41
|
+
*/
|
42
|
+
declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): boolean;
|
43
|
+
|
44
|
+
declare function initRNGPool(): Promise<void>;
|
45
|
+
|
46
|
+
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array;
|
47
|
+
|
48
|
+
declare const EmptyArray: Uint8Array;
|
49
|
+
/**
|
50
|
+
* 加密
|
51
|
+
*/
|
52
|
+
declare function doEncrypt(msg: string | Uint8Array, publicKey: string | ProjPointType<bigint>, cipherMode?: number, options?: {
|
53
|
+
asn1?: boolean;
|
54
|
+
}): string;
|
55
|
+
/**
|
56
|
+
* 解密
|
57
|
+
*/
|
58
|
+
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
59
|
+
output: 'array';
|
60
|
+
asn1?: boolean;
|
61
|
+
}): Uint8Array;
|
62
|
+
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
+
output: 'string';
|
64
|
+
asn1?: boolean;
|
65
|
+
}): string;
|
66
|
+
interface SignaturePoint {
|
67
|
+
k: bigint;
|
68
|
+
x1: bigint;
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* 签名
|
72
|
+
*/
|
73
|
+
declare function doSignature(msg: Uint8Array | string, privateKey: string, options?: {
|
74
|
+
pointPool?: SignaturePoint[];
|
75
|
+
der?: boolean;
|
76
|
+
hash?: boolean;
|
77
|
+
publicKey?: string;
|
78
|
+
userId?: string;
|
79
|
+
}): string;
|
80
|
+
/**
|
81
|
+
* 验签
|
82
|
+
*/
|
83
|
+
declare function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string | ProjPointType<bigint>, options?: {
|
84
|
+
der?: boolean;
|
85
|
+
hash?: boolean;
|
86
|
+
userId?: string;
|
87
|
+
}): boolean;
|
88
|
+
declare function getZ(publicKey: string, userId?: string): Uint8Array;
|
89
|
+
/**
|
90
|
+
* sm3杂凑算法
|
91
|
+
*/
|
92
|
+
declare function getHash(hashHex: string | Uint8Array, publicKey: string, userId?: string): string;
|
93
|
+
/**
|
94
|
+
* 预计算公钥点,可用于提升加密性能
|
95
|
+
* @export
|
96
|
+
* @param {string} publicKey 公钥
|
97
|
+
* @param windowSize 计算窗口大小,默认为 8
|
98
|
+
* @returns {ProjPointType<bigint>} 预计算的点
|
99
|
+
*/
|
100
|
+
declare function precomputePublicKey(publicKey: string, windowSize?: number): ProjPointType<bigint>;
|
101
|
+
/**
|
102
|
+
* 计算公钥
|
103
|
+
*/
|
104
|
+
declare function getPublicKeyFromPrivateKey(privateKey: string): string;
|
105
|
+
/**
|
106
|
+
* 获取椭圆曲线点
|
107
|
+
*/
|
108
|
+
declare function getPoint(): {
|
109
|
+
k: bigint;
|
110
|
+
x1: bigint;
|
111
|
+
privateKey: string;
|
112
|
+
publicKey: string;
|
113
|
+
};
|
114
|
+
|
115
|
+
declare const index$1_EmptyArray: typeof EmptyArray;
|
116
|
+
type index$1_KeyPair = KeyPair;
|
117
|
+
type index$1_SignaturePoint = SignaturePoint;
|
118
|
+
declare const index$1_arrayToHex: typeof arrayToHex;
|
119
|
+
declare const index$1_arrayToUtf8: typeof arrayToUtf8;
|
120
|
+
declare const index$1_calculateSharedKey: typeof calculateSharedKey;
|
121
|
+
declare const index$1_comparePublicKeyHex: typeof comparePublicKeyHex;
|
122
|
+
declare const index$1_compressPublicKeyHex: typeof compressPublicKeyHex;
|
123
|
+
declare const index$1_doDecrypt: typeof doDecrypt;
|
124
|
+
declare const index$1_doEncrypt: typeof doEncrypt;
|
125
|
+
declare const index$1_doSignature: typeof doSignature;
|
126
|
+
declare const index$1_doVerifySignature: typeof doVerifySignature;
|
127
|
+
declare const index$1_generateKeyPairHex: typeof generateKeyPairHex;
|
128
|
+
declare const index$1_getHash: typeof getHash;
|
129
|
+
declare const index$1_getPoint: typeof getPoint;
|
130
|
+
declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
131
|
+
declare const index$1_getZ: typeof getZ;
|
132
|
+
declare const index$1_hexToArray: typeof hexToArray;
|
133
|
+
declare const index$1_initRNGPool: typeof initRNGPool;
|
134
|
+
declare const index$1_leftPad: typeof leftPad;
|
135
|
+
declare const index$1_precomputePublicKey: typeof precomputePublicKey;
|
136
|
+
declare const index$1_utf8ToHex: typeof utf8ToHex;
|
137
|
+
declare const index$1_verifyPublicKey: typeof verifyPublicKey;
|
138
|
+
declare namespace index$1 {
|
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
|
+
}
|
141
|
+
|
142
|
+
declare function sm3(input: string | Uint8Array, options?: {
|
143
|
+
key: Uint8Array | string;
|
144
|
+
mode?: 'hmac' | 'mac';
|
145
|
+
}): string;
|
146
|
+
|
147
|
+
interface SM4Options {
|
148
|
+
padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
|
149
|
+
mode?: 'cbc' | 'ecb';
|
150
|
+
iv?: Uint8Array | string;
|
151
|
+
output?: 'string' | 'array';
|
152
|
+
}
|
153
|
+
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
|
154
|
+
declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
155
|
+
output: 'array';
|
156
|
+
} | SM4Options): Uint8Array;
|
157
|
+
declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
158
|
+
output: 'string';
|
159
|
+
} | SM4Options): string;
|
160
|
+
declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
161
|
+
output: 'array';
|
162
|
+
} | SM4Options): Uint8Array;
|
163
|
+
declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
164
|
+
output: 'string';
|
165
|
+
} | SM4Options): string;
|
166
|
+
|
167
|
+
type index_SM4Options = SM4Options;
|
168
|
+
declare const index_decrypt: typeof decrypt;
|
169
|
+
declare const index_encrypt: typeof encrypt;
|
170
|
+
declare const index_sm4: typeof sm4;
|
171
|
+
declare namespace index {
|
172
|
+
export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
|
173
|
+
}
|
174
|
+
|
175
|
+
export { index$1 as sm2, sm3, index as sm4 };
|
package/dist/index.d.ts
CHANGED
@@ -113,54 +113,30 @@ declare function getPoint(): {
|
|
113
113
|
};
|
114
114
|
|
115
115
|
declare const index$1_EmptyArray: typeof EmptyArray;
|
116
|
-
|
117
|
-
declare const index$1_doDecrypt: typeof doDecrypt;
|
116
|
+
type index$1_KeyPair = KeyPair;
|
118
117
|
type index$1_SignaturePoint = SignaturePoint;
|
118
|
+
declare const index$1_arrayToHex: typeof arrayToHex;
|
119
|
+
declare const index$1_arrayToUtf8: typeof arrayToUtf8;
|
120
|
+
declare const index$1_calculateSharedKey: typeof calculateSharedKey;
|
121
|
+
declare const index$1_comparePublicKeyHex: typeof comparePublicKeyHex;
|
122
|
+
declare const index$1_compressPublicKeyHex: typeof compressPublicKeyHex;
|
123
|
+
declare const index$1_doDecrypt: typeof doDecrypt;
|
124
|
+
declare const index$1_doEncrypt: typeof doEncrypt;
|
119
125
|
declare const index$1_doSignature: typeof doSignature;
|
120
126
|
declare const index$1_doVerifySignature: typeof doVerifySignature;
|
121
|
-
declare const index$
|
127
|
+
declare const index$1_generateKeyPairHex: typeof generateKeyPairHex;
|
122
128
|
declare const index$1_getHash: typeof getHash;
|
123
|
-
declare const index$1_precomputePublicKey: typeof precomputePublicKey;
|
124
|
-
declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
125
129
|
declare const index$1_getPoint: typeof getPoint;
|
130
|
+
declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
131
|
+
declare const index$1_getZ: typeof getZ;
|
132
|
+
declare const index$1_hexToArray: typeof hexToArray;
|
126
133
|
declare const index$1_initRNGPool: typeof initRNGPool;
|
127
|
-
declare const index$1_calculateSharedKey: typeof calculateSharedKey;
|
128
|
-
type index$1_KeyPair = KeyPair;
|
129
|
-
declare const index$1_generateKeyPairHex: typeof generateKeyPairHex;
|
130
|
-
declare const index$1_compressPublicKeyHex: typeof compressPublicKeyHex;
|
131
|
-
declare const index$1_utf8ToHex: typeof utf8ToHex;
|
132
134
|
declare const index$1_leftPad: typeof leftPad;
|
133
|
-
declare const index$
|
134
|
-
declare const index$
|
135
|
-
declare const index$1_hexToArray: typeof hexToArray;
|
135
|
+
declare const index$1_precomputePublicKey: typeof precomputePublicKey;
|
136
|
+
declare const index$1_utf8ToHex: typeof utf8ToHex;
|
136
137
|
declare const index$1_verifyPublicKey: typeof verifyPublicKey;
|
137
|
-
declare const index$1_comparePublicKeyHex: typeof comparePublicKeyHex;
|
138
138
|
declare namespace index$1 {
|
139
|
-
export {
|
140
|
-
index$1_EmptyArray as EmptyArray,
|
141
|
-
index$1_doEncrypt as doEncrypt,
|
142
|
-
index$1_doDecrypt as doDecrypt,
|
143
|
-
index$1_SignaturePoint as SignaturePoint,
|
144
|
-
index$1_doSignature as doSignature,
|
145
|
-
index$1_doVerifySignature as doVerifySignature,
|
146
|
-
index$1_getZ as getZ,
|
147
|
-
index$1_getHash as getHash,
|
148
|
-
index$1_precomputePublicKey as precomputePublicKey,
|
149
|
-
index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey,
|
150
|
-
index$1_getPoint as getPoint,
|
151
|
-
index$1_initRNGPool as initRNGPool,
|
152
|
-
index$1_calculateSharedKey as calculateSharedKey,
|
153
|
-
index$1_KeyPair as KeyPair,
|
154
|
-
index$1_generateKeyPairHex as generateKeyPairHex,
|
155
|
-
index$1_compressPublicKeyHex as compressPublicKeyHex,
|
156
|
-
index$1_utf8ToHex as utf8ToHex,
|
157
|
-
index$1_leftPad as leftPad,
|
158
|
-
index$1_arrayToHex as arrayToHex,
|
159
|
-
index$1_arrayToUtf8 as arrayToUtf8,
|
160
|
-
index$1_hexToArray as hexToArray,
|
161
|
-
index$1_verifyPublicKey as verifyPublicKey,
|
162
|
-
index$1_comparePublicKeyHex as comparePublicKeyHex,
|
163
|
-
};
|
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 };
|
164
140
|
}
|
165
141
|
|
166
142
|
declare function sm3(input: string | Uint8Array, options?: {
|
@@ -189,16 +165,11 @@ declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string,
|
|
189
165
|
} | SM4Options): string;
|
190
166
|
|
191
167
|
type index_SM4Options = SM4Options;
|
192
|
-
declare const index_sm4: typeof sm4;
|
193
|
-
declare const index_encrypt: typeof encrypt;
|
194
168
|
declare const index_decrypt: typeof decrypt;
|
169
|
+
declare const index_encrypt: typeof encrypt;
|
170
|
+
declare const index_sm4: typeof sm4;
|
195
171
|
declare namespace index {
|
196
|
-
export {
|
197
|
-
index_SM4Options as SM4Options,
|
198
|
-
index_sm4 as sm4,
|
199
|
-
index_encrypt as encrypt,
|
200
|
-
index_decrypt as decrypt,
|
201
|
-
};
|
172
|
+
export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
|
202
173
|
}
|
203
174
|
|
204
175
|
export { index$1 as sm2, sm3, index as sm4 };
|
package/dist/index.js
CHANGED
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
18
18
|
return to;
|
19
19
|
};
|
20
20
|
var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
21
25
|
isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
|
22
26
|
mod2
|
23
27
|
));
|
@@ -98,6 +102,9 @@ var ASN1Object = class {
|
|
98
102
|
this.l = l;
|
99
103
|
this.v = v;
|
100
104
|
}
|
105
|
+
/**
|
106
|
+
* 获取 der 编码比特流16进制串
|
107
|
+
*/
|
101
108
|
getEncodedHex() {
|
102
109
|
if (!this.tlv) {
|
103
110
|
this.v = this.getValue();
|
@@ -315,6 +322,7 @@ function toBytes(data) {
|
|
315
322
|
return data;
|
316
323
|
}
|
317
324
|
var Hash = class {
|
325
|
+
// Safe version that clones internal state
|
318
326
|
clone() {
|
319
327
|
return this._cloneInto();
|
320
328
|
}
|
@@ -364,6 +372,7 @@ var SHA2 = class extends Hash {
|
|
364
372
|
this.buffer = new Uint8Array(blockLen);
|
365
373
|
this.view = createView(this.buffer);
|
366
374
|
}
|
375
|
+
// For partial updates less than block size
|
367
376
|
buffer;
|
368
377
|
view;
|
369
378
|
finished = false;
|
@@ -445,6 +454,8 @@ var SM3_M = new Uint32Array(64);
|
|
445
454
|
var T1 = 2043430169;
|
446
455
|
var T2 = 2055708042;
|
447
456
|
var SM3 = class extends SHA2 {
|
457
|
+
// We cannot use array here since array allows indexing by variable
|
458
|
+
// which means optimizer/compiler cannot use registers.
|
448
459
|
A = IV[0] | 0;
|
449
460
|
B = IV[1] | 0;
|
450
461
|
C = IV[2] | 0;
|
@@ -460,6 +471,7 @@ var SM3 = class extends SHA2 {
|
|
460
471
|
const { A, B, C, D, E, F, G, H } = this;
|
461
472
|
return [A, B, C, D, E, F, G, H];
|
462
473
|
}
|
474
|
+
// prettier-ignore
|
463
475
|
set(A, B, C, D, E, F, G, H) {
|
464
476
|
this.A = A | 0;
|
465
477
|
this.B = B | 0;
|
@@ -585,6 +597,7 @@ hmac.create = (hash, key) => new HMAC(hash, key);
|
|
585
597
|
var import_utils3 = require("@noble/curves/abstract/utils");
|
586
598
|
var sm2Fp = (0, import_modular.Field)(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
587
599
|
var sm2Curve = (0, import_weierstrass.weierstrass)({
|
600
|
+
// sm2: short weierstrass.
|
588
601
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
589
602
|
b: BigInt("18505919022281880113072981827955639221458448578012075254857346196103069175443"),
|
590
603
|
Fp: sm2Fp,
|
package/dist/index.mjs
CHANGED
@@ -70,6 +70,9 @@ var ASN1Object = class {
|
|
70
70
|
this.l = l;
|
71
71
|
this.v = v;
|
72
72
|
}
|
73
|
+
/**
|
74
|
+
* 获取 der 编码比特流16进制串
|
75
|
+
*/
|
73
76
|
getEncodedHex() {
|
74
77
|
if (!this.tlv) {
|
75
78
|
this.v = this.getValue();
|
@@ -287,6 +290,7 @@ function toBytes(data) {
|
|
287
290
|
return data;
|
288
291
|
}
|
289
292
|
var Hash = class {
|
293
|
+
// Safe version that clones internal state
|
290
294
|
clone() {
|
291
295
|
return this._cloneInto();
|
292
296
|
}
|
@@ -336,6 +340,7 @@ var SHA2 = class extends Hash {
|
|
336
340
|
this.buffer = new Uint8Array(blockLen);
|
337
341
|
this.view = createView(this.buffer);
|
338
342
|
}
|
343
|
+
// For partial updates less than block size
|
339
344
|
buffer;
|
340
345
|
view;
|
341
346
|
finished = false;
|
@@ -417,6 +422,8 @@ var SM3_M = new Uint32Array(64);
|
|
417
422
|
var T1 = 2043430169;
|
418
423
|
var T2 = 2055708042;
|
419
424
|
var SM3 = class extends SHA2 {
|
425
|
+
// We cannot use array here since array allows indexing by variable
|
426
|
+
// which means optimizer/compiler cannot use registers.
|
420
427
|
A = IV[0] | 0;
|
421
428
|
B = IV[1] | 0;
|
422
429
|
C = IV[2] | 0;
|
@@ -432,6 +439,7 @@ var SM3 = class extends SHA2 {
|
|
432
439
|
const { A, B, C, D, E, F, G, H } = this;
|
433
440
|
return [A, B, C, D, E, F, G, H];
|
434
441
|
}
|
442
|
+
// prettier-ignore
|
435
443
|
set(A, B, C, D, E, F, G, H) {
|
436
444
|
this.A = A | 0;
|
437
445
|
this.B = B | 0;
|
@@ -557,6 +565,7 @@ hmac.create = (hash, key) => new HMAC(hash, key);
|
|
557
565
|
import { concatBytes } from "@noble/curves/abstract/utils";
|
558
566
|
var sm2Fp = Field(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
559
567
|
var sm2Curve = weierstrass({
|
568
|
+
// sm2: short weierstrass.
|
560
569
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
561
570
|
b: BigInt("18505919022281880113072981827955639221458448578012075254857346196103069175443"),
|
562
571
|
Fp: sm2Fp,
|
@@ -0,0 +1,175 @@
|
|
1
|
+
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
|
2
|
+
|
3
|
+
interface KeyPair {
|
4
|
+
privateKey: string;
|
5
|
+
publicKey: string;
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* 生成密钥对:publicKey = privateKey * G
|
9
|
+
*/
|
10
|
+
declare function generateKeyPairHex(str?: string): KeyPair;
|
11
|
+
/**
|
12
|
+
* 生成压缩公钥
|
13
|
+
*/
|
14
|
+
declare function compressPublicKeyHex(s: string): string;
|
15
|
+
/**
|
16
|
+
* utf8串转16进制串
|
17
|
+
*/
|
18
|
+
declare function utf8ToHex(input: string): string;
|
19
|
+
/**
|
20
|
+
* 补全16进制字符串
|
21
|
+
*/
|
22
|
+
declare function leftPad(input: string, num: number): string;
|
23
|
+
/**
|
24
|
+
* 转成16进制串
|
25
|
+
*/
|
26
|
+
declare function arrayToHex(arr: number[]): string;
|
27
|
+
/**
|
28
|
+
* 转成utf8串
|
29
|
+
*/
|
30
|
+
declare function arrayToUtf8(arr: Uint8Array): string;
|
31
|
+
/**
|
32
|
+
* 转成字节数组
|
33
|
+
*/
|
34
|
+
declare function hexToArray(hexStr: string): Uint8Array;
|
35
|
+
/**
|
36
|
+
* 验证公钥是否为椭圆曲线上的点
|
37
|
+
*/
|
38
|
+
declare function verifyPublicKey(publicKey: string): boolean;
|
39
|
+
/**
|
40
|
+
* 验证公钥是否等价,等价返回true
|
41
|
+
*/
|
42
|
+
declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): boolean;
|
43
|
+
|
44
|
+
declare function initRNGPool(): Promise<void>;
|
45
|
+
|
46
|
+
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array;
|
47
|
+
|
48
|
+
declare const EmptyArray: Uint8Array;
|
49
|
+
/**
|
50
|
+
* 加密
|
51
|
+
*/
|
52
|
+
declare function doEncrypt(msg: string | Uint8Array, publicKey: string | ProjPointType<bigint>, cipherMode?: number, options?: {
|
53
|
+
asn1?: boolean;
|
54
|
+
}): string;
|
55
|
+
/**
|
56
|
+
* 解密
|
57
|
+
*/
|
58
|
+
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
59
|
+
output: 'array';
|
60
|
+
asn1?: boolean;
|
61
|
+
}): Uint8Array;
|
62
|
+
declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
|
63
|
+
output: 'string';
|
64
|
+
asn1?: boolean;
|
65
|
+
}): string;
|
66
|
+
interface SignaturePoint {
|
67
|
+
k: bigint;
|
68
|
+
x1: bigint;
|
69
|
+
}
|
70
|
+
/**
|
71
|
+
* 签名
|
72
|
+
*/
|
73
|
+
declare function doSignature(msg: Uint8Array | string, privateKey: string, options?: {
|
74
|
+
pointPool?: SignaturePoint[];
|
75
|
+
der?: boolean;
|
76
|
+
hash?: boolean;
|
77
|
+
publicKey?: string;
|
78
|
+
userId?: string;
|
79
|
+
}): string;
|
80
|
+
/**
|
81
|
+
* 验签
|
82
|
+
*/
|
83
|
+
declare function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string | ProjPointType<bigint>, options?: {
|
84
|
+
der?: boolean;
|
85
|
+
hash?: boolean;
|
86
|
+
userId?: string;
|
87
|
+
}): boolean;
|
88
|
+
declare function getZ(publicKey: string, userId?: string): Uint8Array;
|
89
|
+
/**
|
90
|
+
* sm3杂凑算法
|
91
|
+
*/
|
92
|
+
declare function getHash(hashHex: string | Uint8Array, publicKey: string, userId?: string): string;
|
93
|
+
/**
|
94
|
+
* 预计算公钥点,可用于提升加密性能
|
95
|
+
* @export
|
96
|
+
* @param {string} publicKey 公钥
|
97
|
+
* @param windowSize 计算窗口大小,默认为 8
|
98
|
+
* @returns {ProjPointType<bigint>} 预计算的点
|
99
|
+
*/
|
100
|
+
declare function precomputePublicKey(publicKey: string, windowSize?: number): ProjPointType<bigint>;
|
101
|
+
/**
|
102
|
+
* 计算公钥
|
103
|
+
*/
|
104
|
+
declare function getPublicKeyFromPrivateKey(privateKey: string): string;
|
105
|
+
/**
|
106
|
+
* 获取椭圆曲线点
|
107
|
+
*/
|
108
|
+
declare function getPoint(): {
|
109
|
+
k: bigint;
|
110
|
+
x1: bigint;
|
111
|
+
privateKey: string;
|
112
|
+
publicKey: string;
|
113
|
+
};
|
114
|
+
|
115
|
+
declare const index$1_EmptyArray: typeof EmptyArray;
|
116
|
+
type index$1_KeyPair = KeyPair;
|
117
|
+
type index$1_SignaturePoint = SignaturePoint;
|
118
|
+
declare const index$1_arrayToHex: typeof arrayToHex;
|
119
|
+
declare const index$1_arrayToUtf8: typeof arrayToUtf8;
|
120
|
+
declare const index$1_calculateSharedKey: typeof calculateSharedKey;
|
121
|
+
declare const index$1_comparePublicKeyHex: typeof comparePublicKeyHex;
|
122
|
+
declare const index$1_compressPublicKeyHex: typeof compressPublicKeyHex;
|
123
|
+
declare const index$1_doDecrypt: typeof doDecrypt;
|
124
|
+
declare const index$1_doEncrypt: typeof doEncrypt;
|
125
|
+
declare const index$1_doSignature: typeof doSignature;
|
126
|
+
declare const index$1_doVerifySignature: typeof doVerifySignature;
|
127
|
+
declare const index$1_generateKeyPairHex: typeof generateKeyPairHex;
|
128
|
+
declare const index$1_getHash: typeof getHash;
|
129
|
+
declare const index$1_getPoint: typeof getPoint;
|
130
|
+
declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
131
|
+
declare const index$1_getZ: typeof getZ;
|
132
|
+
declare const index$1_hexToArray: typeof hexToArray;
|
133
|
+
declare const index$1_initRNGPool: typeof initRNGPool;
|
134
|
+
declare const index$1_leftPad: typeof leftPad;
|
135
|
+
declare const index$1_precomputePublicKey: typeof precomputePublicKey;
|
136
|
+
declare const index$1_utf8ToHex: typeof utf8ToHex;
|
137
|
+
declare const index$1_verifyPublicKey: typeof verifyPublicKey;
|
138
|
+
declare namespace index$1 {
|
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
|
+
}
|
141
|
+
|
142
|
+
declare function sm3(input: string | Uint8Array, options?: {
|
143
|
+
key: Uint8Array | string;
|
144
|
+
mode?: 'hmac' | 'mac';
|
145
|
+
}): string;
|
146
|
+
|
147
|
+
interface SM4Options {
|
148
|
+
padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
|
149
|
+
mode?: 'cbc' | 'ecb';
|
150
|
+
iv?: Uint8Array | string;
|
151
|
+
output?: 'string' | 'array';
|
152
|
+
}
|
153
|
+
declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
|
154
|
+
declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
155
|
+
output: 'array';
|
156
|
+
} | SM4Options): Uint8Array;
|
157
|
+
declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
158
|
+
output: 'string';
|
159
|
+
} | SM4Options): string;
|
160
|
+
declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
161
|
+
output: 'array';
|
162
|
+
} | SM4Options): Uint8Array;
|
163
|
+
declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
|
164
|
+
output: 'string';
|
165
|
+
} | SM4Options): string;
|
166
|
+
|
167
|
+
type index_SM4Options = SM4Options;
|
168
|
+
declare const index_decrypt: typeof decrypt;
|
169
|
+
declare const index_encrypt: typeof encrypt;
|
170
|
+
declare const index_sm4: typeof sm4;
|
171
|
+
declare namespace index {
|
172
|
+
export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
|
173
|
+
}
|
174
|
+
|
175
|
+
export { index$1 as sm2, sm3, index as sm4 };
|