sm-crypto-v2 1.12.0 → 1.14.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
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.14.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.13.0...v1.14.0) (2025-09-05)
6
+
7
+
8
+ ### Features
9
+
10
+ * **sm2:** expose ecdh interface and test cases ([68af176](https://github.com/Cubelrti/sm-crypto-v2/commit/68af176fd4013ea0e3165ad31c21f9cae590fcf7))
11
+
12
+ ## [1.13.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.12.0...v1.13.0) (2025-07-15)
13
+
14
+
15
+ ### Features
16
+
17
+ * downgrade to es2020 target ([3058285](https://github.com/Cubelrti/sm-crypto-v2/commit/305828570dc15fd686d04f3d85c37b978e8893c8))
18
+
5
19
  ## [1.12.0](https://github.com/Cubelrti/sm-crypto-v2/compare/v1.11.0...v1.12.0) (2025-06-05)
6
20
 
7
21
 
package/dist/index.d.mts CHANGED
@@ -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;
package/dist/index.d.ts CHANGED
@@ -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;
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
9
  var __export = (target, all) => {
9
10
  for (var name in all)
10
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -26,6 +27,10 @@ var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__
26
27
  mod2
27
28
  ));
28
29
  var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
30
+ var __publicField = (obj, key, value) => {
31
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
32
+ return value;
33
+ };
29
34
 
30
35
  // src/index.ts
31
36
  var src_exports = {};
@@ -50,6 +55,7 @@ __export(sm2_exports, {
50
55
  doEncrypt: () => doEncrypt,
51
56
  doSignature: () => doSignature,
52
57
  doVerifySignature: () => doVerifySignature,
58
+ ecdh: () => getSharedSecret,
53
59
  generateKeyPairHex: () => generateKeyPairHex,
54
60
  getHash: () => getHash,
55
61
  getPoint: () => getPoint,
@@ -145,11 +151,11 @@ var DEROctetString = class extends ASN1Object {
145
151
  constructor(s) {
146
152
  super();
147
153
  this.s = s;
154
+ __publicField(this, "hV", "");
148
155
  this.t = "04";
149
156
  if (s)
150
157
  this.v = s.toLowerCase();
151
158
  }
152
- hV = "";
153
159
  getValue() {
154
160
  return this.v;
155
161
  }
@@ -158,8 +164,8 @@ var DERSequence = class extends ASN1Object {
158
164
  constructor(asn1Array) {
159
165
  super();
160
166
  this.asn1Array = asn1Array;
167
+ __publicField(this, "t", "30");
161
168
  }
162
- t = "30";
163
169
  getValue() {
164
170
  this.v = this.asn1Array.map((asn1Object) => asn1Object.getEncodedHex()).join("");
165
171
  return this.v;
@@ -407,16 +413,16 @@ var SHA2 = class extends Hash {
407
413
  this.outputLen = outputLen;
408
414
  this.padOffset = padOffset;
409
415
  this.isLE = isLE2;
416
+ // For partial updates less than block size
417
+ __publicField(this, "buffer");
418
+ __publicField(this, "view");
419
+ __publicField(this, "finished", false);
420
+ __publicField(this, "length", 0);
421
+ __publicField(this, "pos", 0);
422
+ __publicField(this, "destroyed", false);
410
423
  this.buffer = new Uint8Array(blockLen);
411
424
  this.view = createView(this.buffer);
412
425
  }
413
- // For partial updates less than block size
414
- buffer;
415
- view;
416
- finished = false;
417
- length = 0;
418
- pos = 0;
419
- destroyed = false;
420
426
  update(data) {
421
427
  const { view, buffer, blockLen } = this;
422
428
  data = toBytes(data);
@@ -474,7 +480,7 @@ var SHA2 = class extends Hash {
474
480
  return res;
475
481
  }
476
482
  _cloneInto(to) {
477
- to ||= new this.constructor();
483
+ to || (to = new this.constructor());
478
484
  to.set(...this.get());
479
485
  const { blockLen, buffer, length, finished, destroyed, pos } = this;
480
486
  to.length = length;
@@ -492,18 +498,18 @@ var SM3_M = new Uint32Array(64);
492
498
  var T1 = 2043430169;
493
499
  var T2 = 2055708042;
494
500
  var SM3 = class extends SHA2 {
495
- // We cannot use array here since array allows indexing by variable
496
- // which means optimizer/compiler cannot use registers.
497
- A = IV[0] | 0;
498
- B = IV[1] | 0;
499
- C = IV[2] | 0;
500
- D = IV[3] | 0;
501
- E = IV[4] | 0;
502
- F = IV[5] | 0;
503
- G = IV[6] | 0;
504
- H = IV[7] | 0;
505
501
  constructor() {
506
502
  super(64, 32, 8, false);
503
+ // We cannot use array here since array allows indexing by variable
504
+ // which means optimizer/compiler cannot use registers.
505
+ __publicField(this, "A", IV[0] | 0);
506
+ __publicField(this, "B", IV[1] | 0);
507
+ __publicField(this, "C", IV[2] | 0);
508
+ __publicField(this, "D", IV[3] | 0);
509
+ __publicField(this, "E", IV[4] | 0);
510
+ __publicField(this, "F", IV[5] | 0);
511
+ __publicField(this, "G", IV[6] | 0);
512
+ __publicField(this, "H", IV[7] | 0);
507
513
  }
508
514
  get() {
509
515
  const { A, B, C, D, E, F, G, H } = this;
@@ -568,14 +574,14 @@ var sm3 = wrapConstructor(() => new SM3());
568
574
 
569
575
  // src/sm2/hmac.ts
570
576
  var HMAC = class extends Hash {
571
- oHash;
572
- iHash;
573
- blockLen;
574
- outputLen;
575
- finished = false;
576
- destroyed = false;
577
577
  constructor(hash, _key) {
578
578
  super();
579
+ __publicField(this, "oHash");
580
+ __publicField(this, "iHash");
581
+ __publicField(this, "blockLen");
582
+ __publicField(this, "outputLen");
583
+ __publicField(this, "finished", false);
584
+ __publicField(this, "destroyed", false);
579
585
  const key = toBytes(_key);
580
586
  this.iHash = hash.create();
581
587
  if (typeof this.iHash.update !== "function")
@@ -611,7 +617,7 @@ var HMAC = class extends Hash {
611
617
  return out;
612
618
  }
613
619
  _cloneInto(to) {
614
- to ||= Object.create(Object.getPrototypeOf(this), {});
620
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
615
621
  const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
616
622
  to = to;
617
623
  to.finished = finished;
@@ -785,8 +791,9 @@ function sm32(input, options) {
785
791
  }
786
792
 
787
793
  // src/sm2/kdf.ts
788
- function kdf(z, keylen) {
794
+ function kdf(z, keylen, iv) {
789
795
  z = typeof z === "string" ? utf8ToArray(z) : z;
796
+ const IV2 = iv == null ? EmptyArray : typeof iv === "string" ? utf8ToArray(iv) : iv;
790
797
  let msg = new Uint8Array(keylen);
791
798
  let ct = 1;
792
799
  let offset = 0;
@@ -797,7 +804,7 @@ function kdf(z, keylen) {
797
804
  ctShift[1] = ct >> 16 & 255;
798
805
  ctShift[2] = ct >> 8 & 255;
799
806
  ctShift[3] = ct & 255;
800
- t = sm3(utils3.concatBytes(z, ctShift));
807
+ t = sm3(utils3.concatBytes(z, ctShift, IV2));
801
808
  ct++;
802
809
  offset = 0;
803
810
  };
@@ -838,6 +845,7 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
838
845
  }
839
846
 
840
847
  // src/sm2/index.ts
848
+ var { getSharedSecret } = sm2Curve;
841
849
  function xorCipherStream(x2, y2, msg) {
842
850
  const stream = kdf(utils5.concatBytes(x2, y2), msg.length);
843
851
  for (let i = 0, len = msg.length; i < len; i++) {
package/dist/index.mjs CHANGED
@@ -1,8 +1,13 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
2
3
  var __export = (target, all) => {
3
4
  for (var name in all)
4
5
  __defProp(target, name, { get: all[name], enumerable: true });
5
6
  };
7
+ var __publicField = (obj, key, value) => {
8
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
9
+ return value;
10
+ };
6
11
 
7
12
  // src/sm2/index.ts
8
13
  var sm2_exports = {};
@@ -17,6 +22,7 @@ __export(sm2_exports, {
17
22
  doEncrypt: () => doEncrypt,
18
23
  doSignature: () => doSignature,
19
24
  doVerifySignature: () => doVerifySignature,
25
+ ecdh: () => getSharedSecret,
20
26
  generateKeyPairHex: () => generateKeyPairHex,
21
27
  getHash: () => getHash,
22
28
  getPoint: () => getPoint,
@@ -112,11 +118,11 @@ var DEROctetString = class extends ASN1Object {
112
118
  constructor(s) {
113
119
  super();
114
120
  this.s = s;
121
+ __publicField(this, "hV", "");
115
122
  this.t = "04";
116
123
  if (s)
117
124
  this.v = s.toLowerCase();
118
125
  }
119
- hV = "";
120
126
  getValue() {
121
127
  return this.v;
122
128
  }
@@ -125,8 +131,8 @@ var DERSequence = class extends ASN1Object {
125
131
  constructor(asn1Array) {
126
132
  super();
127
133
  this.asn1Array = asn1Array;
134
+ __publicField(this, "t", "30");
128
135
  }
129
- t = "30";
130
136
  getValue() {
131
137
  this.v = this.asn1Array.map((asn1Object) => asn1Object.getEncodedHex()).join("");
132
138
  return this.v;
@@ -374,16 +380,16 @@ var SHA2 = class extends Hash {
374
380
  this.outputLen = outputLen;
375
381
  this.padOffset = padOffset;
376
382
  this.isLE = isLE2;
383
+ // For partial updates less than block size
384
+ __publicField(this, "buffer");
385
+ __publicField(this, "view");
386
+ __publicField(this, "finished", false);
387
+ __publicField(this, "length", 0);
388
+ __publicField(this, "pos", 0);
389
+ __publicField(this, "destroyed", false);
377
390
  this.buffer = new Uint8Array(blockLen);
378
391
  this.view = createView(this.buffer);
379
392
  }
380
- // For partial updates less than block size
381
- buffer;
382
- view;
383
- finished = false;
384
- length = 0;
385
- pos = 0;
386
- destroyed = false;
387
393
  update(data) {
388
394
  const { view, buffer, blockLen } = this;
389
395
  data = toBytes(data);
@@ -441,7 +447,7 @@ var SHA2 = class extends Hash {
441
447
  return res;
442
448
  }
443
449
  _cloneInto(to) {
444
- to ||= new this.constructor();
450
+ to || (to = new this.constructor());
445
451
  to.set(...this.get());
446
452
  const { blockLen, buffer, length, finished, destroyed, pos } = this;
447
453
  to.length = length;
@@ -459,18 +465,18 @@ var SM3_M = new Uint32Array(64);
459
465
  var T1 = 2043430169;
460
466
  var T2 = 2055708042;
461
467
  var SM3 = class extends SHA2 {
462
- // We cannot use array here since array allows indexing by variable
463
- // which means optimizer/compiler cannot use registers.
464
- A = IV[0] | 0;
465
- B = IV[1] | 0;
466
- C = IV[2] | 0;
467
- D = IV[3] | 0;
468
- E = IV[4] | 0;
469
- F = IV[5] | 0;
470
- G = IV[6] | 0;
471
- H = IV[7] | 0;
472
468
  constructor() {
473
469
  super(64, 32, 8, false);
470
+ // We cannot use array here since array allows indexing by variable
471
+ // which means optimizer/compiler cannot use registers.
472
+ __publicField(this, "A", IV[0] | 0);
473
+ __publicField(this, "B", IV[1] | 0);
474
+ __publicField(this, "C", IV[2] | 0);
475
+ __publicField(this, "D", IV[3] | 0);
476
+ __publicField(this, "E", IV[4] | 0);
477
+ __publicField(this, "F", IV[5] | 0);
478
+ __publicField(this, "G", IV[6] | 0);
479
+ __publicField(this, "H", IV[7] | 0);
474
480
  }
475
481
  get() {
476
482
  const { A, B, C, D, E, F, G, H } = this;
@@ -535,14 +541,14 @@ var sm3 = wrapConstructor(() => new SM3());
535
541
 
536
542
  // src/sm2/hmac.ts
537
543
  var HMAC = class extends Hash {
538
- oHash;
539
- iHash;
540
- blockLen;
541
- outputLen;
542
- finished = false;
543
- destroyed = false;
544
544
  constructor(hash, _key) {
545
545
  super();
546
+ __publicField(this, "oHash");
547
+ __publicField(this, "iHash");
548
+ __publicField(this, "blockLen");
549
+ __publicField(this, "outputLen");
550
+ __publicField(this, "finished", false);
551
+ __publicField(this, "destroyed", false);
546
552
  const key = toBytes(_key);
547
553
  this.iHash = hash.create();
548
554
  if (typeof this.iHash.update !== "function")
@@ -578,7 +584,7 @@ var HMAC = class extends Hash {
578
584
  return out;
579
585
  }
580
586
  _cloneInto(to) {
581
- to ||= Object.create(Object.getPrototypeOf(this), {});
587
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
582
588
  const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
583
589
  to = to;
584
590
  to.finished = finished;
@@ -752,8 +758,9 @@ function sm32(input, options) {
752
758
  }
753
759
 
754
760
  // src/sm2/kdf.ts
755
- function kdf(z, keylen) {
761
+ function kdf(z, keylen, iv) {
756
762
  z = typeof z === "string" ? utf8ToArray(z) : z;
763
+ const IV2 = iv == null ? EmptyArray : typeof iv === "string" ? utf8ToArray(iv) : iv;
757
764
  let msg = new Uint8Array(keylen);
758
765
  let ct = 1;
759
766
  let offset = 0;
@@ -764,7 +771,7 @@ function kdf(z, keylen) {
764
771
  ctShift[1] = ct >> 16 & 255;
765
772
  ctShift[2] = ct >> 8 & 255;
766
773
  ctShift[3] = ct & 255;
767
- t = sm3(utils3.concatBytes(z, ctShift));
774
+ t = sm3(utils3.concatBytes(z, ctShift, IV2));
768
775
  ct++;
769
776
  offset = 0;
770
777
  };
@@ -805,6 +812,7 @@ function calculateSharedKey(keypairA, ephemeralKeypairA, publicKeyB, ephemeralPu
805
812
  }
806
813
 
807
814
  // src/sm2/index.ts
815
+ var { getSharedSecret } = sm2Curve;
808
816
  function xorCipherStream(x2, y2, msg) {
809
817
  const stream = kdf(utils5.concatBytes(x2, y2), msg.length);
810
818
  for (let i = 0, len = msg.length; i < len; i++) {
@@ -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.12.0",
3
+ "version": "1.14.0",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",