sm-crypto-v2 1.2.0 → 1.2.2

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 CHANGED
@@ -2,6 +2,15 @@
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.2](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.2.1...v1.2.2) (2023-06-07)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **sm2:** add test for prng ([6502a34](https://github.com/Cubelrti/sm-crypto-v2/commit/6502a3440dae2ecfe62603239448076a11c760cf))
11
+
12
+ ### [1.2.1](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.2.0...v1.2.1) (2023-06-07)
13
+
5
14
  ## [1.2.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.1.0...v1.2.0) (2023-06-07)
6
15
 
7
16
 
package/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # sm-crypto-v2
2
2
 
3
+ [![npm version](https://badge.fury.io/js/sm-crypto-v2.svg)](https://badge.fury.io/js/sm-crypto-v2)
3
4
  [![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
5
  [![cov](https://cubelrti.github.io/sm-crypto-v2/badges/coverage.svg)](https://github.com/cubelrti/sm-crypto-v2/actions)
5
6
 
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 生成密钥对:publicKey = privateKey * G
3
3
  */
4
- declare function generateKeyPairHex(): {
4
+ declare function generateKeyPairHex(str?: string): {
5
5
  privateKey: string;
6
6
  publicKey: string;
7
7
  };
package/dist/index.js CHANGED
@@ -360,7 +360,7 @@ function sm32(input, options) {
360
360
 
361
361
  // src/sm2/ec.ts
362
362
  var DEFAULT_PRNG_POOL_SIZE = 4096;
363
- var prngPool = new Uint8Array(DEFAULT_PRNG_POOL_SIZE);
363
+ var prngPool = new Uint8Array(0);
364
364
  async function FillPRNGPoolIfNeeded() {
365
365
  if ("crypto" in globalThis)
366
366
  return;
@@ -405,7 +405,8 @@ function randomBytes(length = 0) {
405
405
  if ("crypto" in globalThis) {
406
406
  return globalThis.crypto.getRandomValues(array);
407
407
  } else {
408
- return consumePool(length);
408
+ const result = consumePool(length);
409
+ return result;
409
410
  }
410
411
  }
411
412
  function createHash() {
@@ -431,8 +432,8 @@ var sm2Curve = (0, import_weierstrass.weierstrass)({
431
432
 
432
433
  // src/sm2/utils.ts
433
434
  var import_modular2 = require("@noble/curves/abstract/modular");
434
- function generateKeyPairHex() {
435
- const privateKey = sm2Curve.utils.randomPrivateKey();
435
+ function generateKeyPairHex(str) {
436
+ const privateKey = str ? utils2.numberToBytesBE((0, import_modular2.mod)(BigInt(str), ONE) + ONE, 32) : sm2Curve.utils.randomPrivateKey();
436
437
  const publicKey = sm2Curve.getPublicKey(privateKey, false);
437
438
  const privPad = leftPad2(utils2.bytesToHex(privateKey), 64);
438
439
  const pubPad = leftPad2(utils2.bytesToHex(publicKey), 64);
package/dist/index.mjs CHANGED
@@ -332,7 +332,7 @@ function sm32(input, options) {
332
332
 
333
333
  // src/sm2/ec.ts
334
334
  var DEFAULT_PRNG_POOL_SIZE = 4096;
335
- var prngPool = new Uint8Array(DEFAULT_PRNG_POOL_SIZE);
335
+ var prngPool = new Uint8Array(0);
336
336
  async function FillPRNGPoolIfNeeded() {
337
337
  if ("crypto" in globalThis)
338
338
  return;
@@ -377,7 +377,8 @@ function randomBytes(length = 0) {
377
377
  if ("crypto" in globalThis) {
378
378
  return globalThis.crypto.getRandomValues(array);
379
379
  } else {
380
- return consumePool(length);
380
+ const result = consumePool(length);
381
+ return result;
381
382
  }
382
383
  }
383
384
  function createHash() {
@@ -403,8 +404,8 @@ var sm2Curve = weierstrass({
403
404
 
404
405
  // src/sm2/utils.ts
405
406
  import { mod } from "@noble/curves/abstract/modular";
406
- function generateKeyPairHex() {
407
- const privateKey = sm2Curve.utils.randomPrivateKey();
407
+ function generateKeyPairHex(str) {
408
+ const privateKey = str ? utils2.numberToBytesBE(mod(BigInt(str), ONE) + ONE, 32) : sm2Curve.utils.randomPrivateKey();
408
409
  const publicKey = sm2Curve.getPublicKey(privateKey, false);
409
410
  const privPad = leftPad2(utils2.bytesToHex(privateKey), 64);
410
411
  const pubPad = leftPad2(utils2.bytesToHex(publicKey), 64);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sm-crypto-v2",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -30,8 +30,7 @@
30
30
  "author": "june_01",
31
31
  "license": "MIT",
32
32
  "dependencies": {
33
- "@noble/curves": "^1.1.0",
34
- "@types/jsbn": "^1.2.30"
33
+ "@noble/curves": "^1.1.0"
35
34
  },
36
35
  "devDependencies": {
37
36
  "@swc/core": "^1.3.62",
package/pnpm-lock.yaml CHANGED
@@ -4,9 +4,6 @@ dependencies:
4
4
  '@noble/curves':
5
5
  specifier: ^1.1.0
6
6
  version: 1.1.0
7
- '@types/jsbn':
8
- specifier: ^1.2.30
9
- version: 1.2.30
10
7
 
11
8
  devDependencies:
12
9
  '@swc/core':
@@ -743,10 +740,6 @@ packages:
743
740
  resolution: {integrity: sha512-z/QT1XN4K4KYuslS23k62yDIDLwLFkzxOuMplDtObz0+y7VqJCaO2o+SPwHCvLFZh7xazvvoor2tA/hPz9ee7g==}
744
741
  dev: true
745
742
 
746
- /@types/jsbn@1.2.30:
747
- resolution: {integrity: sha512-VZouplBofjq3YOIHLNRBDxILs/nAArdTZ2QP1ooflyhS+yPExWsFE+i2paBIBb7OI3NJShfcde/nogqk4SPB/Q==}
748
- dev: false
749
-
750
743
  /@types/json-schema@7.0.12:
751
744
  resolution: {integrity: sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==}
752
745
  dev: true
package/src/sm2/ec.ts CHANGED
@@ -20,7 +20,7 @@ declare module wx {
20
20
  }
21
21
 
22
22
  const DEFAULT_PRNG_POOL_SIZE = 4096
23
- let prngPool = new Uint8Array(DEFAULT_PRNG_POOL_SIZE)
23
+ let prngPool = new Uint8Array(0)
24
24
 
25
25
  async function FillPRNGPoolIfNeeded() {
26
26
  if ('crypto' in globalThis) return // no need to use pooling
@@ -67,7 +67,8 @@ export function randomBytes(length = 0): Uint8Array {
67
67
  if ('crypto' in globalThis) {
68
68
  return globalThis.crypto.getRandomValues(array);
69
69
  } else {
70
- return consumePool(length)
70
+ const result = consumePool(length)
71
+ return result
71
72
  }
72
73
  }
73
74
 
package/src/sm2/utils.ts CHANGED
@@ -3,12 +3,13 @@ import * as utils from '@noble/curves/abstract/utils';
3
3
 
4
4
  import { sm2Curve, sm2Fp } from './ec';
5
5
  import { mod } from '@noble/curves/abstract/modular';
6
- import { TWO, ZERO } from './bn';
6
+ import { ONE, TWO, ZERO } from './bn';
7
7
 
8
8
  /**
9
9
  * 生成密钥对:publicKey = privateKey * G
10
10
  */
11
- export function generateKeyPairHex() {
11
+ export function generateKeyPairHex(str?: string) {
12
+ const privateKey = str ? utils.numberToBytesBE((mod(BigInt(str), ONE) + ONE), 32) : sm2Curve.utils.randomPrivateKey()
12
13
  // const random = typeof a === 'string' ? new BigInteger(a, b) :
13
14
  // a ? new BigInteger(a, b!, c!) : new BigInteger(n.bitLength(), rng)
14
15
  // const d = random.mod(n.subtract(BigInteger.ONE)).add(BigInteger.ONE) // 随机数
@@ -18,7 +19,6 @@ export function generateKeyPairHex() {
18
19
  // const Px = leftPad(P.getX().toBigInteger().toString(16), 64)
19
20
  // const Py = leftPad(P.getY().toBigInteger().toString(16), 64)
20
21
  // const publicKey = '04' + Px + Py
21
- const privateKey = sm2Curve.utils.randomPrivateKey();
22
22
  const publicKey = sm2Curve.getPublicKey(privateKey, false);
23
23
  const privPad = leftPad(utils.bytesToHex(privateKey), 64)
24
24
  const pubPad = leftPad(utils.bytesToHex(publicKey), 64)