knight-web 2.0.3 → 2.0.5

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,215 @@
1
+ import { Hasher, WordArray } from 'crypto-es';
2
+ import { Format } from 'crypto-es/dist/cipher-core.cjs';
3
+ import { Encoder, HasherCfg } from 'crypto-es/dist/core.cjs';
4
+ /** 加密工具类
5
+ * npm install crypto-es
6
+ * npm install @types/crypto-es
7
+ * npm install jsonwebtoken
8
+ * npm install @types/jsonwebtoken
9
+ */
10
+ export declare namespace CryptoUtility {
11
+ /** 常用Token加解密工具 */
12
+ class Token {
13
+ /**
14
+ * 验签及解析Token
15
+ * @param _cipherText
16
+ * @param _key
17
+ */
18
+ static CheckToken(_cipherText: string, _key?: string): ICheckResult;
19
+ /**
20
+ * 解析Base64URL
21
+ * @param _message
22
+ * @returns
23
+ */
24
+ static OnBase64UrlDecode(_message: string): string;
25
+ }
26
+ /** 检测结果 */
27
+ interface ICheckResult {
28
+ header: object;
29
+ payload: object;
30
+ signature: boolean;
31
+ }
32
+ /**
33
+ * AES(Advanced Encryption Standard)加密算法工具
34
+ * @introduce 最常用的对称加密算法,支持不同的密钥长度(128、192 和 256 位)
35
+ */
36
+ class _AES {
37
+ /** 获取编码转换器 */
38
+ private static OnGetEncoder;
39
+ /** 获取密文配置
40
+ * CBC、CFB、OFB、CTR模式必须提供IV,ECB模式不能提供IV
41
+ * IV长度通常为16字节(128位)
42
+ */
43
+ private static OnGetCipherCfg;
44
+ /**
45
+ * 加密
46
+ * @param context 待加密数据
47
+ * @param key 密钥
48
+ * @param cipherConfig 密文配置参数
49
+ * @returns 加密后数据
50
+ */
51
+ static Encrypt(context: string, key: string, cipherConfig?: CipherConfig): string;
52
+ /**
53
+ * 解密
54
+ * @param context 待解密数据
55
+ * @param key 密钥
56
+ * @param cipherConfig 配置参数
57
+ * @returns WordArray 密文参数
58
+ */
59
+ static Decrypt(context: string, key: string, cipherConfig?: CipherConfig): string;
60
+ }
61
+ /** 密文配置 */
62
+ interface CipherConfig {
63
+ /** 初始化向量(IV长度通常为16字节128位) */
64
+ iv?: string;
65
+ /** 分组密码模式 */
66
+ mode?: E_ModeType;
67
+ /** 填充策略 */
68
+ padding?: E_PaddingType;
69
+ /** 序列化格式化器 */
70
+ format?: Format;
71
+ /** 密钥派生函数 */
72
+ kdf?: KDF;
73
+ /** 用于密钥生成的盐值 */
74
+ salt?: string;
75
+ /** 用于密钥生成的哈希器 */
76
+ hasher?: new (cfg?: HasherCfg) => Hasher;
77
+ /** RC4 降值操作 */
78
+ drop?: number;
79
+ }
80
+ /** 编码转换类型 */
81
+ enum E_EncodeType {
82
+ Base64 = "base64",
83
+ Hex = "hex",
84
+ Latin1 = "latin1",
85
+ Utf16 = "utf16",
86
+ Utf16BE = "utf16BE",
87
+ Utf16LE = "utf16LE",
88
+ Utf8 = "utf8"
89
+ }
90
+ /** 加密模式类型 */
91
+ enum E_ModeType {
92
+ CBC = "cbc",
93
+ ECB = "ecb",
94
+ CFB = "cfb",
95
+ OFB = "ofb",
96
+ CTR = "ctr",
97
+ GCM = "gcm"
98
+ }
99
+ /** 填充方式类型 */
100
+ enum E_PaddingType {
101
+ PKCS7 = "pkcs7",
102
+ PKCS5 = "pkcs5",
103
+ ZeroPadding = "zeropadding",
104
+ ISO10126 = "iso10126",
105
+ ISO97971 = "iso97971",
106
+ Ansix923 = "ansix923",
107
+ NoPadding = "nopadding"
108
+ }
109
+ /** 编码转换 */
110
+ class Encode {
111
+ /**
112
+ * Base64编码
113
+ * @param data 输入数据
114
+ * @returns 编码后的字符集
115
+ */
116
+ static Base64: Encoder;
117
+ /**
118
+ * Base64url编码
119
+ * @param data 输入数据
120
+ * @returns 编码后的字符集
121
+ */
122
+ /**
123
+ * Hex编码
124
+ * @param data 输入数据
125
+ * @returns 编码后的字符集
126
+ */
127
+ static Hex: Encoder;
128
+ /**
129
+ * Latin1编码
130
+ * @param data 输入数据
131
+ * @returns 编码后的字符集
132
+ */
133
+ static Latin1: Encoder;
134
+ /**
135
+ * Utf16编码
136
+ * @param data 输入数据
137
+ * @returns 编码后的字符集
138
+ */
139
+ static Utf16: Encoder;
140
+ /**
141
+ * Utf16BE编码
142
+ * @param data 输入数据
143
+ * @returns 编码后的字符集
144
+ */
145
+ static Utf16BE: Encoder;
146
+ /**
147
+ * Utf16LE编码
148
+ * @param data 输入数据
149
+ * @returns 编码后的字符集
150
+ */
151
+ static Utf16LE: Encoder;
152
+ /**
153
+ * Utf16LE编码
154
+ * @param data 输入数据
155
+ * @returns 编码后的字符集
156
+ */
157
+ static Utf8: Encoder;
158
+ }
159
+ /**
160
+ * 格式化工具
161
+ */
162
+ class _Format {
163
+ /** Hex */
164
+ static Hex: Encoder;
165
+ }
166
+ /**
167
+ * KDF工具
168
+ */
169
+ class KDF {
170
+ }
171
+ /**
172
+ * 使用SHA256进行哈希计算
173
+ * @param message 输入数据
174
+ * @param cfg 哈希配置数据
175
+ * @returns 哈希后的值
176
+ * @example
177
+ * const hash = CryptoUtility.SHA256('message');
178
+ * const hash = CryptoUtility.SHA256(wordArray);
179
+ */
180
+ function _SHA256(message: WordArray | string, cfg?: HasherCfg): WordArray;
181
+ /**
182
+ * 使用MD5进行哈希计算
183
+ * @param message 输入数据
184
+ * @param cfg 哈希配置数据
185
+ * @returns 哈希后的值
186
+ * @example
187
+ * const hash = CryptoUtility.MD5('message');
188
+ * const hash = CryptoUtility.MD5(wordArray);
189
+ */
190
+ function _MD5(message: WordArray | string, cfg?: HasherCfg): WordArray;
191
+ /**
192
+ * HmacSHA256编码
193
+ * @param message 输入数据
194
+ * @param key 密钥
195
+ * @returns 编码后的值
196
+ * @example
197
+ * const hmac = CryptoUtility.HmacSHA256(message, key);
198
+ */
199
+ function _HmacSHA256(message: WordArray | string, key: WordArray | string): WordArray;
200
+ /**
201
+ * HmacSHA256编码
202
+ * @param message 输入数据
203
+ * @param key 密钥
204
+ * @returns 编码后的值
205
+ * @example
206
+ * const hmac = CryptoUtility.HmacMD5(message, key);
207
+ */
208
+ function _HmacMD5(message: WordArray | string, key: WordArray | string): WordArray;
209
+ /**
210
+ * 随机生成密钥
211
+ * @param length 密钥长度
212
+ * @returns 随机生成的密钥
213
+ */
214
+ function _RandomKey(length?: number): string;
215
+ }