sm-crypto-v2 0.3.12

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.
@@ -0,0 +1,409 @@
1
+ import { BigInteger, RandomGenerator } from 'jsbn';
2
+
3
+ /**
4
+ * 椭圆曲线域元素
5
+ */
6
+ declare class ECFieldElementFp {
7
+ q: BigInteger;
8
+ x: BigInteger;
9
+ constructor(q: BigInteger, x: BigInteger);
10
+ /**
11
+ * 判断相等
12
+ */
13
+ equals(other: ECFieldElementFp): boolean;
14
+ /**
15
+ * 返回具体数值
16
+ */
17
+ toBigInteger(): BigInteger;
18
+ /**
19
+ * 取反
20
+ */
21
+ negate(): ECFieldElementFp;
22
+ /**
23
+ * 相加
24
+ */
25
+ add(b: any): ECFieldElementFp;
26
+ /**
27
+ * 相减
28
+ */
29
+ subtract(b: any): ECFieldElementFp;
30
+ /**
31
+ * 相乘
32
+ */
33
+ multiply(b: any): ECFieldElementFp;
34
+ /**
35
+ * 相除
36
+ */
37
+ divide(b: any): ECFieldElementFp;
38
+ /**
39
+ * 平方
40
+ */
41
+ square(): ECFieldElementFp;
42
+ }
43
+ declare class ECPointFp {
44
+ curve: ECCurveFp;
45
+ x: ECFieldElementFp | null;
46
+ y: ECFieldElementFp | null;
47
+ zinv: BigInteger | null;
48
+ z: BigInteger;
49
+ constructor(curve: ECCurveFp, x: ECFieldElementFp | null, y: ECFieldElementFp | null, z?: BigInteger);
50
+ getX(): ECFieldElementFp;
51
+ getY(): ECFieldElementFp;
52
+ /**
53
+ * 判断相等
54
+ */
55
+ equals(other: ECPointFp): boolean;
56
+ /**
57
+ * 是否是无穷远点
58
+ */
59
+ isInfinity(): boolean;
60
+ /**
61
+ * 取反,x 轴对称点
62
+ */
63
+ negate(): ECPointFp;
64
+ /**
65
+ * 相加
66
+ *
67
+ * 标准射影坐标系:
68
+ *
69
+ * λ1 = x1 * z2
70
+ * λ2 = x2 * z1
71
+ * λ3 = λ1 − λ2
72
+ * λ4 = y1 * z2
73
+ * λ5 = y2 * z1
74
+ * λ6 = λ4 − λ5
75
+ * λ7 = λ1 + λ2
76
+ * λ8 = z1 * z2
77
+ * λ9 = λ3^2
78
+ * λ10 = λ3 * λ9
79
+ * λ11 = λ8 * λ6^2 − λ7 * λ9
80
+ * x3 = λ3 * λ11
81
+ * y3 = λ6 * (λ9 * λ1 − λ11) − λ4 * λ10
82
+ * z3 = λ10 * λ8
83
+ */
84
+ add(b: ECPointFp): ECPointFp;
85
+ /**
86
+ * 自加
87
+ *
88
+ * 标准射影坐标系:
89
+ *
90
+ * λ1 = 3 * x1^2 + a * z1^2
91
+ * λ2 = 2 * y1 * z1
92
+ * λ3 = y1^2
93
+ * λ4 = λ3 * x1 * z1
94
+ * λ5 = λ2^2
95
+ * λ6 = λ1^2 − 8 * λ4
96
+ * x3 = λ2 * λ6
97
+ * y3 = λ1 * (4 * λ4 − λ6) − 2 * λ5 * λ3
98
+ * z3 = λ2 * λ5
99
+ */
100
+ twice(): ECPointFp;
101
+ /**
102
+ * 倍点计算
103
+ */
104
+ multiply(k: BigInteger): ECPointFp;
105
+ }
106
+ /**
107
+ * 椭圆曲线 y^2 = x^3 + ax + b
108
+ */
109
+ declare class ECCurveFp {
110
+ q: BigInteger;
111
+ infinity: ECPointFp;
112
+ a: ECFieldElementFp;
113
+ b: ECFieldElementFp;
114
+ constructor(q: BigInteger, a: BigInteger, b: BigInteger);
115
+ /**
116
+ * 判断两个椭圆曲线是否相等
117
+ */
118
+ equals(other: ECCurveFp): boolean;
119
+ /**
120
+ * 生成椭圆曲线域元素
121
+ */
122
+ fromBigInteger(x: BigInteger): ECFieldElementFp;
123
+ /**
124
+ * 解析 16 进制串为椭圆曲线点
125
+ */
126
+ decodePointHex(s: string): ECPointFp | null;
127
+ }
128
+
129
+ declare module 'jsbn' {
130
+ class SecureRandom implements RandomGenerator {
131
+ nextBytes(bytes: number[]): void;
132
+ }
133
+ }
134
+ /**
135
+ * 获取公共椭圆曲线
136
+ */
137
+ declare function getGlobalCurve(): ECCurveFp;
138
+ /**
139
+ * 生成ecparam
140
+ */
141
+ declare function generateEcparam(): {
142
+ curve: ECCurveFp;
143
+ G: {
144
+ zinv: BigInteger | null;
145
+ z: BigInteger;
146
+ curve: ECCurveFp;
147
+ x: {
148
+ /**
149
+ * 生成ecparam
150
+ */
151
+ q: BigInteger;
152
+ x: BigInteger;
153
+ equals(other: any): boolean;
154
+ toBigInteger(): BigInteger;
155
+ negate(): any;
156
+ add(b: any): any;
157
+ subtract(b: any): any;
158
+ multiply(b: any): any;
159
+ divide(b: any): any;
160
+ square(): any;
161
+ } | null;
162
+ y: {
163
+ /**
164
+ * 生成ecparam
165
+ */
166
+ q: BigInteger;
167
+ x: BigInteger;
168
+ equals(other: any): boolean;
169
+ toBigInteger(): BigInteger;
170
+ negate(): any;
171
+ add(b: any): any;
172
+ subtract(b: any): any;
173
+ multiply(b: any): any;
174
+ divide(b: any): any;
175
+ square(): any;
176
+ } | null;
177
+ getX(): {
178
+ /**
179
+ * 生成ecparam
180
+ */
181
+ q: BigInteger;
182
+ x: BigInteger;
183
+ equals(other: any): boolean;
184
+ toBigInteger(): BigInteger;
185
+ negate(): any;
186
+ add(b: any): any;
187
+ subtract(b: any): any;
188
+ multiply(b: any): any;
189
+ divide(b: any): any;
190
+ square(): any;
191
+ };
192
+ getY(): {
193
+ /**
194
+ * 生成ecparam
195
+ */
196
+ q: BigInteger;
197
+ x: BigInteger;
198
+ equals(other: any): boolean;
199
+ toBigInteger(): BigInteger;
200
+ negate(): any;
201
+ add(b: any): any;
202
+ subtract(b: any): any;
203
+ multiply(b: any): any;
204
+ divide(b: any): any;
205
+ square(): any;
206
+ };
207
+ equals(other: any): boolean;
208
+ isInfinity(): boolean;
209
+ negate(): any;
210
+ add(b: any): any;
211
+ twice(): any;
212
+ multiply(k: BigInteger): any;
213
+ };
214
+ n: BigInteger;
215
+ };
216
+ /**
217
+ * 生成密钥对:publicKey = privateKey * G
218
+ */
219
+ declare function generateKeyPairHex(a?: number | string, b?: number, c?: RandomGenerator): {
220
+ privateKey: string;
221
+ publicKey: string;
222
+ };
223
+ /**
224
+ * 生成压缩公钥
225
+ */
226
+ declare function compressPublicKeyHex(s: string): string;
227
+ /**
228
+ * utf8串转16进制串
229
+ */
230
+ declare function utf8ToHex(input: string): string;
231
+ /**
232
+ * 补全16进制字符串
233
+ */
234
+ declare function leftPad(input: string, num: number): string;
235
+ /**
236
+ * 转成16进制串
237
+ */
238
+ declare function arrayToHex(arr: number[]): string;
239
+ /**
240
+ * 转成utf8串
241
+ */
242
+ declare function arrayToUtf8(arr: Uint8Array): string;
243
+ /**
244
+ * 转成字节数组
245
+ */
246
+ declare function hexToArray(hexStr: string): Uint8Array;
247
+ /**
248
+ * 验证公钥是否为椭圆曲线上的点
249
+ */
250
+ declare function verifyPublicKey(publicKey: string): boolean;
251
+ /**
252
+ * 验证公钥是否等价,等价返回true
253
+ */
254
+ declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): boolean;
255
+ declare function concatArray(...arrays: Uint8Array[]): Uint8Array;
256
+
257
+ /**
258
+ * 加密
259
+ */
260
+ declare function doEncrypt(msg: string | Uint8Array, publicKey: string, cipherMode?: number): string;
261
+ /**
262
+ * 解密
263
+ */
264
+ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
265
+ output: 'array';
266
+ }): Uint8Array;
267
+ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
268
+ output: 'string';
269
+ }): string;
270
+ interface SignaturePoint {
271
+ k: BigInteger;
272
+ x1: BigInteger;
273
+ }
274
+ /**
275
+ * 签名
276
+ */
277
+ declare function doSignature(msg: Uint8Array | string, privateKey: string, options?: {
278
+ pointPool?: SignaturePoint[];
279
+ der?: boolean;
280
+ hash?: boolean;
281
+ publicKey?: string;
282
+ userId?: string;
283
+ }): string;
284
+ /**
285
+ * 验签
286
+ */
287
+ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string, options?: {
288
+ der?: boolean;
289
+ hash?: boolean;
290
+ userId?: string;
291
+ }): boolean;
292
+ /**
293
+ * sm3杂凑算法
294
+ */
295
+ declare function getHash(hashHex: string | Uint8Array, publicKey: string, userId?: string): string;
296
+ /**
297
+ * 计算公钥
298
+ */
299
+ declare function getPublicKeyFromPrivateKey(privateKey: string): string;
300
+ /**
301
+ * 获取椭圆曲线点
302
+ */
303
+ declare function getPoint(): {
304
+ k: BigInteger;
305
+ x1: BigInteger;
306
+ privateKey: string;
307
+ publicKey: string;
308
+ };
309
+
310
+ declare const index$2_doEncrypt: typeof doEncrypt;
311
+ declare const index$2_doDecrypt: typeof doDecrypt;
312
+ type index$2_SignaturePoint = SignaturePoint;
313
+ declare const index$2_doSignature: typeof doSignature;
314
+ declare const index$2_doVerifySignature: typeof doVerifySignature;
315
+ declare const index$2_getHash: typeof getHash;
316
+ declare const index$2_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
317
+ declare const index$2_getPoint: typeof getPoint;
318
+ declare const index$2_getGlobalCurve: typeof getGlobalCurve;
319
+ declare const index$2_generateEcparam: typeof generateEcparam;
320
+ declare const index$2_generateKeyPairHex: typeof generateKeyPairHex;
321
+ declare const index$2_compressPublicKeyHex: typeof compressPublicKeyHex;
322
+ declare const index$2_utf8ToHex: typeof utf8ToHex;
323
+ declare const index$2_leftPad: typeof leftPad;
324
+ declare const index$2_arrayToHex: typeof arrayToHex;
325
+ declare const index$2_arrayToUtf8: typeof arrayToUtf8;
326
+ declare const index$2_hexToArray: typeof hexToArray;
327
+ declare const index$2_verifyPublicKey: typeof verifyPublicKey;
328
+ declare const index$2_comparePublicKeyHex: typeof comparePublicKeyHex;
329
+ declare const index$2_concatArray: typeof concatArray;
330
+ declare namespace index$2 {
331
+ export {
332
+ index$2_doEncrypt as doEncrypt,
333
+ index$2_doDecrypt as doDecrypt,
334
+ index$2_SignaturePoint as SignaturePoint,
335
+ index$2_doSignature as doSignature,
336
+ index$2_doVerifySignature as doVerifySignature,
337
+ index$2_getHash as getHash,
338
+ index$2_getPublicKeyFromPrivateKey as getPublicKeyFromPrivateKey,
339
+ index$2_getPoint as getPoint,
340
+ index$2_getGlobalCurve as getGlobalCurve,
341
+ index$2_generateEcparam as generateEcparam,
342
+ index$2_generateKeyPairHex as generateKeyPairHex,
343
+ index$2_compressPublicKeyHex as compressPublicKeyHex,
344
+ index$2_utf8ToHex as utf8ToHex,
345
+ index$2_leftPad as leftPad,
346
+ index$2_arrayToHex as arrayToHex,
347
+ index$2_arrayToUtf8 as arrayToUtf8,
348
+ index$2_hexToArray as hexToArray,
349
+ index$2_verifyPublicKey as verifyPublicKey,
350
+ index$2_comparePublicKeyHex as comparePublicKeyHex,
351
+ index$2_concatArray as concatArray,
352
+ };
353
+ }
354
+
355
+ /**
356
+ * 补全16进制字符串
357
+ */
358
+ /**
359
+ * utf8 串转字节数组
360
+ */
361
+ declare function utf8ToArray(str: string): Uint8Array;
362
+ declare function sm3(input: string | Uint8Array, options?: {
363
+ key: Uint8Array | string;
364
+ mode?: 'hmac' | 'mac';
365
+ }): string;
366
+
367
+ declare const index$1_utf8ToArray: typeof utf8ToArray;
368
+ declare const index$1_sm3: typeof sm3;
369
+ declare namespace index$1 {
370
+ export {
371
+ index$1_utf8ToArray as utf8ToArray,
372
+ index$1_sm3 as sm3,
373
+ };
374
+ }
375
+
376
+ interface SM4Options {
377
+ padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
378
+ mode?: 'cbc' | 'ecb';
379
+ iv?: Uint8Array | string;
380
+ output?: 'string' | 'array';
381
+ }
382
+ declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
383
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
384
+ output: 'array';
385
+ } | SM4Options): Uint8Array;
386
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
387
+ output: 'string';
388
+ } | SM4Options): string;
389
+ declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
390
+ output: 'array';
391
+ } | SM4Options): Uint8Array;
392
+ declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
393
+ output: 'string';
394
+ } | SM4Options): string;
395
+
396
+ type index_SM4Options = SM4Options;
397
+ declare const index_sm4: typeof sm4;
398
+ declare const index_encrypt: typeof encrypt;
399
+ declare const index_decrypt: typeof decrypt;
400
+ declare namespace index {
401
+ export {
402
+ index_SM4Options as SM4Options,
403
+ index_sm4 as sm4,
404
+ index_encrypt as encrypt,
405
+ index_decrypt as decrypt,
406
+ };
407
+ }
408
+
409
+ export { index$2 as sm2, index$1 as sm3, index as sm4 };