sm-crypto-v2 1.7.0 → 1.9.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/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "sm-crypto-v2",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "sm-crypto-v2",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
+ "miniprogram": "miniprogram_dist",
7
8
  "types": "dist/index.d.ts",
8
9
  "scripts": {
9
10
  "prepublish": "npm run build",
10
11
  "lint": "eslint \"src/**/*.js\" --fix",
11
- "build": "vitest run && tsup",
12
+ "prerelease": "vitest run && npm run build && npm run build-mp",
13
+ "build": "tsup",
14
+ "build-mp": "tsup --config=tsup.config.miniprogram.ts",
12
15
  "watch": "tsup --watch",
13
16
  "test": "vitest",
14
- "release": "npm run build && standard-version && git push --follow-tags origin master",
17
+ "release": "npm run prerelease && standard-version && git push --follow-tags origin master",
15
18
  "coverage": "vitest run --coverage"
16
19
  },
17
20
  "repository": {
@@ -44,12 +47,13 @@
44
47
  "@vitest/runner": "^0.33.0",
45
48
  "@vitest/ui": "^0.31.0",
46
49
  "conventional-changelog-cli": "^2.2.2",
50
+ "esbuild": "^0.19.10",
47
51
  "eslint": "^8.15.0",
48
52
  "eslint-config-prettier": "^8.3.0",
49
53
  "eslint-plugin-prettier": "^4.0.0",
50
54
  "prettier": "^2.6.2",
51
55
  "standard-version": "^9.5.0",
52
- "tsup": "^5.12.7",
56
+ "tsup": "^8.0.1",
53
57
  "typescript": "^4.7.2",
54
58
  "vite": "^4.3.9",
55
59
  "vitest": "^0.31.0"
@@ -3,15 +3,20 @@ import { defineConfig } from 'tsup';
3
3
  export default defineConfig({
4
4
  entry: ['./src/index.ts'],
5
5
  clean: true,
6
- outDir: 'dist',
6
+ outDir: 'miniprogram_dist',
7
7
  dts: true,
8
8
  // we need to keep minify false, since webpack magic comments
9
9
  // will be stripped if minify.
10
10
  minify: false,
11
- format: ['esm', 'cjs'],
12
- target: 'esnext',
11
+ format: ['cjs'],
12
+ target: 'es5',
13
+ noExternal: ['@noble/curves'],
13
14
  tsconfig: 'tsconfig.json',
14
15
  esbuildOptions(options) {
15
- options.define.__BUILD_TS__ = Date.now();
16
+ options.define.__BUILD_TS__ = Date.now().toString();
17
+ options.define.import = 'require';
18
+ options.supported = {
19
+ 'dynamic-import': false,
20
+ }
16
21
  }
17
22
  });
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * as sm2 from './sm2/index'
2
- export { sm3 } from './sm3/index'
3
- export * as sm4 from './sm4/index'
package/src/sm2/asn1.ts DELETED
@@ -1,161 +0,0 @@
1
- /* eslint-disable class-methods-use-this */
2
-
3
- import * as utils from '@noble/curves/abstract/utils';
4
- import { ONE } from './bn';
5
-
6
- export function bigintToValue(bigint: bigint) {
7
- let h = bigint.toString(16)
8
- if (h[0] !== '-') {
9
- // 正数
10
- if (h.length % 2 === 1) h = '0' + h // 补齐到整字节
11
- else if (!h.match(/^[0-7]/)) h = '00' + h // 非0开头,则补一个全0字节
12
- } else {
13
- // 负数
14
- h = h.substring(1)
15
- let len = h.length
16
- if (len % 2 === 1) len += 1 // 补齐到整字节
17
- else if (!h.match(/^[0-7]/)) len += 2 // 非0开头,则补一个全0字节
18
-
19
- let maskString = ''
20
- for (let i = 0; i < len; i++) maskString += 'f'
21
- let mask = utils.hexToNumber(maskString)
22
-
23
- // 对绝对值取反,加1
24
-
25
- let output = (mask ^ bigint) + ONE
26
- h = output.toString(16).replace(/^-/, '')
27
- }
28
- return h
29
- }
30
-
31
- class ASN1Object {
32
- constructor(
33
- public tlv: string | null = null,
34
- public t = '00',
35
- public l = '00',
36
- public v = ''
37
- ) { }
38
-
39
- /**
40
- * 获取 der 编码比特流16进制串
41
- */
42
- getEncodedHex() {
43
- if (!this.tlv) {
44
- this.v = this.getValue()
45
- this.l = this.getLength()
46
- this.tlv = this.t + this.l + this.v
47
- }
48
- return this.tlv
49
- }
50
-
51
- getLength() {
52
- const n = this.v.length / 2 // 字节数
53
- let nHex = n.toString(16)
54
- if (nHex.length % 2 === 1) nHex = '0' + nHex // 补齐到整字节
55
-
56
- if (n < 128) {
57
- // 短格式,以 0 开头
58
- return nHex
59
- } else {
60
- // 长格式,以 1 开头
61
- const head = 128 + nHex.length / 2 // 1(1位) + 真正的长度占用字节数(7位) + 真正的长度
62
- return head.toString(16) + nHex
63
- }
64
- }
65
-
66
- getValue() {
67
- return ''
68
- }
69
- }
70
-
71
- class DERInteger extends ASN1Object {
72
- constructor(bigint: bigint) {
73
- super()
74
-
75
- this.t = '02' // 整型标签说明
76
- if (bigint) this.v = bigintToValue(bigint)
77
- }
78
-
79
- getValue() {
80
- return this.v
81
- }
82
- }
83
-
84
- class DERSequence extends ASN1Object {
85
- public t = '30'
86
- constructor(public asn1Array: ASN1Object[]) {
87
- super()
88
- }
89
-
90
- getValue() {
91
- this.v = this.asn1Array.map(asn1Object => asn1Object.getEncodedHex()).join('')
92
- return this.v
93
- }
94
- }
95
-
96
- /**
97
- * 获取 l 占用字节数
98
- */
99
- function getLenOfL(str: string, start: number) {
100
- if (+str[start + 2] < 8) return 1 // l 以0开头,则表示短格式,只占一个字节
101
- return +str.substring(start + 2, start + 4) & 0x7f + 1 // 长格式,取第一个字节后7位作为长度真正占用字节数,再加上本身
102
- }
103
-
104
- /**
105
- * 获取 l
106
- */
107
- function getL(str: string, start: number) {
108
- // 获取 l
109
- const len = getLenOfL(str, start)
110
- const l = str.substring(start + 2, start + 2 + len * 2)
111
-
112
- if (!l) return -1
113
- const bigint = +l[0] < 8 ? utils.hexToNumber(l): utils.hexToNumber(l.substring(2))
114
-
115
- return +bigint.toString()
116
- }
117
-
118
- /**
119
- * 获取 v 的位置
120
- */
121
- function getStartOfV(str: string, start: number) {
122
- const len = getLenOfL(str, start)
123
- return start + (len + 1) * 2
124
- }
125
-
126
- /**
127
- * ASN.1 der 编码,针对 sm2 签名
128
- */
129
- export function encodeDer(r: bigint, s: bigint) {
130
- const derR = new DERInteger(r)
131
- const derS = new DERInteger(s)
132
- const derSeq = new DERSequence([derR, derS])
133
-
134
- return derSeq.getEncodedHex()
135
- }
136
-
137
- /**
138
- * 解析 ASN.1 der,针对 sm2 验签
139
- */
140
- export function decodeDer(input: string) {
141
- // 结构:
142
- // input = | tSeq | lSeq | vSeq |
143
- // vSeq = | tR | lR | vR | tS | lS | vS |
144
- const start = getStartOfV(input, 0)
145
-
146
- const vIndexR = getStartOfV(input, start)
147
- const lR = getL(input, start)
148
- const vR = input.substr(vIndexR, lR * 2)
149
-
150
- const nextStart = vIndexR + vR.length
151
- const vIndexS = getStartOfV(input, nextStart)
152
- const lS = getL(input, nextStart)
153
- const vS = input.substring(vIndexS, vIndexS + lS * 2)
154
-
155
- // const r = new BigInteger(vR, 16)
156
- // const s = new BigInteger(vS, 16)
157
- const r = utils.hexToNumber(vR)
158
- const s = utils.hexToNumber(vS)
159
-
160
- return { r, s }
161
- }
package/src/sm2/bn.ts DELETED
@@ -1,4 +0,0 @@
1
- export const ZERO = BigInt(0);
2
- export const ONE = BigInt(1);
3
- export const TWO = BigInt(2);
4
- export const THREE = BigInt(3);
package/src/sm2/ec.ts DELETED
@@ -1,24 +0,0 @@
1
- import { weierstrass } from '@noble/curves/abstract/weierstrass';
2
- import { Field } from '@noble/curves/abstract/modular'; // finite field for mod arithmetics
3
- import { ONE } from './bn';
4
- import { randomBytes } from './rng';
5
- import { sm3 } from './sm3';
6
- import { hmac } from './hmac';
7
- import { concatBytes } from '@noble/curves/abstract/utils';
8
-
9
- export const sm2Fp = Field(BigInt('115792089210356248756420345214020892766250353991924191454421193933289684991999'))
10
- export const sm2Curve = weierstrass({
11
- // sm2: short weierstrass.
12
- a: BigInt('115792089210356248756420345214020892766250353991924191454421193933289684991996'),
13
- b: BigInt('18505919022281880113072981827955639221458448578012075254857346196103069175443'),
14
- Fp: sm2Fp,
15
- h: ONE,
16
- n: BigInt('115792089210356248756420345214020892766061623724957744567843809356293439045923'),
17
- Gx: BigInt('22963146547237050559479531362550074578802567295341616970375194840604139615431'),
18
- Gy: BigInt('85132369209828568825618990617112496413088388631904505083283536607588877201568'),
19
- hash: sm3,
20
- hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(sm3, key, concatBytes(...msgs)),
21
- randomBytes,
22
- });
23
- // 有限域运算
24
- export const field = Field(BigInt(sm2Curve.CURVE.n))
package/src/sm2/hmac.ts DELETED
@@ -1,76 +0,0 @@
1
- import { Hash, CHash, Input, toBytes } from '../sm3/utils';
2
- // HMAC (RFC 2104)
3
- export class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> {
4
- oHash: T;
5
- iHash: T;
6
- blockLen: number;
7
- outputLen: number;
8
- private finished = false;
9
- private destroyed = false;
10
-
11
- constructor(hash: CHash, _key: Input) {
12
- super();
13
- const key = toBytes(_key);
14
- this.iHash = hash.create() as T;
15
- if (typeof this.iHash.update !== 'function')
16
- throw new Error('Expected instance of class which extends utils.Hash');
17
- this.blockLen = this.iHash.blockLen;
18
- this.outputLen = this.iHash.outputLen;
19
- const blockLen = this.blockLen;
20
- const pad = new Uint8Array(blockLen);
21
- // blockLen can be bigger than outputLen
22
- pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
23
- for (let i = 0; i < pad.length; i++) pad[i] ^= 0x36;
24
- this.iHash.update(pad);
25
- // By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
26
- this.oHash = hash.create() as T;
27
- // Undo internal XOR && apply outer XOR
28
- for (let i = 0; i < pad.length; i++) pad[i] ^= 0x36 ^ 0x5c;
29
- this.oHash.update(pad);
30
- pad.fill(0);
31
- }
32
- update(buf: Input) {
33
- this.iHash.update(buf);
34
- return this;
35
- }
36
- digestInto(out: Uint8Array) {
37
- this.finished = true;
38
- this.iHash.digestInto(out);
39
- this.oHash.update(out);
40
- this.oHash.digestInto(out);
41
- this.destroy();
42
- }
43
- digest() {
44
- const out = new Uint8Array(this.oHash.outputLen);
45
- this.digestInto(out);
46
- return out;
47
- }
48
- _cloneInto(to?: HMAC<T>): HMAC<T> {
49
- // Create new instance without calling constructor since key already in state and we don't know it.
50
- to ||= Object.create(Object.getPrototypeOf(this), {});
51
- const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
52
- to = to as this;
53
- to.finished = finished;
54
- to.destroyed = destroyed;
55
- to.blockLen = blockLen;
56
- to.outputLen = outputLen;
57
- to.oHash = oHash._cloneInto(to.oHash);
58
- to.iHash = iHash._cloneInto(to.iHash);
59
- return to;
60
- }
61
- destroy() {
62
- this.destroyed = true;
63
- this.oHash.destroy();
64
- this.iHash.destroy();
65
- }
66
- }
67
-
68
- /**
69
- * HMAC: RFC2104 message authentication code.
70
- * @param hash - function that would be used e.g. sha256
71
- * @param key - message key
72
- * @param message - message data
73
- */
74
- export const hmac = (hash: CHash, key: Input, message: Input): Uint8Array =>
75
- new HMAC<any>(hash, key).update(message).digest();
76
- hmac.create = (hash: CHash, key: Input) => new HMAC<any>(hash, key);
package/src/sm2/index.ts DELETED
@@ -1,291 +0,0 @@
1
- /* eslint-disable no-use-before-define */
2
- import { encodeDer, decodeDer } from './asn1'
3
- import { arrayToHex, arrayToUtf8, generateKeyPairHex, hexToArray, leftPad, utf8ToHex } from './utils'
4
- import { sm3 } from './sm3'
5
- import * as utils from '@noble/curves/abstract/utils';
6
- import { field, sm2Curve } from './ec';
7
- import { ONE, ZERO } from './bn';
8
- import { bytesToHex } from '@/sm3/utils';
9
- import { ProjPointType } from '@noble/curves/abstract/weierstrass';
10
-
11
- export * from './utils'
12
- export { initRNGPool } from './rng'
13
- export { calculateSharedKey } from './kx'
14
-
15
- const C1C2C3 = 0
16
- // a empty array, just make tsc happy
17
- export const EmptyArray = new Uint8Array()
18
- /**
19
- * 加密
20
- */
21
- export function doEncrypt(msg: string | Uint8Array, publicKey: string | ProjPointType<bigint>, cipherMode = 1) {
22
-
23
- const msgArr = typeof msg === 'string' ? hexToArray(utf8ToHex(msg)) : Uint8Array.from(msg)
24
- const publicKeyPoint = typeof publicKey === 'string' ? sm2Curve.ProjectivePoint.fromHex(publicKey) :
25
- publicKey
26
-
27
- const keypair = generateKeyPairHex()
28
- const k = utils.hexToNumber(keypair.privateKey)
29
-
30
- // c1 = k * G
31
- let c1 = keypair.publicKey
32
- if (c1.length > 128) c1 = c1.substring(c1.length - 128)
33
- const p = publicKeyPoint.multiply(k)
34
-
35
- // (x2, y2) = k * publicKey
36
- const x2 = hexToArray(leftPad(utils.numberToHexUnpadded(p.x), 64))
37
- const y2 = hexToArray(leftPad(utils.numberToHexUnpadded(p.y), 64))
38
-
39
- // c3 = hash(x2 || msg || y2)
40
- const c3 = bytesToHex(sm3(utils.concatBytes(x2, msgArr, y2)));
41
-
42
- xorCipherStream(x2, y2, msgArr)
43
- const c2 = bytesToHex(msgArr)
44
-
45
- return cipherMode === C1C2C3 ? c1 + c2 + c3 : c1 + c3 + c2
46
- }
47
-
48
- function xorCipherStream(x2: Uint8Array, y2: Uint8Array, msg: Uint8Array) {
49
- let ct = 1
50
- let offset = 0
51
- let t = EmptyArray
52
- const ctShift = new Uint8Array(4)
53
- const nextT = () => {
54
- // (1) Hai = hash(z || ct)
55
- // (2) ct++
56
- ctShift[0] = ct >> 24 & 0x00ff
57
- ctShift[1] = ct >> 16 & 0x00ff
58
- ctShift[2] = ct >> 8 & 0x00ff
59
- ctShift[3] = ct & 0x00ff
60
- t = sm3(utils.concatBytes(x2, y2, ctShift))
61
- ct++
62
- offset = 0
63
- }
64
- nextT() // 先生成 Ha1
65
-
66
- for (let i = 0, len = msg.length; i < len; i++) {
67
- // t = Ha1 || Ha2 || Ha3 || Ha4
68
- if (offset === t.length) nextT()
69
-
70
- // c2 = msg ^ t
71
- msg[i] ^= t[offset++] & 0xff
72
- }
73
-
74
- }
75
-
76
- /**
77
- * 解密
78
- */
79
- export function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
80
- output: 'array'
81
- }): Uint8Array
82
- export function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
83
- output: 'string'
84
- }): string
85
- export function doDecrypt(encryptData: string, privateKey: string, cipherMode = 1, {
86
- output = 'string',
87
- } = {}) {
88
- const privateKeyInteger = utils.hexToNumber(privateKey)
89
-
90
- let c3 = encryptData.substring(128, 128 + 64)
91
- let c2 = encryptData.substring(128 + 64)
92
-
93
- if (cipherMode === C1C2C3) {
94
- c3 = encryptData.substring(encryptData.length - 64)
95
- c2 = encryptData.substring(128, encryptData.length - 64)
96
- }
97
-
98
- const msg = hexToArray(c2)
99
- const c1 = sm2Curve.ProjectivePoint.fromHex('04' + encryptData.substring(0, 128))!
100
-
101
- const p = c1.multiply(privateKeyInteger)
102
- const x2 = hexToArray(leftPad(utils.numberToHexUnpadded(p.x), 64))
103
- const y2 = hexToArray(leftPad(utils.numberToHexUnpadded(p.y), 64))
104
-
105
- xorCipherStream(x2, y2, msg)
106
- // c3 = hash(x2 || msg || y2)
107
- const checkC3 = arrayToHex(Array.from(sm3(utils.concatBytes(x2, msg, y2))))
108
-
109
- if (checkC3 === c3.toLowerCase()) {
110
- return output === 'array' ? msg : arrayToUtf8(msg)
111
- } else {
112
- return output === 'array' ? [] : ''
113
- }
114
- }
115
-
116
- export interface SignaturePoint {
117
- k: bigint
118
- x1: bigint
119
- }
120
-
121
- /**
122
- * 签名
123
- */
124
- export function doSignature(msg: Uint8Array | string, privateKey: string, options: {
125
- pointPool?: SignaturePoint[], der?: boolean, hash?: boolean, publicKey?: string, userId?: string
126
- } = {}) {
127
- let {
128
- pointPool, der, hash, publicKey, userId
129
- } = options
130
- let hashHex = typeof msg === 'string' ? utf8ToHex(msg) : arrayToHex(Array.from(msg))
131
-
132
- if (hash) {
133
- // sm3杂凑
134
- publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey)
135
- hashHex = getHash(hashHex, publicKey, userId)
136
- }
137
-
138
- const dA = utils.hexToNumber(privateKey)
139
- const e = utils.hexToNumber(hashHex)
140
-
141
- // k
142
- let k: bigint | null = null
143
- let r: bigint | null = null
144
- let s: bigint | null = null
145
-
146
- do {
147
- do {
148
- let point: SignaturePoint
149
- if (pointPool && pointPool.length) {
150
- point = pointPool.pop()!
151
- } else {
152
- point = getPoint()
153
- }
154
- k = point.k
155
-
156
- // r = (e + x1) mod n
157
- r = field.add(e, point.x1)
158
- } while (r === ZERO || (r + k) === sm2Curve.CURVE.n)
159
-
160
- // s = ((1 + dA)^-1 * (k - r * dA)) mod n
161
- s = field.mul(field.inv(field.addN(dA, ONE)), field.subN(k, field.mulN(r, dA)))
162
- } while (s === ZERO)
163
- if (der) return encodeDer(r, s) // asn.1 der 编码
164
- return leftPad(utils.numberToHexUnpadded(r), 64) + leftPad(utils.numberToHexUnpadded(s), 64)
165
- }
166
-
167
- /**
168
- * 验签
169
- */
170
- export function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string | ProjPointType<bigint>, options: { der?: boolean, hash?: boolean, userId?: string } = {}) {
171
- let hashHex: string
172
- const {
173
- hash,
174
- der,
175
- userId,
176
- } = options
177
- const publicKeyHex = typeof publicKey === 'string' ? publicKey : publicKey.toHex(false)
178
- if (hash) {
179
- // sm3杂凑
180
- hashHex = getHash(typeof msg === 'string' ? utf8ToHex(msg) : msg, publicKeyHex, userId)
181
- } else {
182
- hashHex = typeof msg === 'string' ? utf8ToHex(msg) : arrayToHex(Array.from(msg))
183
- }
184
-
185
- let r: bigint;
186
- let s: bigint;
187
- if (der) {
188
- const decodeDerObj = decodeDer(signHex) // asn.1 der 解码
189
- r = decodeDerObj.r
190
- s = decodeDerObj.s
191
- } else {
192
- r = utils.hexToNumber(signHex.substring(0, 64))
193
- s = utils.hexToNumber(signHex.substring(64))
194
- }
195
-
196
- const PA = typeof publicKey === 'string' ? sm2Curve.ProjectivePoint.fromHex(publicKey) : publicKey
197
- const e = utils.hexToNumber(hashHex)
198
-
199
- // t = (r + s) mod n
200
- const t = field.add(r, s)
201
-
202
- if (t === ZERO) return false
203
-
204
- // x1y1 = s * G + t * PA
205
- const x1y1 = sm2Curve.ProjectivePoint.BASE.multiply(s).add(PA.multiply(t))
206
-
207
- // R = (e + x1) mod n
208
- // const R = e.add(x1y1.getX().toBigInteger()).mod(n)
209
- const R = field.add(e, x1y1.x)
210
-
211
- // return r.equals(R)
212
- return r === R
213
- }
214
-
215
- export function getZ(publicKey: string, userId = '1234567812345678') {
216
- // z = hash(entl || userId || a || b || gx || gy || px || py)
217
- userId = utf8ToHex(userId)
218
- const a = leftPad(utils.numberToHexUnpadded(sm2Curve.CURVE.a), 64)
219
- // const b = leftPad(G.curve.b.toBigInteger().toRadix(16), 64)
220
- const b = leftPad(utils.numberToHexUnpadded(sm2Curve.CURVE.b), 64)
221
- // const gx = leftPad(G.getX().toBigInteger().toRadix(16), 64)
222
- const gx = leftPad(utils.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.x), 64)
223
- // const gy = leftPad(G.getY().toBigInteger().toRadix(16), 64)
224
- const gy = leftPad(utils.numberToHexUnpadded(sm2Curve.ProjectivePoint.BASE.y), 64)
225
- let px: string
226
- let py: string
227
- if (publicKey.length === 128) {
228
- px = publicKey.substring(0, 64)
229
- py = publicKey.substring(64, 128)
230
- } else {
231
- // const point = G.curve.decodePointHex(publicKey)!
232
- const point = sm2Curve.ProjectivePoint.fromHex(publicKey)!
233
- // px = leftPad(point.getX().toBigInteger().toRadix(16), 64)
234
- px = leftPad(utils.numberToHexUnpadded(point.x), 64)
235
- // py = leftPad(point.getY().toBigInteger().toRadix(16), 64)
236
- py = leftPad(utils.numberToHexUnpadded(point.y), 64)
237
- }
238
- const data = hexToArray(userId + a + b + gx + gy + px + py)
239
-
240
- const entl = userId.length * 4
241
-
242
- const z = sm3(utils.concatBytes(new Uint8Array([entl >> 8 & 0x00ff, entl & 0x00ff]), data))
243
-
244
- return z
245
- }
246
-
247
- /**
248
- * sm3杂凑算法
249
- */
250
- export function getHash(hashHex: string | Uint8Array, publicKey: string, userId = '1234567812345678') {
251
- const z = getZ(publicKey, userId)
252
- // e = hash(z || msg)
253
- return bytesToHex(sm3(utils.concatBytes(z, typeof hashHex === 'string' ? hexToArray(hashHex) : hashHex)))
254
- }
255
-
256
- /**
257
- * 预计算公钥点,可用于提升加密性能
258
- * @export
259
- * @param {string} publicKey 公钥
260
- * @param windowSize 计算窗口大小,默认为 8
261
- * @returns {ProjPointType<bigint>} 预计算的点
262
- */
263
- export function precomputePublicKey(publicKey: string, windowSize?: number) {
264
- const point = sm2Curve.ProjectivePoint.fromHex(publicKey)
265
- return sm2Curve.utils.precompute(windowSize, point)
266
- }
267
-
268
- /**
269
- * 计算公钥
270
- */
271
- export function getPublicKeyFromPrivateKey(privateKey: string) {
272
- const pubKey = sm2Curve.getPublicKey(privateKey, false)
273
- const pubPad = leftPad(utils.bytesToHex(pubKey), 64)
274
- return pubPad
275
- }
276
-
277
- /**
278
- * 获取椭圆曲线点
279
- */
280
- export function getPoint() {
281
- const keypair = generateKeyPairHex()
282
- const PA = sm2Curve.ProjectivePoint.fromHex(keypair.publicKey)
283
- const k = utils.hexToNumber(keypair.privateKey)
284
-
285
- return {
286
- ...keypair,
287
- k,
288
- x1: PA!.x,
289
- }
290
- }
291
-