sm-crypto-v2 1.4.0 → 1.5.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 +16 -0
- package/README.md +39 -28
- package/dist/index.d.ts +49 -62
- package/dist/index.js +425 -274
- package/dist/index.mjs +421 -270
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/sm2/ec.ts +8 -85
- package/src/sm2/hmac.ts +76 -0
- package/src/sm2/index.ts +40 -63
- package/src/sm2/kx.ts +23 -16
- package/src/sm2/rng.ts +71 -0
- package/src/sm2/sm3.ts +187 -120
- package/src/sm2/utils.ts +1 -23
- package/src/sm3/index.ts +7 -4
- package/src/sm3/utils.ts +117 -0
- package/src/sm4/index.ts +10 -12
- package/.babelrc +0 -3
- package/.eslintrc.js +0 -97
- package/.github/workflows/test.yml +0 -17
- package/benchmark/index.js +0 -29
- package/benchmark/package.json +0 -19
- package/benchmark/pnpm-lock.yaml +0 -28
- package/pnpm-lock.yaml +0 -3942
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,22 @@
|
|
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.5.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.5.0...v1.5.1) (2023-06-28)
|
6
|
+
|
7
|
+
|
8
|
+
### Bug Fixes
|
9
|
+
|
10
|
+
* **sm2/kx:** align key exchange with standard ([b4fae83](https://github.com/Cubelrti/sm-crypto-v2/commit/b4fae831ae587821a9c1fe72617420134ae7f326))
|
11
|
+
|
12
|
+
## [1.5.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.4.0...v1.5.0) (2023-06-11)
|
13
|
+
|
14
|
+
|
15
|
+
### Features
|
16
|
+
|
17
|
+
* add benchmark ([81736dd](https://github.com/Cubelrti/sm-crypto-v2/commit/81736dd3acc05131a81fcbee61ffc247c4e2f21c))
|
18
|
+
* add benchmark result ([a8a45b2](https://github.com/Cubelrti/sm-crypto-v2/commit/a8a45b2ce234ebcd8ebc4c245325a0866f3399e5))
|
19
|
+
* **sm3:** optimize sm3 ([096bd95](https://github.com/Cubelrti/sm-crypto-v2/commit/096bd9568b451ac52a9a01bc1bdd711cfeb595c6))
|
20
|
+
|
5
21
|
## [1.4.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.3.0...v1.4.0) (2023-06-09)
|
6
22
|
|
7
23
|
|
package/README.md
CHANGED
@@ -4,7 +4,9 @@
|
|
4
4
|
[](https://github.com/cubelrti/sm-crypto-v2/actions)
|
5
5
|
[](https://github.com/cubelrti/sm-crypto-v2/actions)
|
6
6
|
|
7
|
-
国密算法 sm2、sm3 和 sm4 的
|
7
|
+
国密算法 sm2、sm3 和 sm4 的 JavaScript 实现。
|
8
|
+
|
9
|
+
参数支持 TypedArray,导出 esm/cjs。
|
8
10
|
|
9
11
|
## 特性
|
10
12
|
|
@@ -12,8 +14,10 @@
|
|
12
14
|
- 📘 使用 TypeScript 实现,提供全面的类型支持
|
13
15
|
- 🔄 移除原有 `jsbn` 依赖,改用原生 BigInt
|
14
16
|
- ✔️ 通过全部历史单元测试,包括 SM2、SM3 和 SM4
|
15
|
-
- 🎲 自动选择最优的安全随机数实现,避免使用 `Math.random` 和 `Date.now` 进行模拟
|
17
|
+
- 🎲 自动选择最优的安全随机数实现,避免使用 `Math.random()` 和 `Date.now()` 进行模拟
|
16
18
|
- 📚 同时导出 ES Module 和 CommonJS 两种格式,可按需使用
|
19
|
+
- 🔑 提供 SM2 密钥交换 API
|
20
|
+
- 🎒 未压缩大小 34kb,压缩后 17kb
|
17
21
|
|
18
22
|
## 安装
|
19
23
|
|
@@ -163,7 +167,7 @@ let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array
|
|
163
167
|
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
|
164
168
|
```
|
165
169
|
|
166
|
-
### 密钥交换
|
170
|
+
### 密钥交换
|
167
171
|
|
168
172
|
```js
|
169
173
|
import { sm2 } from 'sm-crypto-v2'
|
@@ -173,10 +177,17 @@ const keyPairB = sm2.generateKeyPairHex() // B 的秘钥对
|
|
173
177
|
const ephemeralKeypairA = sm2.generateKeyPairHex() // A 的临时秘钥对
|
174
178
|
const ephemeralKeypairB = sm2.generateKeyPairHex() // B 的临时秘钥对
|
175
179
|
|
176
|
-
//
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
+
// 无身份的密钥交换
|
181
|
+
// A 所需参数:A 的秘钥对,A 的临时秘钥对,B 的公钥,B 的临时秘钥公钥,长度,是否为接收方(默认为 false)
|
182
|
+
const sharedKeyFromA = sm2.calculateSharedKey(keyPairA, ephemeralKeypairA, keyPairB.publicKey, ephemeralKeypairB.publicKey, 233)
|
183
|
+
// B 所需参数:B 的秘钥对,B 的临时秘钥对,A 的公钥,A 的临时秘钥公钥,长度,是否为接收方(默认为 false)
|
184
|
+
const sharedKeyFromB = sm2.calculateSharedKey(keyPairB, ephemeralKeypairB, keyPairA.publicKey, ephemeralKeypairA.publicKey, 233, true)
|
185
|
+
|
186
|
+
// 带身份的密钥交换
|
187
|
+
// A 所需参数:A 的秘钥对,A 的临时秘钥对,B 的公钥,B 的临时秘钥公钥,长度,是否为接收方(默认为 false),A 的身份,B 的身份
|
188
|
+
const sharedKeyFromA = sm2.calculateSharedKey(keyPairA, ephemeralKeypairA, keyPairB.publicKey, ephemeralKeypairB.publicKey, 233, false, 'alice@yahoo.com', 'bob@yahoo.com')
|
189
|
+
// B 所需参数:B 的秘钥对,B 的临时秘钥对,A 的公钥,A 的临时秘钥公钥,长度,是否为接收方(默认为 false),B 的身份,A 的身份
|
190
|
+
const sharedKeyFromB = sm2.calculateSharedKey(keyPairB, ephemeralKeypairB, keyPairA.publicKey, ephemeralKeypairA.publicKey, 233, true, 'bob@yahoo.com', 'alice@yahoo.com')
|
180
191
|
|
181
192
|
// expect(sharedKeyFromA).toEqual(sharedKeyFromB) => true
|
182
193
|
```
|
@@ -190,27 +201,27 @@ const sharedKeyFromB = sm2.calculateSharedKey(keyPairB, ephemeralKeypairB, keyPa
|
|
190
201
|
|
191
202
|
## 性能
|
192
203
|
|
193
|
-
CPU: Apple
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
204
|
+
CPU: Apple M2
|
205
|
+
|
206
|
+
| Operation | sm-crypto | sm-crypto-v2 | Difference (in times) |
|
207
|
+
|--------------------|--------------|--------------|-----------------------|
|
208
|
+
| sm2 generateKeyPair| 148 ops/sec | 3,452 ops/sec| 23.3x |
|
209
|
+
| sm2 encrypt | 76 ops/sec | 304 ops/sec | 4x |
|
210
|
+
| sm2 sign | 150 ops/sec | 3,829 ops/sec| 25.5x |
|
211
|
+
| sm2 verify | 76 ops/sec | 306 ops/sec | 4x |
|
212
|
+
| sm3 hash | 322 ops/sec | 519 ops/sec | 1.6x |
|
213
|
+
| sm3 hmac | 244 ops/sec | 518 ops/sec | 2.1x |
|
214
|
+
| sm4 encrypt | 102,009 ops/sec | 102,124 ops/sec | 1x |
|
215
|
+
| sm4 decrypt | 143,430 ops/sec | 237,247 ops/sec | 1.7x |
|
216
|
+
|
217
|
+
内存:
|
218
|
+
|
219
|
+
| Metric | sm-crypto | sm-crypto-v2 | Difference |
|
220
|
+
|--------------------|--------------|--------------|-----------------------|
|
221
|
+
| RAM (rss) | 57.9mb | 57.7mb | -0.2mb |
|
222
|
+
| RAM (heap) | 16.6mb | 16.6mb | 0mb |
|
223
|
+
| RAM (used) | 10.4mb | 10.5mb | +0.1mb |
|
224
|
+
| RAM (end - start) | 83.1mb | 71.8mb | -11.3mb |
|
214
225
|
|
215
226
|
## 协议
|
216
227
|
|
package/dist/index.d.ts
CHANGED
@@ -38,12 +38,12 @@ declare function verifyPublicKey(publicKey: string): boolean;
|
|
38
38
|
* 验证公钥是否等价,等价返回true
|
39
39
|
*/
|
40
40
|
declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): boolean;
|
41
|
-
declare function concatArray(...arrays: Uint8Array[]): Uint8Array;
|
42
41
|
|
43
42
|
declare function initRNGPool(): Promise<void>;
|
44
43
|
|
45
|
-
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string,
|
44
|
+
declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array;
|
46
45
|
|
46
|
+
declare const EmptyArray: Uint8Array;
|
47
47
|
/**
|
48
48
|
* 加密
|
49
49
|
*/
|
@@ -79,6 +79,7 @@ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, pu
|
|
79
79
|
hash?: boolean;
|
80
80
|
userId?: string;
|
81
81
|
}): boolean;
|
82
|
+
declare function getZ(publicKey: string, userId?: string): Uint8Array;
|
82
83
|
/**
|
83
84
|
* sm3杂凑算法
|
84
85
|
*/
|
@@ -97,74 +98,60 @@ declare function getPoint(): {
|
|
97
98
|
publicKey: string;
|
98
99
|
};
|
99
100
|
|
100
|
-
declare const index$
|
101
|
-
declare const index$
|
102
|
-
|
103
|
-
|
104
|
-
declare const index$
|
105
|
-
declare const index$
|
106
|
-
declare const index$
|
107
|
-
declare const index$
|
108
|
-
declare const index$
|
109
|
-
declare const index$
|
110
|
-
|
111
|
-
declare const index$
|
112
|
-
|
113
|
-
declare const index$
|
114
|
-
declare const index$
|
115
|
-
declare const index$
|
116
|
-
declare const index$
|
117
|
-
declare const index$
|
118
|
-
declare const index$
|
119
|
-
declare const index$
|
120
|
-
declare const index$
|
121
|
-
declare
|
101
|
+
declare const index$1_EmptyArray: typeof EmptyArray;
|
102
|
+
declare const index$1_doEncrypt: typeof doEncrypt;
|
103
|
+
declare const index$1_doDecrypt: typeof doDecrypt;
|
104
|
+
type index$1_SignaturePoint = SignaturePoint;
|
105
|
+
declare const index$1_doSignature: typeof doSignature;
|
106
|
+
declare const index$1_doVerifySignature: typeof doVerifySignature;
|
107
|
+
declare const index$1_getZ: typeof getZ;
|
108
|
+
declare const index$1_getHash: typeof getHash;
|
109
|
+
declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
110
|
+
declare const index$1_getPoint: typeof getPoint;
|
111
|
+
declare const index$1_initRNGPool: typeof initRNGPool;
|
112
|
+
declare const index$1_calculateSharedKey: typeof calculateSharedKey;
|
113
|
+
type index$1_KeyPair = KeyPair;
|
114
|
+
declare const index$1_generateKeyPairHex: typeof generateKeyPairHex;
|
115
|
+
declare const index$1_compressPublicKeyHex: typeof compressPublicKeyHex;
|
116
|
+
declare const index$1_utf8ToHex: typeof utf8ToHex;
|
117
|
+
declare const index$1_leftPad: typeof leftPad;
|
118
|
+
declare const index$1_arrayToHex: typeof arrayToHex;
|
119
|
+
declare const index$1_arrayToUtf8: typeof arrayToUtf8;
|
120
|
+
declare const index$1_hexToArray: typeof hexToArray;
|
121
|
+
declare const index$1_verifyPublicKey: typeof verifyPublicKey;
|
122
|
+
declare const index$1_comparePublicKeyHex: typeof comparePublicKeyHex;
|
123
|
+
declare namespace index$1 {
|
122
124
|
export {
|
123
|
-
index$
|
124
|
-
index$
|
125
|
-
index$
|
126
|
-
index$
|
127
|
-
index$
|
128
|
-
index$
|
129
|
-
index$
|
130
|
-
index$
|
131
|
-
index$
|
132
|
-
index$
|
133
|
-
index$
|
134
|
-
index$
|
135
|
-
index$
|
136
|
-
index$
|
137
|
-
index$
|
138
|
-
index$
|
139
|
-
index$
|
140
|
-
index$
|
141
|
-
index$
|
142
|
-
index$
|
143
|
-
index$
|
125
|
+
index$1_EmptyArray as EmptyArray,
|
126
|
+
index$1_doEncrypt as doEncrypt,
|
127
|
+
index$1_doDecrypt as doDecrypt,
|
128
|
+
index$1_SignaturePoint as SignaturePoint,
|
129
|
+
index$1_doSignature as doSignature,
|
130
|
+
index$1_doVerifySignature as doVerifySignature,
|
131
|
+
index$1_getZ as getZ,
|
132
|
+
index$1_getHash as getHash,
|
133
|
+
index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey,
|
134
|
+
index$1_getPoint as getPoint,
|
135
|
+
index$1_initRNGPool as initRNGPool,
|
136
|
+
index$1_calculateSharedKey as calculateSharedKey,
|
137
|
+
index$1_KeyPair as KeyPair,
|
138
|
+
index$1_generateKeyPairHex as generateKeyPairHex,
|
139
|
+
index$1_compressPublicKeyHex as compressPublicKeyHex,
|
140
|
+
index$1_utf8ToHex as utf8ToHex,
|
141
|
+
index$1_leftPad as leftPad,
|
142
|
+
index$1_arrayToHex as arrayToHex,
|
143
|
+
index$1_arrayToUtf8 as arrayToUtf8,
|
144
|
+
index$1_hexToArray as hexToArray,
|
145
|
+
index$1_verifyPublicKey as verifyPublicKey,
|
146
|
+
index$1_comparePublicKeyHex as comparePublicKeyHex,
|
144
147
|
};
|
145
148
|
}
|
146
149
|
|
147
|
-
/**
|
148
|
-
* 补全16进制字符串
|
149
|
-
*/
|
150
|
-
/**
|
151
|
-
* utf8 串转字节数组
|
152
|
-
*/
|
153
|
-
declare function utf8ToArray(str: string): Uint8Array;
|
154
150
|
declare function sm3(input: string | Uint8Array, options?: {
|
155
151
|
key: Uint8Array | string;
|
156
152
|
mode?: 'hmac' | 'mac';
|
157
153
|
}): string;
|
158
154
|
|
159
|
-
declare const index$1_utf8ToArray: typeof utf8ToArray;
|
160
|
-
declare const index$1_sm3: typeof sm3;
|
161
|
-
declare namespace index$1 {
|
162
|
-
export {
|
163
|
-
index$1_utf8ToArray as utf8ToArray,
|
164
|
-
index$1_sm3 as sm3,
|
165
|
-
};
|
166
|
-
}
|
167
|
-
|
168
155
|
interface SM4Options {
|
169
156
|
padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
|
170
157
|
mode?: 'cbc' | 'ecb';
|
@@ -198,4 +185,4 @@ declare namespace index {
|
|
198
185
|
};
|
199
186
|
}
|
200
187
|
|
201
|
-
export { index$
|
188
|
+
export { index$1 as sm2, sm3, index as sm4 };
|