sm-crypto-v2 1.5.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 +7 -0
- package/README.md +13 -6
- package/dist/index.d.ts +4 -1
- package/dist/index.js +13 -5
- package/dist/index.mjs +13 -5
- package/package.json +1 -1
- package/src/sm2/index.ts +9 -4
- package/src/sm2/kx.ts +9 -6
- 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 -32
- package/pnpm-lock.yaml +0 -3946
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,13 @@
|
|
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
|
+
|
5
12
|
## [1.5.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.4.0...v1.5.0) (2023-06-11)
|
6
13
|
|
7
14
|
|
package/README.md
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
- ✔️ 通过全部历史单元测试,包括 SM2、SM3 和 SM4
|
17
17
|
- 🎲 自动选择最优的安全随机数实现,避免使用 `Math.random()` 和 `Date.now()` 进行模拟
|
18
18
|
- 📚 同时导出 ES Module 和 CommonJS 两种格式,可按需使用
|
19
|
-
- 🔑
|
19
|
+
- 🔑 提供 SM2 密钥交换 API
|
20
20
|
- 🎒 未压缩大小 34kb,压缩后 17kb
|
21
21
|
|
22
22
|
## 安装
|
@@ -167,7 +167,7 @@ let decryptData = sm4.decrypt(encryptData, key, {padding: 'none', output: 'array
|
|
167
167
|
let decryptData = sm4.decrypt(encryptData, key, {mode: 'cbc', iv: 'fedcba98765432100123456789abcdef'}) // 解密,cbc 模式
|
168
168
|
```
|
169
169
|
|
170
|
-
### 密钥交换
|
170
|
+
### 密钥交换
|
171
171
|
|
172
172
|
```js
|
173
173
|
import { sm2 } from 'sm-crypto-v2'
|
@@ -177,10 +177,17 @@ const keyPairB = sm2.generateKeyPairHex() // B 的秘钥对
|
|
177
177
|
const ephemeralKeypairA = sm2.generateKeyPairHex() // A 的临时秘钥对
|
178
178
|
const ephemeralKeypairB = sm2.generateKeyPairHex() // B 的临时秘钥对
|
179
179
|
|
180
|
-
//
|
181
|
-
|
182
|
-
|
183
|
-
|
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')
|
184
191
|
|
185
192
|
// expect(sharedKeyFromA).toEqual(sharedKeyFromB) => true
|
186
193
|
```
|
package/dist/index.d.ts
CHANGED
@@ -41,7 +41,7 @@ declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): bo
|
|
41
41
|
|
42
42
|
declare function initRNGPool(): Promise<void>;
|
43
43
|
|
44
|
-
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;
|
45
45
|
|
46
46
|
declare const EmptyArray: Uint8Array;
|
47
47
|
/**
|
@@ -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
|
*/
|
@@ -103,6 +104,7 @@ declare const index$1_doDecrypt: typeof doDecrypt;
|
|
103
104
|
type index$1_SignaturePoint = SignaturePoint;
|
104
105
|
declare const index$1_doSignature: typeof doSignature;
|
105
106
|
declare const index$1_doVerifySignature: typeof doVerifySignature;
|
107
|
+
declare const index$1_getZ: typeof getZ;
|
106
108
|
declare const index$1_getHash: typeof getHash;
|
107
109
|
declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
|
108
110
|
declare const index$1_getPoint: typeof getPoint;
|
@@ -126,6 +128,7 @@ declare namespace index$1 {
|
|
126
128
|
index$1_SignaturePoint as SignaturePoint,
|
127
129
|
index$1_doSignature as doSignature,
|
128
130
|
index$1_doVerifySignature as doVerifySignature,
|
131
|
+
index$1_getZ as getZ,
|
129
132
|
index$1_getHash as getHash,
|
130
133
|
index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey,
|
131
134
|
index$1_getPoint as getPoint,
|
package/dist/index.js
CHANGED
@@ -49,6 +49,7 @@ __export(sm2_exports, {
|
|
49
49
|
getHash: () => getHash,
|
50
50
|
getPoint: () => getPoint,
|
51
51
|
getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
|
52
|
+
getZ: () => getZ,
|
52
53
|
hexToArray: () => hexToArray,
|
53
54
|
initRNGPool: () => initRNGPool,
|
54
55
|
leftPad: () => leftPad,
|
@@ -679,16 +680,19 @@ function hkdf(z, keylen) {
|
|
679
680
|
}
|
680
681
|
return msg;
|
681
682
|
}
|
682
|
-
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, idA = "1234567812345678", idB = "1234567812345678"
|
683
|
+
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
683
684
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
684
685
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
685
686
|
const PB = sm2Curve.ProjectivePoint.fromHex(publicKeyB);
|
686
|
-
|
687
|
-
|
687
|
+
let ZA = getZ(keypairA.publicKey, idA);
|
688
|
+
let ZB = getZ(publicKeyB, idB);
|
689
|
+
if (isRecipient) {
|
690
|
+
[ZA, ZB] = [ZB, ZA];
|
691
|
+
}
|
688
692
|
const rA = utils3.hexToNumber(ephemeralKeypairA.privateKey);
|
689
693
|
const dA = utils3.hexToNumber(keypairA.privateKey);
|
690
694
|
const x1 = RA.x;
|
691
|
-
const x1_ =
|
695
|
+
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
692
696
|
const tA = field.add(dA, field.mulN(x1_, rA));
|
693
697
|
const x2 = RB.x;
|
694
698
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
@@ -828,7 +832,7 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
828
832
|
const R = field.add(e, x1y1.x);
|
829
833
|
return r === R;
|
830
834
|
}
|
831
|
-
function
|
835
|
+
function getZ(publicKey, userId = "1234567812345678") {
|
832
836
|
userId = utf8ToHex(userId);
|
833
837
|
const a = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
834
838
|
const b = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
@@ -847,6 +851,10 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
|
847
851
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
848
852
|
const entl = userId.length * 4;
|
849
853
|
const z = sm3(utils4.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
854
|
+
return z;
|
855
|
+
}
|
856
|
+
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
857
|
+
const z = getZ(publicKey, userId);
|
850
858
|
return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
851
859
|
}
|
852
860
|
function getPublicKeyFromPrivateKey(privateKey) {
|
package/dist/index.mjs
CHANGED
@@ -21,6 +21,7 @@ __export(sm2_exports, {
|
|
21
21
|
getHash: () => getHash,
|
22
22
|
getPoint: () => getPoint,
|
23
23
|
getPublicKeyFromPrivateKey: () => getPublicKeyFromPrivateKey,
|
24
|
+
getZ: () => getZ,
|
24
25
|
hexToArray: () => hexToArray,
|
25
26
|
initRNGPool: () => initRNGPool,
|
26
27
|
leftPad: () => leftPad,
|
@@ -651,16 +652,19 @@ function hkdf(z, keylen) {
|
|
651
652
|
}
|
652
653
|
return msg;
|
653
654
|
}
|
654
|
-
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, idA = "1234567812345678", idB = "1234567812345678"
|
655
|
+
function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPublicKeyB, sharedKeyLength, isRecipient = false, idA = "1234567812345678", idB = "1234567812345678") {
|
655
656
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey);
|
656
657
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB);
|
657
658
|
const PB = sm2Curve.ProjectivePoint.fromHex(publicKeyB);
|
658
|
-
|
659
|
-
|
659
|
+
let ZA = getZ(keypairA.publicKey, idA);
|
660
|
+
let ZB = getZ(publicKeyB, idB);
|
661
|
+
if (isRecipient) {
|
662
|
+
[ZA, ZB] = [ZB, ZA];
|
663
|
+
}
|
660
664
|
const rA = utils3.hexToNumber(ephemeralKeypairA.privateKey);
|
661
665
|
const dA = utils3.hexToNumber(keypairA.privateKey);
|
662
666
|
const x1 = RA.x;
|
663
|
-
const x1_ =
|
667
|
+
const x1_ = wPow2 + (x1 & wPow2Sub1);
|
664
668
|
const tA = field.add(dA, field.mulN(x1_, rA));
|
665
669
|
const x2 = RB.x;
|
666
670
|
const x2_ = field.add(wPow2, x2 & wPow2Sub1);
|
@@ -800,7 +804,7 @@ function doVerifySignature(msg, signHex, publicKey, options = {}) {
|
|
800
804
|
const R = field.add(e, x1y1.x);
|
801
805
|
return r === R;
|
802
806
|
}
|
803
|
-
function
|
807
|
+
function getZ(publicKey, userId = "1234567812345678") {
|
804
808
|
userId = utf8ToHex(userId);
|
805
809
|
const a = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.a), 64);
|
806
810
|
const b = leftPad(utils4.numberToHexUnpadded(sm2Curve.CURVE.b), 64);
|
@@ -819,6 +823,10 @@ function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
|
819
823
|
const data = hexToArray(userId + a + b + gx + gy + px + py);
|
820
824
|
const entl = userId.length * 4;
|
821
825
|
const z = sm3(utils4.concatBytes(new Uint8Array([entl >> 8 & 255, entl & 255]), data));
|
826
|
+
return z;
|
827
|
+
}
|
828
|
+
function getHash(hashHex, publicKey, userId = "1234567812345678") {
|
829
|
+
const z = getZ(publicKey, userId);
|
822
830
|
return bytesToHex(sm3(utils4.concatBytes(z, typeof hashHex === "string" ? hexToArray(hashHex) : hashHex)));
|
823
831
|
}
|
824
832
|
function getPublicKeyFromPrivateKey(privateKey) {
|
package/package.json
CHANGED
package/src/sm2/index.ts
CHANGED
@@ -209,10 +209,7 @@ export function doVerifySignature(msg: string | Uint8Array, signHex: string, pub
|
|
209
209
|
return r === R
|
210
210
|
}
|
211
211
|
|
212
|
-
|
213
|
-
* sm3杂凑算法
|
214
|
-
*/
|
215
|
-
export function getHash(hashHex: string | Uint8Array, publicKey: string, userId = '1234567812345678') {
|
212
|
+
export function getZ(publicKey: string, userId = '1234567812345678') {
|
216
213
|
// z = hash(entl || userId || a || b || gx || gy || px || py)
|
217
214
|
userId = utf8ToHex(userId)
|
218
215
|
const a = leftPad(utils.numberToHexUnpadded(sm2Curve.CURVE.a), 64)
|
@@ -241,6 +238,14 @@ export function getHash(hashHex: string | Uint8Array, publicKey: string, userId
|
|
241
238
|
|
242
239
|
const z = sm3(utils.concatBytes(new Uint8Array([entl >> 8 & 0x00ff, entl & 0x00ff]), data))
|
243
240
|
|
241
|
+
return z
|
242
|
+
}
|
243
|
+
|
244
|
+
/**
|
245
|
+
* sm3杂凑算法
|
246
|
+
*/
|
247
|
+
export function getHash(hashHex: string | Uint8Array, publicKey: string, userId = '1234567812345678') {
|
248
|
+
const z = getZ(publicKey, userId)
|
244
249
|
// e = hash(z || msg)
|
245
250
|
return bytesToHex(sm3(utils.concatBytes(z, typeof hashHex === 'string' ? hexToArray(hashHex) : hashHex)))
|
246
251
|
}
|
package/src/sm2/kx.ts
CHANGED
@@ -2,7 +2,7 @@ import { field, sm2Curve } from './ec';
|
|
2
2
|
import { KeyPair, hexToArray, leftPad } from './utils';
|
3
3
|
import * as utils from '@noble/curves/abstract/utils';
|
4
4
|
import { sm3 } from './sm3';
|
5
|
-
import { EmptyArray } from '.';
|
5
|
+
import { EmptyArray, getZ } from '.';
|
6
6
|
|
7
7
|
|
8
8
|
// 用到的常数
|
@@ -39,28 +39,31 @@ function hkdf(z: Uint8Array, keylen: number) {
|
|
39
39
|
return msg
|
40
40
|
}
|
41
41
|
|
42
|
-
|
43
42
|
export function calculateSharedKey(
|
44
43
|
keypairA: KeyPair,
|
45
44
|
ephemeralKeypairA: KeyPair,
|
46
45
|
publicKeyB: string,
|
47
46
|
ephemeralPublicKeyB: string,
|
47
|
+
sharedKeyLength: number,
|
48
|
+
isRecipient = false,
|
48
49
|
idA: string = '1234567812345678',
|
49
50
|
idB: string = '1234567812345678',
|
50
|
-
sharedKeyLength: number,
|
51
51
|
) {
|
52
52
|
const RA = sm2Curve.ProjectivePoint.fromHex(ephemeralKeypairA.publicKey)
|
53
53
|
const RB = sm2Curve.ProjectivePoint.fromHex(ephemeralPublicKeyB)
|
54
54
|
// const PA = sm2Curve.ProjectivePoint.fromHex(keypairA.publicKey) // 用不到
|
55
55
|
const PB = sm2Curve.ProjectivePoint.fromHex(publicKeyB)
|
56
|
-
|
57
|
-
|
56
|
+
let ZA = getZ(keypairA.publicKey, idA)
|
57
|
+
let ZB = getZ(publicKeyB, idB)
|
58
|
+
if (isRecipient) {
|
59
|
+
[ZA, ZB] = [ZB, ZA];
|
60
|
+
}
|
58
61
|
const rA = utils.hexToNumber(ephemeralKeypairA.privateKey)
|
59
62
|
const dA = utils.hexToNumber(keypairA.privateKey)
|
60
63
|
// 1.先算 tA
|
61
64
|
const x1 = RA.x
|
62
65
|
// x1_ = 2^w + (x1 & (2^w - 1))
|
63
|
-
const x1_ =
|
66
|
+
const x1_ = wPow2 + (x1 & wPow2Sub1)
|
64
67
|
// tA = (dA + x1b * rA) mod n
|
65
68
|
const tA = field.add(dA, field.mulN(x1_, rA))
|
66
69
|
|
package/.babelrc
DELETED
package/.eslintrc.js
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
module.exports = {
|
2
|
-
'extends': [
|
3
|
-
'airbnb-base',
|
4
|
-
'plugin:promise/recommended'
|
5
|
-
],
|
6
|
-
'parserOptions': {
|
7
|
-
'ecmaVersion': 9,
|
8
|
-
'ecmaFeatures': {
|
9
|
-
'jsx': false
|
10
|
-
},
|
11
|
-
'sourceType': 'module'
|
12
|
-
},
|
13
|
-
'env': {
|
14
|
-
'es6': true,
|
15
|
-
'node': true,
|
16
|
-
'jest': true
|
17
|
-
},
|
18
|
-
'plugins': [
|
19
|
-
'import',
|
20
|
-
'node',
|
21
|
-
'promise'
|
22
|
-
],
|
23
|
-
'rules': {
|
24
|
-
'arrow-parens': 'off',
|
25
|
-
'comma-dangle': [
|
26
|
-
'error',
|
27
|
-
'only-multiline'
|
28
|
-
],
|
29
|
-
'complexity': ['error', 20],
|
30
|
-
'func-names': 'off',
|
31
|
-
'global-require': 'off',
|
32
|
-
'handle-callback-err': [
|
33
|
-
'error',
|
34
|
-
'^(err|error)$'
|
35
|
-
],
|
36
|
-
'import/no-unresolved': [
|
37
|
-
'error',
|
38
|
-
{
|
39
|
-
'caseSensitive': true,
|
40
|
-
'commonjs': true,
|
41
|
-
'ignore': ['^[^.]']
|
42
|
-
}
|
43
|
-
],
|
44
|
-
'import/prefer-default-export': 'off',
|
45
|
-
'linebreak-style': 'off',
|
46
|
-
'no-catch-shadow': 'error',
|
47
|
-
'no-continue': 'off',
|
48
|
-
'no-div-regex': 'warn',
|
49
|
-
'no-else-return': 'off',
|
50
|
-
'no-param-reassign': 'off',
|
51
|
-
'no-plusplus': 'off',
|
52
|
-
'no-shadow': 'off',
|
53
|
-
'no-multi-assign': 'off',
|
54
|
-
'no-underscore-dangle': 'off',
|
55
|
-
'node/no-deprecated-api': 'error',
|
56
|
-
'node/process-exit-as-throw': 'error',
|
57
|
-
'object-curly-spacing': [
|
58
|
-
'error',
|
59
|
-
'never'
|
60
|
-
],
|
61
|
-
'operator-linebreak': [
|
62
|
-
'error',
|
63
|
-
'after',
|
64
|
-
{
|
65
|
-
'overrides': {
|
66
|
-
':': 'before',
|
67
|
-
'?': 'before'
|
68
|
-
}
|
69
|
-
}
|
70
|
-
],
|
71
|
-
'prefer-arrow-callback': 'off',
|
72
|
-
'prefer-destructuring': 'off',
|
73
|
-
'prefer-template': 'off',
|
74
|
-
'quote-props': [
|
75
|
-
1,
|
76
|
-
'as-needed',
|
77
|
-
{
|
78
|
-
'unnecessary': true
|
79
|
-
}
|
80
|
-
],
|
81
|
-
'semi': [
|
82
|
-
'error',
|
83
|
-
'never'
|
84
|
-
],
|
85
|
-
'max-len': 'off',
|
86
|
-
'no-bitwise': 'off',
|
87
|
-
'no-mixed-operators': 'off',
|
88
|
-
},
|
89
|
-
'globals': {
|
90
|
-
'window': true,
|
91
|
-
'document': true,
|
92
|
-
'App': true,
|
93
|
-
'Page': true,
|
94
|
-
'Component': true,
|
95
|
-
'Behavior': true
|
96
|
-
}
|
97
|
-
}
|
@@ -1,17 +0,0 @@
|
|
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/benchmark/index.js
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
import { run, mark, utils } from 'micro-bmark';
|
2
|
-
import * as sm from 'sm-crypto'
|
3
|
-
import * as smV2 from 'sm-crypto-v2'
|
4
|
-
|
5
|
-
const msg = 'Hello world~!'
|
6
|
-
const longMsg = msg.repeat(10000)
|
7
|
-
const keypair = smV2.sm2.generateKeyPairHex('12345678901234567890')
|
8
|
-
|
9
|
-
run(async () => {
|
10
|
-
await smV2.sm2.initRNGPool()
|
11
|
-
const sig = smV2.sm2.doSignature(msg, keypair.privateKey, { publicKey: keypair.publicKey})
|
12
|
-
|
13
|
-
const RAM = !!process.env.RAM
|
14
|
-
|
15
|
-
const backend = process.env.V2 ? smV2 : sm;
|
16
|
-
console.log();
|
17
|
-
if (RAM) utils.logMem();
|
18
|
-
console.log(`=== sm-crypto${process.env.V2 ? '-v2' : ''} ===`)
|
19
|
-
await mark('sm2 generateKeyPair', 100, () => backend.sm2.generateKeyPairHex())
|
20
|
-
await mark('sm2 encrypt', 500, () => backend.sm2.doEncrypt(msg, keypair.publicKey));
|
21
|
-
await mark('sm2 sign', 500, () => backend.sm2.doSignature(msg, keypair.privateKey, { publicKey: keypair.publicKey}));
|
22
|
-
await mark('sm2 verify', 500, () => backend.sm2.doVerifySignature(msg, sig, keypair.publicKey));
|
23
|
-
await mark('sm3 hash', 1000, () => backend.default.sm3(longMsg))
|
24
|
-
await mark('sm3 hmac', 1000, () => backend.default.sm3(longMsg, { key: 'asdfgh' }))
|
25
|
-
await mark('sm4 encrypt', 1000, () => backend.default.sm4.encrypt('hello world! 我是 juneandgreen.', '0123456789abcdeffedcba9876543210'))
|
26
|
-
await mark('sm4 decrypt', 1000, () => backend.default.sm4.encrypt('681edf34d206965e86b3e94f536e4246002a8a4efa863ccad024ac0300bb40d2', '0123456789abcdeffedcba9876543210'))
|
27
|
-
|
28
|
-
if (RAM) utils.logMem();
|
29
|
-
});
|
package/benchmark/package.json
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"name": "benchmark",
|
3
|
-
"private": true,
|
4
|
-
"version": "0.1.0",
|
5
|
-
"description": "benchmarks",
|
6
|
-
"main": "index.js",
|
7
|
-
"type": "module",
|
8
|
-
"scripts": {
|
9
|
-
"bench": "node index.js"
|
10
|
-
},
|
11
|
-
"keywords": [],
|
12
|
-
"author": "",
|
13
|
-
"license": "MIT",
|
14
|
-
"dependencies": {
|
15
|
-
"micro-bmark": "^0.3.1",
|
16
|
-
"sm-crypto": "^0.3.12",
|
17
|
-
"sm-crypto-v2": "^1.2.2"
|
18
|
-
}
|
19
|
-
}
|
package/benchmark/pnpm-lock.yaml
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
lockfileVersion: '6.1'
|
2
|
-
|
3
|
-
settings:
|
4
|
-
autoInstallPeers: true
|
5
|
-
excludeLinksFromLockfile: false
|
6
|
-
|
7
|
-
dependencies:
|
8
|
-
micro-bmark:
|
9
|
-
specifier: ^0.3.1
|
10
|
-
version: 0.3.1
|
11
|
-
sm-crypto:
|
12
|
-
specifier: ^0.3.12
|
13
|
-
version: 0.3.12
|
14
|
-
sm-crypto-v2:
|
15
|
-
specifier: ^1.2.2
|
16
|
-
version: link:..
|
17
|
-
|
18
|
-
packages:
|
19
|
-
|
20
|
-
/jsbn@1.1.0:
|
21
|
-
resolution: {integrity: sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==}
|
22
|
-
dev: false
|
23
|
-
|
24
|
-
/micro-bmark@0.3.1:
|
25
|
-
resolution: {integrity: sha512-bNaKObD4yPAAPrpEqp5jO6LJ2sEFgLoFSmRjEY809mJ62+2AehI/K3+RlVpN3Oo92RHpgC2RQhj6b1Tb4dmo+w==}
|
26
|
-
dev: false
|
27
|
-
|
28
|
-
/sm-crypto@0.3.12:
|
29
|
-
resolution: {integrity: sha512-272PBzB4PYaBdeGa41TH9ZlMGLPVRmS36Gs4FjmHwXIdihQypAbhhFWZTaa/3de69q2KfMme1M5O2W5+spAdrg==}
|
30
|
-
dependencies:
|
31
|
-
jsbn: 1.1.0
|
32
|
-
dev: false
|