@maiyunnet/kebab 2.0.7 → 2.0.8

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.
Files changed (83) hide show
  1. package/index.d.ts +11 -1
  2. package/index.js +13 -1
  3. package/lib/buffer.d.ts +25 -0
  4. package/lib/buffer.js +30 -5
  5. package/lib/captcha.d.ts +15 -0
  6. package/lib/captcha.js +20 -0
  7. package/lib/consistent.d.ts +51 -0
  8. package/lib/consistent.js +59 -0
  9. package/lib/core.d.ts +134 -0
  10. package/lib/core.js +176 -0
  11. package/lib/crypto.d.ts +75 -6
  12. package/lib/crypto.js +206 -38
  13. package/lib/db.d.ts +104 -0
  14. package/lib/db.js +126 -0
  15. package/lib/dns.d.ts +51 -0
  16. package/lib/dns.js +54 -2
  17. package/lib/fs.d.ts +100 -0
  18. package/lib/fs.js +118 -0
  19. package/lib/jwt.d.ts +43 -0
  20. package/lib/jwt.js +45 -0
  21. package/lib/kv.d.ts +362 -0
  22. package/lib/kv.js +377 -0
  23. package/lib/lan.d.ts +6 -0
  24. package/lib/lan.js +7 -0
  25. package/lib/net/formdata.d.ts +38 -0
  26. package/lib/net/formdata.js +43 -0
  27. package/lib/net/request.d.ts +62 -0
  28. package/lib/net/request.js +57 -0
  29. package/lib/net/response.d.ts +21 -0
  30. package/lib/net/response.js +16 -0
  31. package/lib/net.d.ts +86 -0
  32. package/lib/net.js +140 -0
  33. package/lib/s3.d.ts +52 -0
  34. package/lib/s3.js +51 -0
  35. package/lib/scan.d.ts +52 -0
  36. package/lib/scan.js +84 -0
  37. package/lib/session.d.ts +31 -0
  38. package/lib/session.js +52 -1
  39. package/lib/sql.d.ts +176 -0
  40. package/lib/sql.js +287 -2
  41. package/lib/ssh/sftp.d.ts +106 -0
  42. package/lib/ssh/sftp.js +106 -0
  43. package/lib/ssh/shell.d.ts +37 -0
  44. package/lib/ssh/shell.js +31 -0
  45. package/lib/ssh.d.ts +32 -0
  46. package/lib/ssh.js +32 -0
  47. package/lib/text.d.ts +131 -0
  48. package/lib/text.js +188 -0
  49. package/lib/time.d.ts +53 -0
  50. package/lib/time.js +55 -0
  51. package/lib/ws.d.ts +68 -0
  52. package/lib/ws.js +74 -0
  53. package/lib/zip.d.ts +53 -0
  54. package/lib/zip.js +73 -0
  55. package/lib/zlib.d.ts +76 -0
  56. package/lib/zlib.js +78 -0
  57. package/main.d.ts +6 -1
  58. package/main.js +11 -1
  59. package/package.json +1 -1
  60. package/sys/child.js +104 -0
  61. package/sys/cmd.js +28 -0
  62. package/sys/ctr.d.ts +166 -0
  63. package/sys/ctr.js +177 -0
  64. package/sys/master.js +63 -0
  65. package/sys/mod.d.ts +266 -0
  66. package/sys/mod.js +335 -0
  67. package/sys/route.d.ts +34 -0
  68. package/sys/route.js +164 -0
  69. package/www/example/ctr/test.d.ts +3 -0
  70. package/www/example/ctr/test.js +63 -1
  71. package/www/example/mod/test.js +14 -0
  72. package/www/example/mod/testdata.js +9 -0
  73. package/www/example/ws/test.js +1 -0
  74. package/.VSCodeCounter/2025-02-14_14-46-44/details.md +0 -82
  75. package/.VSCodeCounter/2025-02-14_14-46-44/diff-details.md +0 -15
  76. package/.VSCodeCounter/2025-02-14_14-46-44/diff.csv +0 -2
  77. package/.VSCodeCounter/2025-02-14_14-46-44/diff.md +0 -19
  78. package/.VSCodeCounter/2025-02-14_14-46-44/diff.txt +0 -22
  79. package/.VSCodeCounter/2025-02-14_14-46-44/results.csv +0 -69
  80. package/.VSCodeCounter/2025-02-14_14-46-44/results.json +0 -1
  81. package/.VSCodeCounter/2025-02-14_14-46-44/results.md +0 -48
  82. package/.VSCodeCounter/2025-02-14_14-46-44/results.txt +0 -118
  83. package/.vscode/tasks.json +0 -15
package/lib/crypto.d.ts CHANGED
@@ -1,4 +1,14 @@
1
+ /**
2
+ * Project: Kebab, User: JianSuoQiYue
3
+ * Date: 2019-4-2 14:01:06
4
+ * Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47
5
+ */
1
6
  import * as crypto from 'crypto';
7
+ /**
8
+ * --- 创建非对称秘钥 ---
9
+ * @param type 如 rsa/ec
10
+ * @param options 参数
11
+ */
2
12
  export declare function generateKeyPair(type: string, options?: {
3
13
  'modulusLength'?: number;
4
14
  'namedCurve'?: string;
@@ -16,32 +26,91 @@ export declare function generateKeyPair(type: string, options?: {
16
26
  }>;
17
27
  export declare function sign(data: crypto.BinaryLike, privateKey: crypto.KeyLike | crypto.SignKeyObjectInput | crypto.SignPrivateKeyInput | crypto.SignJsonWebKeyInput, format: 'hex' | 'base64' | 'binary', algorithm?: string): string;
18
28
  export declare function sign(data: crypto.BinaryLike, privateKey: crypto.KeyLike | crypto.SignKeyObjectInput | crypto.SignPrivateKeyInput | crypto.SignJsonWebKeyInput, format?: 'buffer', algorithm?: string): Buffer;
29
+ /**
30
+ * --- 非对称验签 ---
31
+ * @param data 数据
32
+ * @param object 证书
33
+ * @param signature 签名
34
+ * @param algorithm 哈希方式
35
+ */
19
36
  export declare function verify(data: crypto.BinaryLike, object: crypto.KeyLike | crypto.VerifyKeyObjectInput | crypto.VerifyPublicKeyInput | crypto.VerifyJsonWebKeyInput, signature: NodeJS.ArrayBufferView, algorithm?: string): boolean;
37
+ /**
38
+ * --- 非对称公钥加密 ---
39
+ * @param key 公钥
40
+ * @param buffer 数据
41
+ */
20
42
  export declare function publicEncrypt(key: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
43
+ /**
44
+ * --- 非对称私钥加密 ---
45
+ * @param key 私钥
46
+ * @param buffer 数据
47
+ */
21
48
  export declare function privateEncrypt(key: crypto.RsaPrivateKey | crypto.KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
49
+ /**
50
+ * --- 非对称公钥解密 ---
51
+ * @param key 公钥
52
+ * @param buffer 数据
53
+ */
22
54
  export declare function publicDecrypt(key: crypto.RsaPublicKey | crypto.RsaPrivateKey | crypto.KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
55
+ /**
56
+ * --- 非对称私钥解密 ---
57
+ * @param key 私钥
58
+ * @param buffer 数据
59
+ */
23
60
  export declare function privateDecrypt(key: crypto.RsaPrivateKey | crypto.KeyLike, buffer: NodeJS.ArrayBufferView): Buffer;
24
- export declare const AES_256_ECB = "AES-256-ECB";
25
- export declare const AES_256_CBC = "AES-256-CBC";
26
- export declare const AES_256_CFB = "AES-256-CFB";
27
- export declare const SM4_ECB = "SM4-ECB";
28
- export declare const SM4_CBC = "SM4-CBC";
29
- export declare const SM4_CFB = "SM4-CFB";
61
+ export declare const AES_256_ECB = "aes-256-ecb";
62
+ export declare const AES_256_CBC = "aes-256-cbc";
63
+ export declare const AES_256_CFB = "aes-256-cfb";
64
+ export declare const AES_256_GCM = "aes-256-gcm";
65
+ export declare const SM4_ECB = "sm4-ecb";
66
+ export declare const SM4_CBC = "sm4-cbc";
67
+ export declare const SM4_CFB = "sm4-cfb";
68
+ /**
69
+ * --- cipher 加密,强烈不建议使用 AES_256_ECB ---
70
+ * @param original 原始字符串
71
+ * @param key 密钥 32 个英文字母和数字
72
+ * @param iv 向量 16(CFB) 或 12(GCM) 个英文字母和数字
73
+ * @param method 加密方法
74
+ */
30
75
  export declare function cipherEncrypt(original: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'base64' | 'buffer'): string | Buffer | false;
31
76
  export declare function aesEncrypt(original: string | Buffer, key: crypto.CipherKey, iv: string, method: string, output: 'buffer'): Buffer | false;
32
77
  export declare function aesEncrypt(original: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'base64'): string | false;
78
+ export declare function gcmEncrypt(original: string | Buffer, key: crypto.CipherKey, output: 'buffer'): Buffer | false;
79
+ export declare function gcmEncrypt(original: string | Buffer, key: crypto.CipherKey, output?: 'base64'): string | false;
33
80
  export declare function sm4Encrypt(original: string | Buffer, key: crypto.CipherKey, iv: string, method: string, output: 'buffer'): Buffer | false;
34
81
  export declare function sm4Encrypt(original: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'base64'): string | false;
82
+ /**
83
+ * --- cipher 解密 ---
84
+ * @param encrypt 需解密的字符串
85
+ * @param key 密钥 32 个英文字母和数字
86
+ * @param iv 向量 16(CFB) 或 12(GCM) 个英文字母和数字
87
+ * @param method 加密方法
88
+ */
35
89
  export declare function cipherDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'binary' | 'buffer'): string | Buffer | false;
36
90
  export declare function aesDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv: string, method: string, output: 'buffer'): Buffer | false;
37
91
  export declare function aesDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'binary'): string | false;
92
+ export declare function gcmDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, output: 'buffer'): Buffer | false;
93
+ export declare function gcmDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, output?: 'binary'): string | false;
38
94
  export declare function sm4Decrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv: string, method: string, output: 'buffer'): Buffer | false;
39
95
  export declare function sm4Decrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'binary'): string | false;
40
96
  export declare function hashHmac(algorithm: string, data: Buffer | string, key?: crypto.CipherKey, format?: 'hex' | 'base64'): string;
41
97
  export declare function hashHmac(algorithm: string, data: Buffer | string, key: crypto.CipherKey | undefined, format: 'buffer'): Buffer;
42
98
  export declare function hashHmacFile(algorithm: string, path: string, key?: crypto.CipherKey, encoding?: 'hex' | 'base64' | 'base64url'): Promise<string | false>;
43
99
  export declare function hashHmacFile(algorithm: string, path: string, key: crypto.CipherKey, encoding: 'buffer'): Promise<Buffer | false>;
100
+ /**
101
+ * --- base64 编码 ---
102
+ * @param data 字符串或 Buffer
103
+ */
44
104
  export declare function base64Encode(data: string | Buffer): string;
105
+ /**
106
+ * --- base64 解码 ---
107
+ * @param data base64 编码的字符串
108
+ * @param encoding 指定解出 Buffer 还是 string ---
109
+ */
45
110
  export declare function base64Decode(data: string, encoding: 'buffer'): Buffer;
46
111
  export declare function base64Decode(data: string, encoding?: 'utf8'): string;
112
+ /**
113
+ * --- 生成 uuid ---
114
+ * @param options 选项
115
+ */
47
116
  export declare function uuid(options?: crypto.RandomUUIDOptions): string;
package/lib/crypto.js CHANGED
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.SM4_CFB = exports.SM4_CBC = exports.SM4_ECB = exports.AES_256_CFB = exports.AES_256_CBC = exports.AES_256_ECB = void 0;
36
+ exports.SM4_CFB = exports.SM4_CBC = exports.SM4_ECB = exports.AES_256_GCM = exports.AES_256_CFB = exports.AES_256_CBC = exports.AES_256_ECB = void 0;
37
37
  exports.generateKeyPair = generateKeyPair;
38
38
  exports.sign = sign;
39
39
  exports.verify = verify;
@@ -43,49 +43,50 @@ exports.publicDecrypt = publicDecrypt;
43
43
  exports.privateDecrypt = privateDecrypt;
44
44
  exports.cipherEncrypt = cipherEncrypt;
45
45
  exports.aesEncrypt = aesEncrypt;
46
+ exports.gcmEncrypt = gcmEncrypt;
46
47
  exports.sm4Encrypt = sm4Encrypt;
47
48
  exports.cipherDecrypt = cipherDecrypt;
48
49
  exports.aesDecrypt = aesDecrypt;
50
+ exports.gcmDecrypt = gcmDecrypt;
49
51
  exports.sm4Decrypt = sm4Decrypt;
50
52
  exports.hashHmac = hashHmac;
51
53
  exports.hashHmacFile = hashHmacFile;
52
54
  exports.base64Encode = base64Encode;
53
55
  exports.base64Decode = base64Decode;
54
56
  exports.uuid = uuid;
57
+ /**
58
+ * Project: Kebab, User: JianSuoQiYue
59
+ * Date: 2019-4-2 14:01:06
60
+ * Last: 2020-3-12 14:05:24, 2022-09-12 11:52:35, 2024-9-8 17:09:39, 2024-11-11 00:21:58, 2025-6-18 20:27:47
61
+ */
55
62
  const crypto = __importStar(require("crypto"));
56
- const fs = __importStar(require("../lib/fs"));
63
+ // --- 库和定义 ---
64
+ const lFs = __importStar(require("../lib/fs"));
65
+ const lCore = __importStar(require("../lib/core"));
66
+ // --- 非对称加密 ---
67
+ /**
68
+ * --- 创建非对称秘钥 ---
69
+ * @param type 如 rsa/ec
70
+ * @param options 参数
71
+ */
57
72
  function generateKeyPair(type, options = {}) {
58
73
  return new Promise((resolve) => {
59
- if (options.modulusLength === undefined) {
60
- options.modulusLength = 2048;
61
- }
74
+ options.modulusLength ??= 2048;
62
75
  if (options.namedCurve !== undefined) {
63
76
  options.namedCurve = options.namedCurve.toUpperCase();
64
77
  }
65
- if (options.publicKeyEncoding === undefined) {
66
- options.publicKeyEncoding = {
67
- 'format': 'pem',
68
- 'type': 'spki'
69
- };
70
- }
71
- if (options.publicKeyEncoding.type === undefined) {
72
- options.publicKeyEncoding.type = 'spki';
73
- }
74
- if (options.publicKeyEncoding.format === undefined) {
75
- options.publicKeyEncoding.format = 'pem';
76
- }
77
- if (options.privateKeyEncoding === undefined) {
78
- options.privateKeyEncoding = {
79
- 'format': 'pem',
80
- 'type': 'pkcs8'
81
- };
82
- }
83
- if (options.privateKeyEncoding.type === undefined) {
84
- options.privateKeyEncoding.type = 'pkcs8';
85
- }
86
- if (options.privateKeyEncoding.format === undefined) {
87
- options.privateKeyEncoding.format = 'pem';
88
- }
78
+ options.publicKeyEncoding ??= {
79
+ 'format': 'pem',
80
+ 'type': 'spki'
81
+ };
82
+ options.publicKeyEncoding.type ??= 'spki';
83
+ options.publicKeyEncoding.format ??= 'pem';
84
+ options.privateKeyEncoding ??= {
85
+ 'format': 'pem',
86
+ 'type': 'pkcs8'
87
+ };
88
+ options.privateKeyEncoding.type ??= 'pkcs8';
89
+ options.privateKeyEncoding.format ??= 'pem';
89
90
  crypto.generateKeyPair(type, options, (err, publicKey, privateKey) => {
90
91
  resolve({
91
92
  'private': privateKey,
@@ -94,42 +95,98 @@ function generateKeyPair(type, options = {}) {
94
95
  });
95
96
  });
96
97
  }
98
+ /**
99
+ * --- 非对称加签 ---
100
+ * @param data 数据
101
+ * @param privateKey 私钥
102
+ * @param format 输出格式
103
+ * @param algorithm 哈希方式
104
+ */
97
105
  function sign(data, privateKey, format = 'buffer', algorithm = 'sha256') {
98
106
  const sign = crypto.createSign(algorithm);
99
107
  sign.update(data);
100
108
  return format === 'buffer' ? sign.sign(privateKey) : sign.sign(privateKey, format);
101
109
  }
110
+ /**
111
+ * --- 非对称验签 ---
112
+ * @param data 数据
113
+ * @param object 证书
114
+ * @param signature 签名
115
+ * @param algorithm 哈希方式
116
+ */
102
117
  function verify(data, object, signature, algorithm = 'sha256') {
103
118
  const verify = crypto.createVerify(algorithm);
104
119
  verify.update(data);
105
120
  return verify.verify(object, signature);
106
121
  }
122
+ /**
123
+ * --- 非对称公钥加密 ---
124
+ * @param key 公钥
125
+ * @param buffer 数据
126
+ */
107
127
  function publicEncrypt(key, buffer) {
108
128
  return crypto.publicEncrypt(key, buffer);
109
129
  }
130
+ /**
131
+ * --- 非对称私钥加密 ---
132
+ * @param key 私钥
133
+ * @param buffer 数据
134
+ */
110
135
  function privateEncrypt(key, buffer) {
111
136
  return crypto.privateEncrypt(key, buffer);
112
137
  }
138
+ /**
139
+ * --- 非对称公钥解密 ---
140
+ * @param key 公钥
141
+ * @param buffer 数据
142
+ */
113
143
  function publicDecrypt(key, buffer) {
114
144
  return crypto.publicDecrypt(key, buffer);
115
145
  }
146
+ /**
147
+ * --- 非对称私钥解密 ---
148
+ * @param key 私钥
149
+ * @param buffer 数据
150
+ */
116
151
  function privateDecrypt(key, buffer) {
117
152
  return crypto.privateDecrypt(key, buffer);
118
153
  }
119
- exports.AES_256_ECB = 'AES-256-ECB';
120
- exports.AES_256_CBC = 'AES-256-CBC';
121
- exports.AES_256_CFB = 'AES-256-CFB';
122
- exports.SM4_ECB = 'SM4-ECB';
123
- exports.SM4_CBC = 'SM4-CBC';
124
- exports.SM4_CFB = 'SM4-CFB';
154
+ // --- Cipher (AES/SM4...) 加/解密 ---
155
+ exports.AES_256_ECB = 'aes-256-ecb'; // --- 如果未设置 iv,则默认这个,但强烈不建议 ---
156
+ exports.AES_256_CBC = 'aes-256-cbc';
157
+ exports.AES_256_CFB = 'aes-256-cfb'; // --- 设置 iv,自动就切换成了这个 ---
158
+ exports.AES_256_GCM = 'aes-256-gcm'; // --- 强烈建议使用这个 ---
159
+ exports.SM4_ECB = 'sm4-ecb'; // --- SM4 如果未设置 iv,则默认这个 ---
160
+ exports.SM4_CBC = 'sm4-cbc';
161
+ exports.SM4_CFB = 'sm4-cfb'; // --- SM4 一般用这个,设置 iv,自动就切换成了这个 ---
162
+ /**
163
+ * --- cipher 加密,强烈不建议使用 AES_256_ECB ---
164
+ * @param original 原始字符串
165
+ * @param key 密钥 32 个英文字母和数字
166
+ * @param iv 向量 16(CFB) 或 12(GCM) 个英文字母和数字
167
+ * @param method 加密方法
168
+ */
125
169
  function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, output = 'base64') {
126
170
  try {
127
- if (typeof key === 'string' && key.length < 32) {
171
+ if ((typeof key === 'string') && (key.length !== 32)) {
128
172
  key = hashHmac('md5', key, 'MaiyunSalt');
129
173
  }
174
+ if (iv) {
175
+ if (method === exports.AES_256_CFB) {
176
+ if (iv.length !== 16) {
177
+ return false;
178
+ }
179
+ }
180
+ else if (method === exports.AES_256_GCM) {
181
+ if (iv.length !== 12) {
182
+ return false;
183
+ }
184
+ }
185
+ }
130
186
  const cip = crypto.createCipheriv(method, key, iv);
131
187
  let r;
132
188
  if (output !== 'buffer') {
189
+ // --- base64 ---
133
190
  if (typeof original === 'string') {
134
191
  r = cip.update(original, 'utf8', 'base64');
135
192
  }
@@ -137,8 +194,12 @@ function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, out
137
194
  r = cip.update(original, undefined, 'base64');
138
195
  }
139
196
  r += cip.final('base64');
197
+ if (method === exports.AES_256_GCM) {
198
+ r += cip.getAuthTag().toString('hex');
199
+ }
140
200
  }
141
201
  else {
202
+ // --- buffer ---
142
203
  if (typeof original === 'string') {
143
204
  r = cip.update(original, 'utf8');
144
205
  }
@@ -146,6 +207,9 @@ function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, out
146
207
  r = cip.update(original);
147
208
  }
148
209
  r = Buffer.concat([r, cip.final()]);
210
+ if (method === exports.AES_256_GCM) {
211
+ r = Buffer.concat([r, cip.getAuthTag()]);
212
+ }
149
213
  }
150
214
  return r;
151
215
  }
@@ -153,26 +217,85 @@ function cipherEncrypt(original, key, iv = '', method = exports.AES_256_ECB, out
153
217
  return false;
154
218
  }
155
219
  }
220
+ /**
221
+ * --- AES 加密 ---
222
+ * @param original 原始字符串
223
+ * @param key 密钥尽量 32 个英文字母和数字,不是 32 个系统会自动处理
224
+ * @param iv 向量 16 个英文字母和数字
225
+ * @param method 加密方法
226
+ * @param output 输出类型
227
+ */
156
228
  function aesEncrypt(original, key, iv = '', method = exports.AES_256_ECB, output = 'base64') {
157
229
  if (iv !== '') {
158
230
  method = method === exports.AES_256_ECB ? exports.AES_256_CFB : method;
159
231
  }
160
232
  return cipherEncrypt(original, key, iv, method, output);
161
233
  }
234
+ /**
235
+ * --- AES GCM 托管加密 ---
236
+ * @param original 原始字符串
237
+ * @param key 密钥尽量 32 个英文字母和数字,不是 32 个系统会自动处理
238
+ * @param output 输出类型
239
+ */
240
+ function gcmEncrypt(original, key, output = 'base64') {
241
+ const iv = lCore.random(12, lCore.RANDOM_LUNS);
242
+ const rtn = cipherEncrypt(original, key, iv, exports.AES_256_GCM, output);
243
+ if (!rtn) {
244
+ return false;
245
+ }
246
+ return typeof rtn === 'string' ? iv + rtn : Buffer.concat([Buffer.from(iv), rtn]);
247
+ }
248
+ /**
249
+ * --- SM4 加密 ---
250
+ * @param original 原始字符串
251
+ * @param key 密钥 32 个英文字母和数字
252
+ * @param iv 向量 16 个英文字母和数字
253
+ * @param method 加密方法
254
+ */
162
255
  function sm4Encrypt(original, key, iv = '', method = exports.SM4_ECB, output = 'base64') {
163
256
  if (iv !== '') {
164
257
  method = method === exports.SM4_ECB ? exports.SM4_CFB : method;
165
258
  }
166
259
  return cipherEncrypt(original, key, iv, method, output);
167
260
  }
261
+ /**
262
+ * --- cipher 解密 ---
263
+ * @param encrypt 需解密的字符串
264
+ * @param key 密钥 32 个英文字母和数字
265
+ * @param iv 向量 16(CFB) 或 12(GCM) 个英文字母和数字
266
+ * @param method 加密方法
267
+ */
168
268
  function cipherDecrypt(encrypt, key, iv = '', method = exports.AES_256_ECB, output = 'binary') {
169
269
  try {
170
- if (typeof key === 'string' && key.length < 32) {
270
+ if ((typeof key === 'string') && (key.length !== 32)) {
171
271
  key = hashHmac('md5', key, 'MaiyunSalt');
172
272
  }
273
+ if (iv) {
274
+ if (method === exports.AES_256_CFB) {
275
+ if (iv.length !== 16) {
276
+ return false;
277
+ }
278
+ }
279
+ else if (method === exports.AES_256_GCM) {
280
+ if (iv.length !== 12) {
281
+ return false;
282
+ }
283
+ }
284
+ }
173
285
  const cip = crypto.createDecipheriv(method, key, iv);
286
+ if (method === exports.AES_256_GCM) {
287
+ if (typeof encrypt === 'string') {
288
+ cip.setAuthTag(Buffer.from(encrypt.slice(-32), 'hex'));
289
+ encrypt = encrypt.slice(0, -32);
290
+ }
291
+ else {
292
+ cip.setAuthTag(encrypt.subarray(-16));
293
+ encrypt = encrypt.subarray(0, -16);
294
+ }
295
+ }
174
296
  let r;
175
297
  if (output !== 'buffer') {
298
+ // --- base64 ---
176
299
  if (typeof encrypt === 'string') {
177
300
  r = cip.update(encrypt, 'base64', 'binary');
178
301
  }
@@ -182,6 +305,7 @@ function cipherDecrypt(encrypt, key, iv = '', method = exports.AES_256_ECB, outp
182
305
  r += cip.final('binary');
183
306
  }
184
307
  else {
308
+ // --- buffer ---
185
309
  if (typeof encrypt === 'string') {
186
310
  r = cip.update(encrypt, 'base64');
187
311
  }
@@ -196,18 +320,48 @@ function cipherDecrypt(encrypt, key, iv = '', method = exports.AES_256_ECB, outp
196
320
  return false;
197
321
  }
198
322
  }
323
+ /**
324
+ * --- AES 解密 ---
325
+ * @param encrypt 需解密的字符串
326
+ * @param key 密钥 32 个英文字母和数字
327
+ * @param iv 向量 16 个英文字母和数字
328
+ * @param method 加密方法
329
+ */
199
330
  function aesDecrypt(encrypt, key, iv = '', method = exports.AES_256_ECB, output = 'binary') {
200
331
  if (iv !== '') {
201
332
  method = method === exports.AES_256_ECB ? exports.AES_256_CFB : method;
202
333
  }
203
334
  return cipherDecrypt(encrypt, key, iv, method, output);
204
335
  }
336
+ /**
337
+ * --- AES 解密 ---
338
+ * @param encrypt 需解密的字符串
339
+ * @param key 密钥 32 个英文字母和数字
340
+ * @param iv 向量 16 个英文字母和数字
341
+ * @param method 加密方法
342
+ */
343
+ function gcmDecrypt(encrypt, key, output = 'binary') {
344
+ return cipherDecrypt(typeof encrypt === 'string' ? encrypt.slice(12) : encrypt.subarray(12), key, typeof encrypt === 'string' ? encrypt.slice(0, 12) : encrypt.subarray(0, 12).toString(), exports.AES_256_GCM, output);
345
+ }
346
+ /**
347
+ * --- SM4 解密 ---
348
+ * @param encrypt 需解密的字符串
349
+ * @param key 密钥 32 个英文字母和数字
350
+ * @param iv 向量 16 个英文字母和数字
351
+ * @param method 加密方法
352
+ */
205
353
  function sm4Decrypt(encrypt, key, iv = '', method = exports.SM4_ECB, output = 'binary') {
206
354
  if (iv !== '') {
207
355
  method = method === exports.SM4_ECB ? exports.SM4_CFB : method;
208
356
  }
209
357
  return cipherDecrypt(encrypt, key, iv, method, output);
210
358
  }
359
+ /**
360
+ * --- hash 或 hmac 加密 ---
361
+ * @param algorithm 哈希方式
362
+ * @param data 源数据
363
+ * @param key 设置则采用 hmac 加密
364
+ */
211
365
  function hashHmac(algorithm, data, key, format = 'hex') {
212
366
  const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
213
367
  cry.update(data);
@@ -218,10 +372,16 @@ function hashHmac(algorithm, data, key, format = 'hex') {
218
372
  return cry.digest(format);
219
373
  }
220
374
  }
375
+ /**
376
+ * --- hash 或 hmac 加密文件 ---
377
+ * @param algorithm 加密方式,如 md5、sha256、sm3 等
378
+ * @param path 文件路径
379
+ * @param key 设置则采用 hmac 加密
380
+ */
221
381
  function hashHmacFile(algorithm, path, key, encoding = 'hex') {
222
382
  return new Promise(function (resolve) {
223
383
  const cry = key ? crypto.createHmac(algorithm, key) : crypto.createHash(algorithm);
224
- const rs = fs.createReadStream(path);
384
+ const rs = lFs.createReadStream(path);
225
385
  rs.on('data', (chunk) => {
226
386
  cry.update(chunk);
227
387
  }).on('end', function () {
@@ -236,6 +396,10 @@ function hashHmacFile(algorithm, path, key, encoding = 'hex') {
236
396
  });
237
397
  });
238
398
  }
399
+ /**
400
+ * --- base64 编码 ---
401
+ * @param data 字符串或 Buffer
402
+ */
239
403
  function base64Encode(data) {
240
404
  if (typeof data === 'string') {
241
405
  return Buffer.from(data, 'utf8').toString('base64');
@@ -251,6 +415,10 @@ function base64Decode(data, encoding = 'utf8') {
251
415
  }
252
416
  return buffer.toString('utf8');
253
417
  }
418
+ /**
419
+ * --- 生成 uuid ---
420
+ * @param options 选项
421
+ */
254
422
  function uuid(options) {
255
423
  return crypto.randomUUID(options);
256
424
  }