sm-crypto-v2 0.3.13 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +8 -0
- package/dist/index.d.ts +5 -224
- package/dist/index.js +304 -435
- package/dist/index.mjs +297 -434
- package/package.json +5 -5
- package/pnpm-lock.yaml +14 -7
- package/src/sm2/asn1.ts +15 -12
- package/src/sm2/bn.ts +4 -0
- package/src/sm2/ec.ts +79 -317
- package/src/sm2/index.ts +83 -54
- package/src/sm2/utils.ts +29 -57
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,29 @@
|
|
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.2.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.1.0...v1.2.0) (2023-06-07)
|
6
|
+
|
7
|
+
|
8
|
+
### Features
|
9
|
+
|
10
|
+
* **sm2:** use secure prng ([b8ba3bf](https://github.com/Cubelrti/sm-crypto-v2/commit/b8ba3bfc0f6948c60db65fd2efe284d0c46855e0))
|
11
|
+
|
12
|
+
## [1.1.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v0.3.15...v1.1.0) (2023-06-07)
|
13
|
+
|
14
|
+
### [0.3.15](https://github.com/Cubelrti/sm-crypto-v2/compare/v0.3.13...v0.3.15) (2023-06-07)
|
15
|
+
|
16
|
+
|
17
|
+
### Features
|
18
|
+
|
19
|
+
* **ec:** add unit tests ([e513d51](https://github.com/Cubelrti/sm-crypto-v2/commit/e513d519fa82491bbb6c823cf862a7840bbc379e))
|
20
|
+
|
21
|
+
### [0.3.14](https://github.com/Cubelrti/sm-crypto-v2/compare/v0.3.13...v0.3.14) (2023-06-07)
|
22
|
+
|
23
|
+
|
24
|
+
### Features
|
25
|
+
|
26
|
+
* **ec:** add unit tests ([e513d51](https://github.com/Cubelrti/sm-crypto-v2/commit/e513d519fa82491bbb6c823cf862a7840bbc379e))
|
27
|
+
|
5
28
|
### 0.3.13 (2023-06-06)
|
6
29
|
|
7
30
|
|
package/README.md
CHANGED
@@ -6,6 +6,14 @@
|
|
6
6
|
|
7
7
|
国密算法 sm2、sm3 和 sm4 的 TypeScript 实现。参数支持 TypedArray,导出 esm/cjs。
|
8
8
|
|
9
|
+
## 特性
|
10
|
+
|
11
|
+
- SM2 底层改用 `noble-curves`,性能提升接近4倍,[noble-curves 文档](https://github.com/paulmillr/noble-curves)
|
12
|
+
- 完整的类型支持
|
13
|
+
- 移除原有 `jsbn` 依赖,改用原生 BigInt 支持
|
14
|
+
- 通过所有之前的单元测试,包括 SM2、SM3 和 SM4
|
15
|
+
- 自动使用最优的安全随机数实现,不使用 `random` 和 `Date.now` 模拟
|
16
|
+
|
9
17
|
## 安装
|
10
18
|
|
11
19
|
```bash
|
package/dist/index.d.ts
CHANGED
@@ -1,222 +1,7 @@
|
|
1
|
-
import { BigInteger, RandomGenerator } from 'jsbn';
|
2
|
-
|
3
|
-
/**
|
4
|
-
* 椭圆曲线域元素
|
5
|
-
*/
|
6
|
-
declare class ECFieldElementFp {
|
7
|
-
q: BigInteger;
|
8
|
-
x: BigInteger;
|
9
|
-
constructor(q: BigInteger, x: BigInteger);
|
10
|
-
/**
|
11
|
-
* 判断相等
|
12
|
-
*/
|
13
|
-
equals(other: ECFieldElementFp): boolean;
|
14
|
-
/**
|
15
|
-
* 返回具体数值
|
16
|
-
*/
|
17
|
-
toBigInteger(): BigInteger;
|
18
|
-
/**
|
19
|
-
* 取反
|
20
|
-
*/
|
21
|
-
negate(): ECFieldElementFp;
|
22
|
-
/**
|
23
|
-
* 相加
|
24
|
-
*/
|
25
|
-
add(b: any): ECFieldElementFp;
|
26
|
-
/**
|
27
|
-
* 相减
|
28
|
-
*/
|
29
|
-
subtract(b: any): ECFieldElementFp;
|
30
|
-
/**
|
31
|
-
* 相乘
|
32
|
-
*/
|
33
|
-
multiply(b: any): ECFieldElementFp;
|
34
|
-
/**
|
35
|
-
* 相除
|
36
|
-
*/
|
37
|
-
divide(b: any): ECFieldElementFp;
|
38
|
-
/**
|
39
|
-
* 平方
|
40
|
-
*/
|
41
|
-
square(): ECFieldElementFp;
|
42
|
-
}
|
43
|
-
declare class ECPointFp {
|
44
|
-
curve: ECCurveFp;
|
45
|
-
x: ECFieldElementFp | null;
|
46
|
-
y: ECFieldElementFp | null;
|
47
|
-
zinv: BigInteger | null;
|
48
|
-
z: BigInteger;
|
49
|
-
constructor(curve: ECCurveFp, x: ECFieldElementFp | null, y: ECFieldElementFp | null, z?: BigInteger);
|
50
|
-
getX(): ECFieldElementFp;
|
51
|
-
getY(): ECFieldElementFp;
|
52
|
-
/**
|
53
|
-
* 判断相等
|
54
|
-
*/
|
55
|
-
equals(other: ECPointFp): boolean;
|
56
|
-
/**
|
57
|
-
* 是否是无穷远点
|
58
|
-
*/
|
59
|
-
isInfinity(): boolean;
|
60
|
-
/**
|
61
|
-
* 取反,x 轴对称点
|
62
|
-
*/
|
63
|
-
negate(): ECPointFp;
|
64
|
-
/**
|
65
|
-
* 相加
|
66
|
-
*
|
67
|
-
* 标准射影坐标系:
|
68
|
-
*
|
69
|
-
* λ1 = x1 * z2
|
70
|
-
* λ2 = x2 * z1
|
71
|
-
* λ3 = λ1 − λ2
|
72
|
-
* λ4 = y1 * z2
|
73
|
-
* λ5 = y2 * z1
|
74
|
-
* λ6 = λ4 − λ5
|
75
|
-
* λ7 = λ1 + λ2
|
76
|
-
* λ8 = z1 * z2
|
77
|
-
* λ9 = λ3^2
|
78
|
-
* λ10 = λ3 * λ9
|
79
|
-
* λ11 = λ8 * λ6^2 − λ7 * λ9
|
80
|
-
* x3 = λ3 * λ11
|
81
|
-
* y3 = λ6 * (λ9 * λ1 − λ11) − λ4 * λ10
|
82
|
-
* z3 = λ10 * λ8
|
83
|
-
*/
|
84
|
-
add(b: ECPointFp): ECPointFp;
|
85
|
-
/**
|
86
|
-
* 自加
|
87
|
-
*
|
88
|
-
* 标准射影坐标系:
|
89
|
-
*
|
90
|
-
* λ1 = 3 * x1^2 + a * z1^2
|
91
|
-
* λ2 = 2 * y1 * z1
|
92
|
-
* λ3 = y1^2
|
93
|
-
* λ4 = λ3 * x1 * z1
|
94
|
-
* λ5 = λ2^2
|
95
|
-
* λ6 = λ1^2 − 8 * λ4
|
96
|
-
* x3 = λ2 * λ6
|
97
|
-
* y3 = λ1 * (4 * λ4 − λ6) − 2 * λ5 * λ3
|
98
|
-
* z3 = λ2 * λ5
|
99
|
-
*/
|
100
|
-
twice(): ECPointFp;
|
101
|
-
/**
|
102
|
-
* 倍点计算
|
103
|
-
*/
|
104
|
-
multiply(k: BigInteger): ECPointFp;
|
105
|
-
}
|
106
|
-
/**
|
107
|
-
* 椭圆曲线 y^2 = x^3 + ax + b
|
108
|
-
*/
|
109
|
-
declare class ECCurveFp {
|
110
|
-
q: BigInteger;
|
111
|
-
infinity: ECPointFp;
|
112
|
-
a: ECFieldElementFp;
|
113
|
-
b: ECFieldElementFp;
|
114
|
-
constructor(q: BigInteger, a: BigInteger, b: BigInteger);
|
115
|
-
/**
|
116
|
-
* 判断两个椭圆曲线是否相等
|
117
|
-
*/
|
118
|
-
equals(other: ECCurveFp): boolean;
|
119
|
-
/**
|
120
|
-
* 生成椭圆曲线域元素
|
121
|
-
*/
|
122
|
-
fromBigInteger(x: BigInteger): ECFieldElementFp;
|
123
|
-
/**
|
124
|
-
* 解析 16 进制串为椭圆曲线点
|
125
|
-
*/
|
126
|
-
decodePointHex(s: string): ECPointFp | null;
|
127
|
-
}
|
128
|
-
|
129
|
-
declare module 'jsbn' {
|
130
|
-
class SecureRandom implements RandomGenerator {
|
131
|
-
nextBytes(bytes: number[]): void;
|
132
|
-
}
|
133
|
-
}
|
134
|
-
/**
|
135
|
-
* 获取公共椭圆曲线
|
136
|
-
*/
|
137
|
-
declare function getGlobalCurve(): ECCurveFp;
|
138
|
-
/**
|
139
|
-
* 生成ecparam
|
140
|
-
*/
|
141
|
-
declare function generateEcparam(): {
|
142
|
-
curve: ECCurveFp;
|
143
|
-
G: {
|
144
|
-
zinv: BigInteger | null;
|
145
|
-
z: BigInteger;
|
146
|
-
curve: ECCurveFp;
|
147
|
-
x: {
|
148
|
-
/**
|
149
|
-
* 生成ecparam
|
150
|
-
*/
|
151
|
-
q: BigInteger;
|
152
|
-
x: BigInteger;
|
153
|
-
equals(other: any): boolean;
|
154
|
-
toBigInteger(): BigInteger;
|
155
|
-
negate(): any;
|
156
|
-
add(b: any): any;
|
157
|
-
subtract(b: any): any;
|
158
|
-
multiply(b: any): any;
|
159
|
-
divide(b: any): any;
|
160
|
-
square(): any;
|
161
|
-
} | null;
|
162
|
-
y: {
|
163
|
-
/**
|
164
|
-
* 生成ecparam
|
165
|
-
*/
|
166
|
-
q: BigInteger;
|
167
|
-
x: BigInteger;
|
168
|
-
equals(other: any): boolean;
|
169
|
-
toBigInteger(): BigInteger;
|
170
|
-
negate(): any;
|
171
|
-
add(b: any): any;
|
172
|
-
subtract(b: any): any;
|
173
|
-
multiply(b: any): any;
|
174
|
-
divide(b: any): any;
|
175
|
-
square(): any;
|
176
|
-
} | null;
|
177
|
-
getX(): {
|
178
|
-
/**
|
179
|
-
* 生成ecparam
|
180
|
-
*/
|
181
|
-
q: BigInteger;
|
182
|
-
x: BigInteger;
|
183
|
-
equals(other: any): boolean;
|
184
|
-
toBigInteger(): BigInteger;
|
185
|
-
negate(): any;
|
186
|
-
add(b: any): any;
|
187
|
-
subtract(b: any): any;
|
188
|
-
multiply(b: any): any;
|
189
|
-
divide(b: any): any;
|
190
|
-
square(): any;
|
191
|
-
};
|
192
|
-
getY(): {
|
193
|
-
/**
|
194
|
-
* 生成ecparam
|
195
|
-
*/
|
196
|
-
q: BigInteger;
|
197
|
-
x: BigInteger;
|
198
|
-
equals(other: any): boolean;
|
199
|
-
toBigInteger(): BigInteger;
|
200
|
-
negate(): any;
|
201
|
-
add(b: any): any;
|
202
|
-
subtract(b: any): any;
|
203
|
-
multiply(b: any): any;
|
204
|
-
divide(b: any): any;
|
205
|
-
square(): any;
|
206
|
-
};
|
207
|
-
equals(other: any): boolean;
|
208
|
-
isInfinity(): boolean;
|
209
|
-
negate(): any;
|
210
|
-
add(b: any): any;
|
211
|
-
twice(): any;
|
212
|
-
multiply(k: BigInteger): any;
|
213
|
-
};
|
214
|
-
n: BigInteger;
|
215
|
-
};
|
216
1
|
/**
|
217
2
|
* 生成密钥对:publicKey = privateKey * G
|
218
3
|
*/
|
219
|
-
declare function generateKeyPairHex(
|
4
|
+
declare function generateKeyPairHex(): {
|
220
5
|
privateKey: string;
|
221
6
|
publicKey: string;
|
222
7
|
};
|
@@ -268,8 +53,8 @@ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?:
|
|
268
53
|
output: 'string';
|
269
54
|
}): string;
|
270
55
|
interface SignaturePoint {
|
271
|
-
k:
|
272
|
-
x1:
|
56
|
+
k: bigint;
|
57
|
+
x1: bigint;
|
273
58
|
}
|
274
59
|
/**
|
275
60
|
* 签名
|
@@ -301,8 +86,8 @@ declare function getPublicKeyFromPrivateKey(privateKey: string): string;
|
|
301
86
|
* 获取椭圆曲线点
|
302
87
|
*/
|
303
88
|
declare function getPoint(): {
|
304
|
-
k:
|
305
|
-
x1:
|
89
|
+
k: bigint;
|
90
|
+
x1: bigint;
|
306
91
|
privateKey: string;
|
307
92
|
publicKey: string;
|
308
93
|
};
|
@@ -315,8 +100,6 @@ declare const index$2_doVerifySignature: typeof doVerifySignature;
|
|
315
100
|
declare const index$2_getHash: typeof getHash;
|
316
101
|
declare const index$2_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
317
102
|
declare const index$2_getPoint: typeof getPoint;
|
318
|
-
declare const index$2_getGlobalCurve: typeof getGlobalCurve;
|
319
|
-
declare const index$2_generateEcparam: typeof generateEcparam;
|
320
103
|
declare const index$2_generateKeyPairHex: typeof generateKeyPairHex;
|
321
104
|
declare const index$2_compressPublicKeyHex: typeof compressPublicKeyHex;
|
322
105
|
declare const index$2_utf8ToHex: typeof utf8ToHex;
|
@@ -337,8 +120,6 @@ declare namespace index$2 {
|
|
337
120
|
index$2_getHash as getHash,
|
338
121
|
index$2_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey,
|
339
122
|
index$2_getPoint as getPoint,
|
340
|
-
index$2_getGlobalCurve as getGlobalCurve,
|
341
|
-
index$2_generateEcparam as generateEcparam,
|
342
123
|
index$2_generateKeyPairHex as generateKeyPairHex,
|
343
124
|
index$2_compressPublicKeyHex as compressPublicKeyHex,
|
344
125
|
index$2_utf8ToHex as utf8ToHex,
|