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.
@@ -0,0 +1,175 @@
1
+ import { ProjPointType } from '@noble/curves/abstract/weierstrass';
2
+
3
+ interface KeyPair {
4
+ privateKey: string;
5
+ publicKey: string;
6
+ }
7
+ /**
8
+ * 生成密钥对:publicKey = privateKey * G
9
+ */
10
+ declare function generateKeyPairHex(str?: string): KeyPair;
11
+ /**
12
+ * 生成压缩公钥
13
+ */
14
+ declare function compressPublicKeyHex(s: string): string;
15
+ /**
16
+ * utf8串转16进制串
17
+ */
18
+ declare function utf8ToHex(input: string): string;
19
+ /**
20
+ * 补全16进制字符串
21
+ */
22
+ declare function leftPad(input: string, num: number): string;
23
+ /**
24
+ * 转成16进制串
25
+ */
26
+ declare function arrayToHex(arr: number[]): string;
27
+ /**
28
+ * 转成utf8串
29
+ */
30
+ declare function arrayToUtf8(arr: Uint8Array): string;
31
+ /**
32
+ * 转成字节数组
33
+ */
34
+ declare function hexToArray(hexStr: string): Uint8Array;
35
+ /**
36
+ * 验证公钥是否为椭圆曲线上的点
37
+ */
38
+ declare function verifyPublicKey(publicKey: string): boolean;
39
+ /**
40
+ * 验证公钥是否等价,等价返回true
41
+ */
42
+ declare function comparePublicKeyHex(publicKey1: string, publicKey2: string): boolean;
43
+
44
+ declare function initRNGPool(): Promise<void>;
45
+
46
+ declare function calculateSharedKey(keypairA: KeyPair, ephemeralKeypairA: KeyPair, publicKeyB: string, ephemeralPublicKeyB: string, sharedKeyLength: number, isRecipient?: boolean, idA?: string, idB?: string): Uint8Array;
47
+
48
+ declare const EmptyArray: Uint8Array;
49
+ /**
50
+ * 加密
51
+ */
52
+ declare function doEncrypt(msg: string | Uint8Array, publicKey: string | ProjPointType<bigint>, cipherMode?: number, options?: {
53
+ asn1?: boolean;
54
+ }): string;
55
+ /**
56
+ * 解密
57
+ */
58
+ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
59
+ output: 'array';
60
+ asn1?: boolean;
61
+ }): Uint8Array;
62
+ declare function doDecrypt(encryptData: string, privateKey: string, cipherMode?: number, options?: {
63
+ output: 'string';
64
+ asn1?: boolean;
65
+ }): string;
66
+ interface SignaturePoint {
67
+ k: bigint;
68
+ x1: bigint;
69
+ }
70
+ /**
71
+ * 签名
72
+ */
73
+ declare function doSignature(msg: Uint8Array | string, privateKey: string, options?: {
74
+ pointPool?: SignaturePoint[];
75
+ der?: boolean;
76
+ hash?: boolean;
77
+ publicKey?: string;
78
+ userId?: string;
79
+ }): string;
80
+ /**
81
+ * 验签
82
+ */
83
+ declare function doVerifySignature(msg: string | Uint8Array, signHex: string, publicKey: string | ProjPointType<bigint>, options?: {
84
+ der?: boolean;
85
+ hash?: boolean;
86
+ userId?: string;
87
+ }): boolean;
88
+ declare function getZ(publicKey: string, userId?: string): Uint8Array;
89
+ /**
90
+ * sm3杂凑算法
91
+ */
92
+ declare function getHash(hashHex: string | Uint8Array, publicKey: string, userId?: string): string;
93
+ /**
94
+ * 预计算公钥点,可用于提升加密性能
95
+ * @export
96
+ * @param {string} publicKey 公钥
97
+ * @param windowSize 计算窗口大小,默认为 8
98
+ * @returns {ProjPointType<bigint>} 预计算的点
99
+ */
100
+ declare function precomputePublicKey(publicKey: string, windowSize?: number): ProjPointType<bigint>;
101
+ /**
102
+ * 计算公钥
103
+ */
104
+ declare function getPublicKeyFromPrivateKey(privateKey: string): string;
105
+ /**
106
+ * 获取椭圆曲线点
107
+ */
108
+ declare function getPoint(): {
109
+ k: bigint;
110
+ x1: bigint;
111
+ privateKey: string;
112
+ publicKey: string;
113
+ };
114
+
115
+ declare const index$1_EmptyArray: typeof EmptyArray;
116
+ type index$1_KeyPair = KeyPair;
117
+ type index$1_SignaturePoint = SignaturePoint;
118
+ declare const index$1_arrayToHex: typeof arrayToHex;
119
+ declare const index$1_arrayToUtf8: typeof arrayToUtf8;
120
+ declare const index$1_calculateSharedKey: typeof calculateSharedKey;
121
+ declare const index$1_comparePublicKeyHex: typeof comparePublicKeyHex;
122
+ declare const index$1_compressPublicKeyHex: typeof compressPublicKeyHex;
123
+ declare const index$1_doDecrypt: typeof doDecrypt;
124
+ declare const index$1_doEncrypt: typeof doEncrypt;
125
+ declare const index$1_doSignature: typeof doSignature;
126
+ declare const index$1_doVerifySignature: typeof doVerifySignature;
127
+ declare const index$1_generateKeyPairHex: typeof generateKeyPairHex;
128
+ declare const index$1_getHash: typeof getHash;
129
+ declare const index$1_getPoint: typeof getPoint;
130
+ declare const index$1_getPublicKeyFromPrivateKey: typeof getPublicKeyFromPrivateKey;
131
+ declare const index$1_getZ: typeof getZ;
132
+ declare const index$1_hexToArray: typeof hexToArray;
133
+ declare const index$1_initRNGPool: typeof initRNGPool;
134
+ declare const index$1_leftPad: typeof leftPad;
135
+ declare const index$1_precomputePublicKey: typeof precomputePublicKey;
136
+ declare const index$1_utf8ToHex: typeof utf8ToHex;
137
+ declare const index$1_verifyPublicKey: typeof verifyPublicKey;
138
+ 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 };
140
+ }
141
+
142
+ declare function sm3(input: string | Uint8Array, options?: {
143
+ key: Uint8Array | string;
144
+ mode?: 'hmac' | 'mac';
145
+ }): string;
146
+
147
+ interface SM4Options {
148
+ padding?: 'pkcs#7' | 'pkcs#5' | 'none' | null;
149
+ mode?: 'cbc' | 'ecb';
150
+ iv?: Uint8Array | string;
151
+ output?: 'string' | 'array';
152
+ }
153
+ declare function sm4(inArray: Uint8Array | string, key: Uint8Array | string, cryptFlag: 0 | 1, options?: SM4Options): string | Uint8Array;
154
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
155
+ output: 'array';
156
+ } | SM4Options): Uint8Array;
157
+ declare function encrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
158
+ output: 'string';
159
+ } | SM4Options): string;
160
+ declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
161
+ output: 'array';
162
+ } | SM4Options): Uint8Array;
163
+ declare function decrypt(inArray: Uint8Array | string, key: Uint8Array | string, options?: {
164
+ output: 'string';
165
+ } | SM4Options): string;
166
+
167
+ type index_SM4Options = SM4Options;
168
+ declare const index_decrypt: typeof decrypt;
169
+ declare const index_encrypt: typeof encrypt;
170
+ declare const index_sm4: typeof sm4;
171
+ declare namespace index {
172
+ export { type index_SM4Options as SM4Options, index_decrypt as decrypt, index_encrypt as encrypt, index_sm4 as sm4 };
173
+ }
174
+
175
+ export { index$1 as sm2, sm3, index as sm4 };