sm-crypto-v2 1.8.0 → 1.9.1
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/dist/index.d.mts +175 -0
- package/dist/index.d.ts +18 -47
- package/dist/index.js +18 -1
- package/dist/index.mjs +14 -1
- package/miniprogram_dist/index.d.ts +175 -0
- package/miniprogram_dist/index.js +3784 -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,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.9.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.9.0...v1.9.1) (2024-07-09)
|
6
|
+
|
7
|
+
|
8
|
+
### Bug Fixes
|
9
|
+
|
10
|
+
* **asn1:** correct the implementation of the long version of L encoding in asn.1 ([55569c5](https://github.com/Cubelrti/sm-crypto-v2/commit/55569c55235c0a82525b142ea9a4f8a2669654ce))
|
11
|
+
|
12
|
+
## [1.9.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.7.0...v1.9.0) (2023-12-27)
|
13
|
+
|
14
|
+
|
15
|
+
### Features
|
16
|
+
|
17
|
+
* **sm2:** support asn1 der encoded encryption/decryption ([f08b9fd](https://github.com/Cubelrti/sm-crypto-v2/commit/f08b9fd5b64a2a257d41694c1858d7a6b07326ae))
|
18
|
+
* support miniprogram build ([bb4dddc](https://github.com/Cubelrti/sm-crypto-v2/commit/bb4dddcb4935299fe6db82912f393aa427961e40))
|
19
|
+
|
5
20
|
## [1.8.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.7.0...v1.8.0) (2023-12-15)
|
6
21
|
|
7
22
|
|
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();
|
@@ -160,7 +167,11 @@ var DERSequence = class extends ASN1Object {
|
|
160
167
|
function getLenOfL(str, start) {
|
161
168
|
if (+str[start + 2] < 8)
|
162
169
|
return 1;
|
163
|
-
|
170
|
+
const encoded = str.slice(start + 2, start + 6);
|
171
|
+
const headHex = encoded.slice(0, 2);
|
172
|
+
const head = parseInt(headHex, 16);
|
173
|
+
const nHexLength = (head - 128) * 2;
|
174
|
+
return nHexLength;
|
164
175
|
}
|
165
176
|
function getL(str, start) {
|
166
177
|
const len = getLenOfL(str, start);
|
@@ -315,6 +326,7 @@ function toBytes(data) {
|
|
315
326
|
return data;
|
316
327
|
}
|
317
328
|
var Hash = class {
|
329
|
+
// Safe version that clones internal state
|
318
330
|
clone() {
|
319
331
|
return this._cloneInto();
|
320
332
|
}
|
@@ -364,6 +376,7 @@ var SHA2 = class extends Hash {
|
|
364
376
|
this.buffer = new Uint8Array(blockLen);
|
365
377
|
this.view = createView(this.buffer);
|
366
378
|
}
|
379
|
+
// For partial updates less than block size
|
367
380
|
buffer;
|
368
381
|
view;
|
369
382
|
finished = false;
|
@@ -445,6 +458,8 @@ var SM3_M = new Uint32Array(64);
|
|
445
458
|
var T1 = 2043430169;
|
446
459
|
var T2 = 2055708042;
|
447
460
|
var SM3 = class extends SHA2 {
|
461
|
+
// We cannot use array here since array allows indexing by variable
|
462
|
+
// which means optimizer/compiler cannot use registers.
|
448
463
|
A = IV[0] | 0;
|
449
464
|
B = IV[1] | 0;
|
450
465
|
C = IV[2] | 0;
|
@@ -460,6 +475,7 @@ var SM3 = class extends SHA2 {
|
|
460
475
|
const { A, B, C, D, E, F, G, H } = this;
|
461
476
|
return [A, B, C, D, E, F, G, H];
|
462
477
|
}
|
478
|
+
// prettier-ignore
|
463
479
|
set(A, B, C, D, E, F, G, H) {
|
464
480
|
this.A = A | 0;
|
465
481
|
this.B = B | 0;
|
@@ -585,6 +601,7 @@ hmac.create = (hash, key) => new HMAC(hash, key);
|
|
585
601
|
var import_utils3 = require("@noble/curves/abstract/utils");
|
586
602
|
var sm2Fp = (0, import_modular.Field)(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
587
603
|
var sm2Curve = (0, import_weierstrass.weierstrass)({
|
604
|
+
// sm2: short weierstrass.
|
588
605
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
589
606
|
b: BigInt("18505919022281880113072981827955639221458448578012075254857346196103069175443"),
|
590
607
|
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();
|
@@ -132,7 +135,11 @@ var DERSequence = class extends ASN1Object {
|
|
132
135
|
function getLenOfL(str, start) {
|
133
136
|
if (+str[start + 2] < 8)
|
134
137
|
return 1;
|
135
|
-
|
138
|
+
const encoded = str.slice(start + 2, start + 6);
|
139
|
+
const headHex = encoded.slice(0, 2);
|
140
|
+
const head = parseInt(headHex, 16);
|
141
|
+
const nHexLength = (head - 128) * 2;
|
142
|
+
return nHexLength;
|
136
143
|
}
|
137
144
|
function getL(str, start) {
|
138
145
|
const len = getLenOfL(str, start);
|
@@ -287,6 +294,7 @@ function toBytes(data) {
|
|
287
294
|
return data;
|
288
295
|
}
|
289
296
|
var Hash = class {
|
297
|
+
// Safe version that clones internal state
|
290
298
|
clone() {
|
291
299
|
return this._cloneInto();
|
292
300
|
}
|
@@ -336,6 +344,7 @@ var SHA2 = class extends Hash {
|
|
336
344
|
this.buffer = new Uint8Array(blockLen);
|
337
345
|
this.view = createView(this.buffer);
|
338
346
|
}
|
347
|
+
// For partial updates less than block size
|
339
348
|
buffer;
|
340
349
|
view;
|
341
350
|
finished = false;
|
@@ -417,6 +426,8 @@ var SM3_M = new Uint32Array(64);
|
|
417
426
|
var T1 = 2043430169;
|
418
427
|
var T2 = 2055708042;
|
419
428
|
var SM3 = class extends SHA2 {
|
429
|
+
// We cannot use array here since array allows indexing by variable
|
430
|
+
// which means optimizer/compiler cannot use registers.
|
420
431
|
A = IV[0] | 0;
|
421
432
|
B = IV[1] | 0;
|
422
433
|
C = IV[2] | 0;
|
@@ -432,6 +443,7 @@ var SM3 = class extends SHA2 {
|
|
432
443
|
const { A, B, C, D, E, F, G, H } = this;
|
433
444
|
return [A, B, C, D, E, F, G, H];
|
434
445
|
}
|
446
|
+
// prettier-ignore
|
435
447
|
set(A, B, C, D, E, F, G, H) {
|
436
448
|
this.A = A | 0;
|
437
449
|
this.B = B | 0;
|
@@ -557,6 +569,7 @@ hmac.create = (hash, key) => new HMAC(hash, key);
|
|
557
569
|
import { concatBytes } from "@noble/curves/abstract/utils";
|
558
570
|
var sm2Fp = Field(BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991999"));
|
559
571
|
var sm2Curve = weierstrass({
|
572
|
+
// sm2: short weierstrass.
|
560
573
|
a: BigInt("115792089210356248756420345214020892766250353991924191454421193933289684991996"),
|
561
574
|
b: BigInt("18505919022281880113072981827955639221458448578012075254857346196103069175443"),
|
562
575
|
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 };
|