sm-crypto-v2 1.13.0 → 1.15.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.
@@ -1,3 +1,4 @@
1
+ import * as utils from '@noble/curves/abstract/utils';
1
2
  import { ProjPointType } from '@noble/curves/abstract/weierstrass';
2
3
 
3
4
  interface KeyPair {
@@ -45,6 +46,8 @@ declare function initRNGPool(): Promise<void>;
45
46
 
46
47
  declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array<ArrayBuffer>;
47
48
 
49
+ declare const getSharedSecret: (privateA: utils.PrivKey, publicB: utils.Hex, isCompressed?: boolean) => Uint8Array;
50
+
48
51
  declare const EmptyArray: Uint8Array<ArrayBuffer>;
49
52
  /**
50
53
  * 加密
@@ -136,7 +139,7 @@ declare const index$1_precomputePublicKey: typeof precomputePublicKey;
136
139
  declare const index$1_utf8ToHex: typeof utf8ToHex;
137
140
  declare const index$1_verifyPublicKey: typeof verifyPublicKey;
138
141
  declare namespace index$1 {
139
- export { index$1_EmptyArray as EmptyArray, type index$1_KeyPair as KeyPair, type index$1_SignaturePoint as SignaturePoint, index$1_arrayToHex as arrayToHex, index$1_arrayToUtf8 as arrayToUtf8, index$1_calculateSharedKey as calculateSharedKey, index$1_comparePublicKeyHex as comparePublicKeyHex, index$1_compressPublicKeyHex as compressPublicKeyHex, index$1_doDecrypt as doDecrypt, index$1_doEncrypt as doEncrypt, index$1_doSignature as doSignature, index$1_doVerifySignature as doVerifySignature, index$1_generateKeyPairHex as generateKeyPairHex, index$1_getHash as getHash, index$1_getPoint as getPoint, index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey, index$1_getZ as getZ, index$1_hexToArray as hexToArray, index$1_initRNGPool as initRNGPool, index$1_leftPad as leftPad, index$1_precomputePublicKey as precomputePublicKey, index$1_utf8ToHex as utf8ToHex, index$1_verifyPublicKey as verifyPublicKey };
142
+ export { index$1_EmptyArray as EmptyArray, type index$1_KeyPair as KeyPair, type index$1_SignaturePoint as SignaturePoint, index$1_arrayToHex as arrayToHex, index$1_arrayToUtf8 as arrayToUtf8, index$1_calculateSharedKey as calculateSharedKey, index$1_comparePublicKeyHex as comparePublicKeyHex, index$1_compressPublicKeyHex as compressPublicKeyHex, index$1_doDecrypt as doDecrypt, index$1_doEncrypt as doEncrypt, index$1_doSignature as doSignature, index$1_doVerifySignature as doVerifySignature, getSharedSecret as ecdh, index$1_generateKeyPairHex as generateKeyPairHex, index$1_getHash as getHash, index$1_getPoint as getPoint, index$1_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey, index$1_getZ as getZ, index$1_hexToArray as hexToArray, index$1_initRNGPool as initRNGPool, index$1_leftPad as leftPad, index$1_precomputePublicKey as precomputePublicKey, index$1_utf8ToHex as utf8ToHex, index$1_verifyPublicKey as verifyPublicKey };
140
143
  }
141
144
 
142
145
  /**
@@ -145,7 +148,7 @@ declare namespace index$1 {
145
148
  * @param keylen Desired key length in bytes
146
149
  * @returns Derived key as Uint8Array
147
150
  */
148
- declare function kdf(z: string | Uint8Array, keylen: number): Uint8Array<ArrayBuffer>;
151
+ declare function kdf(z: string | Uint8Array, keylen: number, iv?: string | Uint8Array): Uint8Array<ArrayBuffer>;
149
152
 
150
153
  declare function sm3(input: string | Uint8Array, options?: {
151
154
  key: Uint8Array | string;
@@ -491,6 +491,9 @@ __export(sm2_exports, {
491
491
  doVerifySignature: function() {
492
492
  return doVerifySignature;
493
493
  },
494
+ ecdh: function() {
495
+ return getSharedSecret;
496
+ },
494
497
  generateKeyPairHex: function() {
495
498
  return generateKeyPairHex;
496
499
  },
@@ -2033,7 +2036,7 @@ function weierstrass(curveDef) {
2033
2036
  if (_instanceof(item, Point)) return true;
2034
2037
  return false;
2035
2038
  };
2036
- var getSharedSecret = function getSharedSecret(privateA, publicB) {
2039
+ var getSharedSecret2 = function getSharedSecret2(privateA, publicB) {
2037
2040
  var isCompressed = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : true;
2038
2041
  if (isProbPub(privateA)) throw new Error("first arg must be private key");
2039
2042
  if (!isProbPub(publicB)) throw new Error("second arg must be public key");
@@ -2356,7 +2359,7 @@ function weierstrass(curveDef) {
2356
2359
  return {
2357
2360
  CURVE: CURVE,
2358
2361
  getPublicKey: getPublicKey,
2359
- getSharedSecret: getSharedSecret,
2362
+ getSharedSecret: getSharedSecret2,
2360
2363
  sign: sign,
2361
2364
  verify: verify,
2362
2365
  ProjectivePoint: Point,
@@ -3043,8 +3046,9 @@ function sm32(input, options) {
3043
3046
  return bytesToHex2(sm3(input));
3044
3047
  }
3045
3048
  // src/sm2/kdf.ts
3046
- function kdf(z, keylen) {
3049
+ function kdf(z, keylen, iv) {
3047
3050
  z = typeof z === "string" ? utf8ToArray(z) : z;
3051
+ var IV2 = iv == null ? EmptyArray : typeof iv === "string" ? utf8ToArray(iv) : iv;
3048
3052
  var msg = new Uint8Array(keylen);
3049
3053
  var ct = 1;
3050
3054
  var offset = 0;
@@ -3055,7 +3059,7 @@ function kdf(z, keylen) {
3055
3059
  ctShift[1] = ct >> 16 & 255;
3056
3060
  ctShift[2] = ct >> 8 & 255;
3057
3061
  ctShift[3] = ct & 255;
3058
- t = sm3(concatBytes(z, ctShift));
3062
+ t = sm3(concatBytes(z, ctShift, IV2));
3059
3063
  ct++;
3060
3064
  offset = 0;
3061
3065
  };
@@ -3097,6 +3101,7 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
3097
3101
  return KA;
3098
3102
  }
3099
3103
  // src/sm2/index.ts
3104
+ var getSharedSecret = sm2Curve.getSharedSecret;
3100
3105
  function xorCipherStream(x2, y2, msg) {
3101
3106
  var stream = kdf(concatBytes(x2, y2), msg.length);
3102
3107
  for(var i = 0, len = msg.length; i < len; i++){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sm-crypto-v2",
3
- "version": "1.13.0",
3
+ "version": "1.15.0",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -9,9 +9,10 @@
9
9
  "scripts": {
10
10
  "prepublish": "npm run build",
11
11
  "lint": "eslint \"src/**/*.js\" --fix",
12
- "prerelease": "vitest run && npm run build && npm run build-mp",
12
+ "prerelease": "vitest run && npm run build && npm run build-mp && npm run build-umd",
13
13
  "build": "tsup",
14
14
  "build-mp": "tsup --config=tsup.config.miniprogram.ts",
15
+ "build-umd": "tsup --config=tsup.config.umd.ts && esm2umd SmCryptoV2 dist/index.umd.mjs > dist/index.umd.js && rm -rf dist/index.umd.mjs && rm -rf dist/index.umd.d.mts",
15
16
  "watch": "tsup --watch",
16
17
  "test": "vitest",
17
18
  "release": "npm run prerelease && standard-version && git push --follow-tags origin master",
@@ -55,6 +56,7 @@
55
56
  "eslint": "^8.15.0",
56
57
  "eslint-config-prettier": "^8.3.0",
57
58
  "eslint-plugin-prettier": "^4.0.0",
59
+ "esm2umd": "^0.3.1",
58
60
  "prettier": "^2.6.2",
59
61
  "standard-version": "^9.5.0",
60
62
  "tsup": "^8.0.1",
@@ -0,0 +1,20 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ entry: {
5
+ 'index.umd': './src/index.ts'
6
+ },
7
+ clean: false,
8
+ outDir: 'dist',
9
+ dts: true,
10
+ // we need to keep minify false, since webpack magic comments
11
+ // will be stripped if minify.
12
+ minify: false,
13
+ format: ['esm'],
14
+ target: 'es5',
15
+ noExternal: ['@noble/curves', '@noble/ciphers'],
16
+ tsconfig: 'tsconfig.json',
17
+ esbuildOptions(options) {
18
+ options.define.__BUILD_TS__ = Date.now().toString();
19
+ }
20
+ });