@jayfong/x-server 2.63.0 → 2.65.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/lib/_cjs/index.js +12 -0
- package/lib/_cjs/services/crypto.js +142 -0
- package/lib/_cjs/x.js +2 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/services/crypto.d.ts +89 -0
- package/lib/services/crypto.js +136 -0
- package/lib/x.d.ts +1 -0
- package/lib/x.js +2 -1
- package/package.json +1 -1
package/lib/_cjs/index.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
exports.__esModule = true;
|
|
4
|
+
var _ctx_storage = require("./core/ctx_storage");
|
|
5
|
+
Object.keys(_ctx_storage).forEach(function (key) {
|
|
6
|
+
if (key === "default" || key === "__esModule") return;
|
|
7
|
+
if (key in exports && exports[key] === _ctx_storage[key]) return;
|
|
8
|
+
exports[key] = _ctx_storage[key];
|
|
9
|
+
});
|
|
4
10
|
var _define_bus = require("./core/define_bus");
|
|
5
11
|
Object.keys(_define_bus).forEach(function (key) {
|
|
6
12
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -145,6 +151,12 @@ Object.keys(_captcha).forEach(function (key) {
|
|
|
145
151
|
if (key in exports && exports[key] === _captcha[key]) return;
|
|
146
152
|
exports[key] = _captcha[key];
|
|
147
153
|
});
|
|
154
|
+
var _crypto = require("./services/crypto");
|
|
155
|
+
Object.keys(_crypto).forEach(function (key) {
|
|
156
|
+
if (key === "default" || key === "__esModule") return;
|
|
157
|
+
if (key in exports && exports[key] === _crypto[key]) return;
|
|
158
|
+
exports[key] = _crypto[key];
|
|
159
|
+
});
|
|
148
160
|
var _db_value = require("./services/db_value");
|
|
149
161
|
Object.keys(_db_value).forEach(function (key) {
|
|
150
162
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
exports.__esModule = true;
|
|
5
|
+
exports.CryptoService = void 0;
|
|
6
|
+
var _nodeCrypto = _interopRequireDefault(require("node:crypto"));
|
|
7
|
+
class CryptoService {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.serviceName = 'crypto';
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 将给定的文本转换为 Base64 编码的字符串
|
|
13
|
+
*
|
|
14
|
+
* @param text 需要编码的原始文本
|
|
15
|
+
* @returns 返回 Base64 编码后的字符串
|
|
16
|
+
*/
|
|
17
|
+
base64Encode(text) {
|
|
18
|
+
return Buffer.from(text).toString('base64');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 将 Base64 编码的字符串转换为 UTF-8 编码的字符串
|
|
23
|
+
*
|
|
24
|
+
* @param text Base64 编码的字符串
|
|
25
|
+
* @returns 转换后的 UTF-8 编码的字符串
|
|
26
|
+
*/
|
|
27
|
+
base64Decode(text) {
|
|
28
|
+
return Buffer.from(text, 'base64').toString('utf8');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 将字符串文本编码为Base64 URL格式
|
|
33
|
+
*
|
|
34
|
+
* 此函数的目的是将输入的字符串转换为Base64编码,并进一步转换为URL安全的格式
|
|
35
|
+
* Base64标准字符集中的'+'和'/'在URL中有特殊意义,因此需要被替换为'-'和'_'
|
|
36
|
+
* 此外,Base64编码中用于填充的'='字符在URL中通常被视为不必要的,因此也会被移除
|
|
37
|
+
*
|
|
38
|
+
* @param text 需要被编码的字符串文本
|
|
39
|
+
* @returns 编码后的Base64 URL安全字符串
|
|
40
|
+
*/
|
|
41
|
+
base64UrlEncode(text) {
|
|
42
|
+
// 将输入文本转换为Buffer对象,并进一步将其转换为Base64编码的字符串
|
|
43
|
+
let base64 = Buffer.from(text).toString('base64');
|
|
44
|
+
|
|
45
|
+
// 将Base64编码中的'+'替换为'-',以适应URL安全要求
|
|
46
|
+
base64 = base64.replace(/\+/g, '-');
|
|
47
|
+
|
|
48
|
+
// 将Base64编码中的'/'替换为'_',以适应URL安全要求
|
|
49
|
+
base64 = base64.replace(/\//g, '_');
|
|
50
|
+
|
|
51
|
+
// 移除Base64编码中用于填充的'='字符,以适应URL安全要求
|
|
52
|
+
base64 = base64.replace(/=/g, '');
|
|
53
|
+
|
|
54
|
+
// 返回经过URL安全处理的Base64编码字符串
|
|
55
|
+
return base64;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 解码base64url格式的字符串
|
|
60
|
+
* base64url是一种用于URL的base64编码变体,它将标准base64中的'+'和'/'字符替换为'-'和'_',以避免URL编码
|
|
61
|
+
* 此函数的作用是将base64url编码的字符串解码回原始字符串
|
|
62
|
+
*
|
|
63
|
+
* @param text 需要解码的base64url格式的字符串
|
|
64
|
+
* @returns 解码后的原始字符串
|
|
65
|
+
*/
|
|
66
|
+
base64UrlDecode(text) {
|
|
67
|
+
// 将base64url编码中的'-'和'_'替换回标准base64编码中的'+'和'/'
|
|
68
|
+
// 然后使用Node.js的Buffer对象从base64编码转换为utf8格式的字符串
|
|
69
|
+
return Buffer.from(text.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 使用MD5算法对文本进行哈希处理
|
|
74
|
+
*
|
|
75
|
+
* @param text 需要进行哈希处理的文本
|
|
76
|
+
* @returns 返回经过MD5哈希处理后的字符串
|
|
77
|
+
*/
|
|
78
|
+
md5(text) {
|
|
79
|
+
return _nodeCrypto.default.createHash('md5').update(text).digest('hex');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 使用SHA-1算法对文本进行哈希处理
|
|
84
|
+
*
|
|
85
|
+
* SHA-1是一种安全散列算法,它接收一个输入(在此例中为文本字符串),
|
|
86
|
+
* 并返回一个固定大小的160位散列值,通常以40位的十六进制数字表示
|
|
87
|
+
* 此函数用于将给定的文本转换为SHA-1散列值,常用于数据完整性校验或存储密码的散列值
|
|
88
|
+
*
|
|
89
|
+
* @param text 待哈希处理的文本字符串
|
|
90
|
+
* @returns 返回文本的SHA-1散列值,以十六进制字符串形式表示
|
|
91
|
+
*/
|
|
92
|
+
sha1(text) {
|
|
93
|
+
return _nodeCrypto.default.createHash('sha1').update(text).digest('hex');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 使用SHA-256算法对文本进行哈希处理
|
|
98
|
+
*
|
|
99
|
+
* @param text 输入的文本字符串
|
|
100
|
+
* @returns 返回经过SHA-256哈希处理后的十六进制字符串
|
|
101
|
+
*/
|
|
102
|
+
sha256(text) {
|
|
103
|
+
return _nodeCrypto.default.createHash('sha256').update(text).digest('hex');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 使用SHA-512算法对文本进行哈希处理
|
|
108
|
+
*
|
|
109
|
+
* @param text 输入的文本字符串
|
|
110
|
+
* @returns 返回经过SHA-512哈希处理后的字符串
|
|
111
|
+
*/
|
|
112
|
+
sha512(text) {
|
|
113
|
+
return _nodeCrypto.default.createHash('sha512').update(text).digest('hex');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 使用给定的密钥对给定的字符串执行 AES 加密(加密模式为 ECB,填充方式为 PKCS7)。
|
|
118
|
+
*
|
|
119
|
+
* @param text - 要加密的字符串。
|
|
120
|
+
* @param key - 用于加密的密钥(仅支持16字节的密钥)。
|
|
121
|
+
*/
|
|
122
|
+
aesEncrypt(text, key) {
|
|
123
|
+
const cipher = _nodeCrypto.default.createCipheriv('aes-128-ecb', key, null);
|
|
124
|
+
let encrypted = cipher.update(text, 'utf8', 'base64');
|
|
125
|
+
encrypted += cipher.final('base64');
|
|
126
|
+
return encrypted;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 使用给定的密钥对给定的字符串执行 AES 解密(解密模式为 ECB,填充方式为 PKCS7)。
|
|
131
|
+
*
|
|
132
|
+
* @param text - 要解密的字符串。
|
|
133
|
+
* @param key - 用于解密的密钥(仅支持16字节的密钥)。
|
|
134
|
+
*/
|
|
135
|
+
aesDecrypt(text, key) {
|
|
136
|
+
const decipher = _nodeCrypto.default.createDecipheriv('aes-128-ecb', key, null);
|
|
137
|
+
let decrypted = decipher.update(text, 'base64', 'utf8');
|
|
138
|
+
decrypted += decipher.final('utf8');
|
|
139
|
+
return decrypted;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.CryptoService = CryptoService;
|
package/lib/_cjs/x.js
CHANGED
|
@@ -7,6 +7,7 @@ var _os = _interopRequireDefault(require("os"));
|
|
|
7
7
|
var _path = _interopRequireDefault(require("path"));
|
|
8
8
|
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
9
9
|
var _ctx_storage = require("./core/ctx_storage");
|
|
10
|
+
var _crypto = require("./services/crypto");
|
|
10
11
|
var _db_value = require("./services/db_value");
|
|
11
12
|
var _dispose = require("./services/dispose");
|
|
12
13
|
var _emoji = require("./services/emoji");
|
|
@@ -29,4 +30,4 @@ const x = exports.x = {
|
|
|
29
30
|
_fsExtra.default.ensureDirSync(x.dataDir);
|
|
30
31
|
x.register(new _dispose.DisposeService({
|
|
31
32
|
disposeOnExit: true
|
|
32
|
-
}), new _log.LogService(), new _emoji.EmojiService(), new _db_value.DbValueService());
|
|
33
|
+
}), new _log.LogService(), new _emoji.EmojiService(), new _db_value.DbValueService(), new _crypto.CryptoService());
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export * from './core/ctx_storage';
|
|
1
2
|
export * from './core/define_bus';
|
|
2
3
|
export * from './core/define_cron';
|
|
3
4
|
export * from './core/define_handler';
|
|
@@ -22,6 +23,7 @@ export * from './plugins/xml_parser';
|
|
|
22
23
|
export * from './services/base';
|
|
23
24
|
export * from './services/cache';
|
|
24
25
|
export * from './services/captcha';
|
|
26
|
+
export * from './services/crypto';
|
|
25
27
|
export * from './services/db_value';
|
|
26
28
|
export * from './services/dingtalk';
|
|
27
29
|
export * from './services/dispose';
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!**/_*'], f => `export * from '${f.path}';`)
|
|
2
|
+
export * from "./core/ctx_storage";
|
|
2
3
|
export * from "./core/define_bus";
|
|
3
4
|
export * from "./core/define_cron";
|
|
4
5
|
export * from "./core/define_handler";
|
|
@@ -23,6 +24,7 @@ export * from "./plugins/xml_parser";
|
|
|
23
24
|
export * from "./services/base";
|
|
24
25
|
export * from "./services/cache";
|
|
25
26
|
export * from "./services/captcha";
|
|
27
|
+
export * from "./services/crypto";
|
|
26
28
|
export * from "./services/db_value";
|
|
27
29
|
export * from "./services/dingtalk";
|
|
28
30
|
export * from "./services/dispose";
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { BaseService } from './base';
|
|
2
|
+
export declare class CryptoService implements BaseService {
|
|
3
|
+
serviceName: string;
|
|
4
|
+
/**
|
|
5
|
+
* 将给定的文本转换为 Base64 编码的字符串
|
|
6
|
+
*
|
|
7
|
+
* @param text 需要编码的原始文本
|
|
8
|
+
* @returns 返回 Base64 编码后的字符串
|
|
9
|
+
*/
|
|
10
|
+
base64Encode(text: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* 将 Base64 编码的字符串转换为 UTF-8 编码的字符串
|
|
13
|
+
*
|
|
14
|
+
* @param text Base64 编码的字符串
|
|
15
|
+
* @returns 转换后的 UTF-8 编码的字符串
|
|
16
|
+
*/
|
|
17
|
+
base64Decode(text: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* 将字符串文本编码为Base64 URL格式
|
|
20
|
+
*
|
|
21
|
+
* 此函数的目的是将输入的字符串转换为Base64编码,并进一步转换为URL安全的格式
|
|
22
|
+
* Base64标准字符集中的'+'和'/'在URL中有特殊意义,因此需要被替换为'-'和'_'
|
|
23
|
+
* 此外,Base64编码中用于填充的'='字符在URL中通常被视为不必要的,因此也会被移除
|
|
24
|
+
*
|
|
25
|
+
* @param text 需要被编码的字符串文本
|
|
26
|
+
* @returns 编码后的Base64 URL安全字符串
|
|
27
|
+
*/
|
|
28
|
+
base64UrlEncode(text: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* 解码base64url格式的字符串
|
|
31
|
+
* base64url是一种用于URL的base64编码变体,它将标准base64中的'+'和'/'字符替换为'-'和'_',以避免URL编码
|
|
32
|
+
* 此函数的作用是将base64url编码的字符串解码回原始字符串
|
|
33
|
+
*
|
|
34
|
+
* @param text 需要解码的base64url格式的字符串
|
|
35
|
+
* @returns 解码后的原始字符串
|
|
36
|
+
*/
|
|
37
|
+
base64UrlDecode(text: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* 使用MD5算法对文本进行哈希处理
|
|
40
|
+
*
|
|
41
|
+
* @param text 需要进行哈希处理的文本
|
|
42
|
+
* @returns 返回经过MD5哈希处理后的字符串
|
|
43
|
+
*/
|
|
44
|
+
md5(text: string): string;
|
|
45
|
+
/**
|
|
46
|
+
* 使用SHA-1算法对文本进行哈希处理
|
|
47
|
+
*
|
|
48
|
+
* SHA-1是一种安全散列算法,它接收一个输入(在此例中为文本字符串),
|
|
49
|
+
* 并返回一个固定大小的160位散列值,通常以40位的十六进制数字表示
|
|
50
|
+
* 此函数用于将给定的文本转换为SHA-1散列值,常用于数据完整性校验或存储密码的散列值
|
|
51
|
+
*
|
|
52
|
+
* @param text 待哈希处理的文本字符串
|
|
53
|
+
* @returns 返回文本的SHA-1散列值,以十六进制字符串形式表示
|
|
54
|
+
*/
|
|
55
|
+
sha1(text: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* 使用SHA-256算法对文本进行哈希处理
|
|
58
|
+
*
|
|
59
|
+
* @param text 输入的文本字符串
|
|
60
|
+
* @returns 返回经过SHA-256哈希处理后的十六进制字符串
|
|
61
|
+
*/
|
|
62
|
+
sha256(text: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* 使用SHA-512算法对文本进行哈希处理
|
|
65
|
+
*
|
|
66
|
+
* @param text 输入的文本字符串
|
|
67
|
+
* @returns 返回经过SHA-512哈希处理后的字符串
|
|
68
|
+
*/
|
|
69
|
+
sha512(text: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* 使用给定的密钥对给定的字符串执行 AES 加密(加密模式为 ECB,填充方式为 PKCS7)。
|
|
72
|
+
*
|
|
73
|
+
* @param text - 要加密的字符串。
|
|
74
|
+
* @param key - 用于加密的密钥(仅支持16字节的密钥)。
|
|
75
|
+
*/
|
|
76
|
+
aesEncrypt(text: string, key: string): string;
|
|
77
|
+
/**
|
|
78
|
+
* 使用给定的密钥对给定的字符串执行 AES 解密(解密模式为 ECB,填充方式为 PKCS7)。
|
|
79
|
+
*
|
|
80
|
+
* @param text - 要解密的字符串。
|
|
81
|
+
* @param key - 用于解密的密钥(仅支持16字节的密钥)。
|
|
82
|
+
*/
|
|
83
|
+
aesDecrypt(text: string, key: string): string;
|
|
84
|
+
}
|
|
85
|
+
declare module '../x' {
|
|
86
|
+
interface X {
|
|
87
|
+
crypto: CryptoService;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
export class CryptoService {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.serviceName = 'crypto';
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 将给定的文本转换为 Base64 编码的字符串
|
|
8
|
+
*
|
|
9
|
+
* @param text 需要编码的原始文本
|
|
10
|
+
* @returns 返回 Base64 编码后的字符串
|
|
11
|
+
*/
|
|
12
|
+
base64Encode(text) {
|
|
13
|
+
return Buffer.from(text).toString('base64');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 将 Base64 编码的字符串转换为 UTF-8 编码的字符串
|
|
18
|
+
*
|
|
19
|
+
* @param text Base64 编码的字符串
|
|
20
|
+
* @returns 转换后的 UTF-8 编码的字符串
|
|
21
|
+
*/
|
|
22
|
+
base64Decode(text) {
|
|
23
|
+
return Buffer.from(text, 'base64').toString('utf8');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 将字符串文本编码为Base64 URL格式
|
|
28
|
+
*
|
|
29
|
+
* 此函数的目的是将输入的字符串转换为Base64编码,并进一步转换为URL安全的格式
|
|
30
|
+
* Base64标准字符集中的'+'和'/'在URL中有特殊意义,因此需要被替换为'-'和'_'
|
|
31
|
+
* 此外,Base64编码中用于填充的'='字符在URL中通常被视为不必要的,因此也会被移除
|
|
32
|
+
*
|
|
33
|
+
* @param text 需要被编码的字符串文本
|
|
34
|
+
* @returns 编码后的Base64 URL安全字符串
|
|
35
|
+
*/
|
|
36
|
+
base64UrlEncode(text) {
|
|
37
|
+
// 将输入文本转换为Buffer对象,并进一步将其转换为Base64编码的字符串
|
|
38
|
+
let base64 = Buffer.from(text).toString('base64');
|
|
39
|
+
|
|
40
|
+
// 将Base64编码中的'+'替换为'-',以适应URL安全要求
|
|
41
|
+
base64 = base64.replace(/\+/g, '-');
|
|
42
|
+
|
|
43
|
+
// 将Base64编码中的'/'替换为'_',以适应URL安全要求
|
|
44
|
+
base64 = base64.replace(/\//g, '_');
|
|
45
|
+
|
|
46
|
+
// 移除Base64编码中用于填充的'='字符,以适应URL安全要求
|
|
47
|
+
base64 = base64.replace(/=/g, '');
|
|
48
|
+
|
|
49
|
+
// 返回经过URL安全处理的Base64编码字符串
|
|
50
|
+
return base64;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 解码base64url格式的字符串
|
|
55
|
+
* base64url是一种用于URL的base64编码变体,它将标准base64中的'+'和'/'字符替换为'-'和'_',以避免URL编码
|
|
56
|
+
* 此函数的作用是将base64url编码的字符串解码回原始字符串
|
|
57
|
+
*
|
|
58
|
+
* @param text 需要解码的base64url格式的字符串
|
|
59
|
+
* @returns 解码后的原始字符串
|
|
60
|
+
*/
|
|
61
|
+
base64UrlDecode(text) {
|
|
62
|
+
// 将base64url编码中的'-'和'_'替换回标准base64编码中的'+'和'/'
|
|
63
|
+
// 然后使用Node.js的Buffer对象从base64编码转换为utf8格式的字符串
|
|
64
|
+
return Buffer.from(text.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 使用MD5算法对文本进行哈希处理
|
|
69
|
+
*
|
|
70
|
+
* @param text 需要进行哈希处理的文本
|
|
71
|
+
* @returns 返回经过MD5哈希处理后的字符串
|
|
72
|
+
*/
|
|
73
|
+
md5(text) {
|
|
74
|
+
return crypto.createHash('md5').update(text).digest('hex');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 使用SHA-1算法对文本进行哈希处理
|
|
79
|
+
*
|
|
80
|
+
* SHA-1是一种安全散列算法,它接收一个输入(在此例中为文本字符串),
|
|
81
|
+
* 并返回一个固定大小的160位散列值,通常以40位的十六进制数字表示
|
|
82
|
+
* 此函数用于将给定的文本转换为SHA-1散列值,常用于数据完整性校验或存储密码的散列值
|
|
83
|
+
*
|
|
84
|
+
* @param text 待哈希处理的文本字符串
|
|
85
|
+
* @returns 返回文本的SHA-1散列值,以十六进制字符串形式表示
|
|
86
|
+
*/
|
|
87
|
+
sha1(text) {
|
|
88
|
+
return crypto.createHash('sha1').update(text).digest('hex');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 使用SHA-256算法对文本进行哈希处理
|
|
93
|
+
*
|
|
94
|
+
* @param text 输入的文本字符串
|
|
95
|
+
* @returns 返回经过SHA-256哈希处理后的十六进制字符串
|
|
96
|
+
*/
|
|
97
|
+
sha256(text) {
|
|
98
|
+
return crypto.createHash('sha256').update(text).digest('hex');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 使用SHA-512算法对文本进行哈希处理
|
|
103
|
+
*
|
|
104
|
+
* @param text 输入的文本字符串
|
|
105
|
+
* @returns 返回经过SHA-512哈希处理后的字符串
|
|
106
|
+
*/
|
|
107
|
+
sha512(text) {
|
|
108
|
+
return crypto.createHash('sha512').update(text).digest('hex');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 使用给定的密钥对给定的字符串执行 AES 加密(加密模式为 ECB,填充方式为 PKCS7)。
|
|
113
|
+
*
|
|
114
|
+
* @param text - 要加密的字符串。
|
|
115
|
+
* @param key - 用于加密的密钥(仅支持16字节的密钥)。
|
|
116
|
+
*/
|
|
117
|
+
aesEncrypt(text, key) {
|
|
118
|
+
const cipher = crypto.createCipheriv('aes-128-ecb', key, null);
|
|
119
|
+
let encrypted = cipher.update(text, 'utf8', 'base64');
|
|
120
|
+
encrypted += cipher.final('base64');
|
|
121
|
+
return encrypted;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 使用给定的密钥对给定的字符串执行 AES 解密(解密模式为 ECB,填充方式为 PKCS7)。
|
|
126
|
+
*
|
|
127
|
+
* @param text - 要解密的字符串。
|
|
128
|
+
* @param key - 用于解密的密钥(仅支持16字节的密钥)。
|
|
129
|
+
*/
|
|
130
|
+
aesDecrypt(text, key) {
|
|
131
|
+
const decipher = crypto.createDecipheriv('aes-128-ecb', key, null);
|
|
132
|
+
let decrypted = decipher.update(text, 'base64', 'utf8');
|
|
133
|
+
decrypted += decipher.final('utf8');
|
|
134
|
+
return decrypted;
|
|
135
|
+
}
|
|
136
|
+
}
|
package/lib/x.d.ts
CHANGED
package/lib/x.js
CHANGED
|
@@ -2,6 +2,7 @@ import os from 'os';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import fs from 'fs-extra';
|
|
4
4
|
import { ctxStorage } from "./core/ctx_storage";
|
|
5
|
+
import { CryptoService } from "./services/crypto";
|
|
5
6
|
import { DbValueService } from "./services/db_value";
|
|
6
7
|
import { DisposeService } from "./services/dispose";
|
|
7
8
|
import { EmojiService } from "./services/emoji";
|
|
@@ -24,4 +25,4 @@ export const x = {
|
|
|
24
25
|
fs.ensureDirSync(x.dataDir);
|
|
25
26
|
x.register(new DisposeService({
|
|
26
27
|
disposeOnExit: true
|
|
27
|
-
}), new LogService(), new EmojiService(), new DbValueService());
|
|
28
|
+
}), new LogService(), new EmojiService(), new DbValueService(), new CryptoService());
|