sm-crypto-v2 0.3.12 → 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.
@@ -0,0 +1,17 @@
1
+ # .github/workflows/test.yml
2
+ name: Test
3
+ on: [push, pull_request, workflow_dispatch]
4
+ jobs:
5
+ test:
6
+ runs-on: ubuntu-latest
7
+ steps:
8
+ # Your original steps
9
+ - uses: actions/checkout@v3
10
+ - uses: actions/setup-node@v3
11
+ - name: Install
12
+ run: npm install
13
+ - name: Test and Coverage
14
+ run: npm run coverage # or npm run coverage
15
+ # Add this
16
+ - name: Update Coverage Badge
17
+ uses: we-cli/coverage-badge-action@main
package/CHANGELOG.md CHANGED
@@ -1,3 +1,52 @@
1
+ # Changelog
2
+
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
+
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
+
28
+ ### 0.3.13 (2023-06-06)
29
+
30
+
31
+ ### Features
32
+
33
+ * add asn1 test and coverage ([da9a6f7](https://github.com/Cubelrti/sm-crypto-v2/commit/da9a6f7e06f5e93b0849f80d0efc9131d151fdab))
34
+ * add dev deps ([5e2b3f3](https://github.com/Cubelrti/sm-crypto-v2/commit/5e2b3f33811d8d927da7954a3f4c0ecb87dec354))
35
+ * add test ([e97ce86](https://github.com/Cubelrti/sm-crypto-v2/commit/e97ce869f5f186cef3b02beeeb0607786823f612))
36
+ * sm2 support binary array ([db4c6c7](https://github.com/Cubelrti/sm-crypto-v2/commit/db4c6c7f5b2abd7926f9fc4118190cea3d527331))
37
+ * **sm2:** add shortcut for sm2 verify, better typing ([84f7f87](https://github.com/Cubelrti/sm-crypto-v2/commit/84f7f87672cb26fd5a5ec1b98be2765e07e2825b))
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * invalid padding check ([89189e3](https://github.com/Cubelrti/sm-crypto-v2/commit/89189e30f4f1083d973e7f57ae3e0e0f220f0efe))
43
+ * remove debug log ([b0f5f2a](https://github.com/Cubelrti/sm-crypto-v2/commit/b0f5f2a9808ff707d874e96c47556e55e26b4929))
44
+ * **sm2:** correct entl impl ([3d7d40d](https://github.com/Cubelrti/sm-crypto-v2/commit/3d7d40db162e76bc052771e7b9c26debdaa832a8))
45
+ * **test:** output reporter ([252ffdd](https://github.com/Cubelrti/sm-crypto-v2/commit/252ffdd289f6325d5e780e01de8137d8e3fe3453))
46
+ * **test:** summary ([73b83fa](https://github.com/Cubelrti/sm-crypto-v2/commit/73b83fa2b30db8311365841c603589f525c2d116))
47
+ * use pkcs[#7](https://github.com/Cubelrti/sm-crypto-v2/issues/7) instead of pkcs[#5](https://github.com/Cubelrti/sm-crypto-v2/issues/5) ([3f24a2a](https://github.com/Cubelrti/sm-crypto-v2/commit/3f24a2a45f09eaffc14271126b848115edf75fb7))
48
+ * use Uint8Array whenever possible ([d98cfcc](https://github.com/Cubelrti/sm-crypto-v2/commit/d98cfcc2d9b14040873b1efeda4e817ab12128d1))
49
+
1
50
  ## 0.3.12
2
51
 
3
52
  * 优化 sm3 运行性能
package/README.md CHANGED
@@ -1,7 +1,19 @@
1
- # sm-crypto
1
+ # sm-crypto-v2
2
+
3
+ [![status](https://img.shields.io/github/actions/workflow/status/cubelrti/sm-crypto-v2/test.yml?branch=master)](https://github.com/cubelrti/sm-crypto-v2/actions)
4
+ [![cov](https://cubelrti.github.io/sm-crypto-v2/badges/coverage.svg)](https://github.com/cubelrti/sm-crypto-v2/actions)
5
+
2
6
 
3
7
  国密算法 sm2、sm3 和 sm4 的 TypeScript 实现。参数支持 TypedArray,导出 esm/cjs。
4
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
+
5
17
  ## 安装
6
18
 
7
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(a?: number | string, b?: number, c?: RandomGenerator): {
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: BigInteger;
272
- x1: BigInteger;
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: BigInteger;
305
- x1: BigInteger;
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,