@yoooloo42/beat 1.0.17 → 1.0.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yoooloo42/beat",
3
- "version": "1.0.17",
3
+ "version": "1.0.19",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -16,8 +16,11 @@
16
16
  },
17
17
  "homepage": "https://github.com/13993193075/beat#readme",
18
18
  "dependencies": {
19
+ "@alicloud/ocr20191230": "^4.0.1",
20
+ "@alicloud/openapi-client": "^0.4.15",
19
21
  "@alicloud/pop-core": "^1.8.0",
20
22
  "@yoooloo42/bean": "^1.0.13",
23
+ "aliyun-api-gateway": "^1.1.6",
21
24
  "nodemailer": "^7.0.10"
22
25
  },
23
26
  "exports": {
@@ -0,0 +1,124 @@
1
+ import crypto from 'crypto';
2
+
3
+ // --- 安全常量定义 ---
4
+ const ALGORITHM = 'aes-128-cbc'; // 算法:AES-128-CBC
5
+ const IV_LENGTH = 16; // IV 长度:16 字节 (128 位)
6
+ const KEY_LENGTH = 16; // 密钥长度:16 字节 (128 位)
7
+ const INPUT_ENCODING = 'utf8'; // 明文输入编码
8
+ const OUTPUT_FORMAT = 'base64';// 密文输出格式 (通常用 Base64 或 Hex)
9
+
10
+ /**
11
+ * 🔑 生成安全随机的 AES 密钥和 IV。
12
+ * 密钥和 IV 应仅生成一次,并安全地存储(例如,作为环境变量或安全配置文件)。
13
+ *
14
+ * @returns {Object} 包含 base64 格式密钥和 IV 的对象
15
+ */
16
+ function generateKeyAndIV() {
17
+ // 使用 cryptographically secure pseudo-random number generator (CSPRNG)
18
+ // Node.js 的 crypto.randomBytes 保证了生成的随机性。
19
+ const key = crypto.randomBytes(KEY_LENGTH).toString(OUTPUT_FORMAT);
20
+ const iv = crypto.randomBytes(IV_LENGTH).toString(OUTPUT_FORMAT);
21
+
22
+ return {
23
+ key: key, // Base64 格式的 16 字节密钥
24
+ iv: iv // Base64 格式的 16 字节 IV
25
+ };
26
+ }
27
+ // 示例用法:
28
+ // const { key, iv } = generateKeyAndIV();
29
+ // console.log("Key:", key);
30
+ // console.log("IV:", iv);
31
+
32
+
33
+ /**
34
+ * 检查密钥和初始化向量的长度是否符合 AES-128-CBC 规范。
35
+ * @param {Buffer} keyBuffer 密钥 Buffer
36
+ * @param {Buffer} ivBuffer 初始化向量 Buffer
37
+ */
38
+ function checkKeyAndIV(keyBuffer, ivBuffer) {
39
+ if (keyBuffer.length !== KEY_LENGTH) {
40
+ throw new Error(`Invalid Key Length. Key must be ${KEY_LENGTH} bytes for ${ALGORITHM}.`);
41
+ }
42
+ if (ivBuffer.length !== IV_LENGTH) {
43
+ throw new Error(`Invalid IV Length. IV must be ${IV_LENGTH} bytes for ${ALGORITHM}.`);
44
+ }
45
+ }
46
+
47
+ /**
48
+ * 🔐 AES-128-CBC 加密
49
+ * 使用内置 crypto 模块,默认使用 PKCS7 自动补位。
50
+ * @param {Object} params
51
+ * @param {string} params.text - 明文
52
+ * @param {string} params.key - 16字节的密钥字符串
53
+ * @param {string} params.iv - 16字节的初始化向量字符串
54
+ * @returns {string} Base64 格式的密文
55
+ */
56
+ function aesEncrypt({ text, key, iv }) {
57
+ try {
58
+ const keyBuffer = Buffer.from(key, INPUT_ENCODING);
59
+ const ivBuffer = Buffer.from(iv, INPUT_ENCODING);
60
+
61
+ checkKeyAndIV(keyBuffer, ivBuffer);
62
+
63
+ // 1. 创建加密器,默认自动 PKCS7 补位
64
+ const cipher = crypto.createCipheriv(ALGORITHM, keyBuffer, ivBuffer);
65
+
66
+ // 2. 加密主体
67
+ // textBuffer 是 Buffer,所以不需要第二个 'utf8' 参数,但为了清晰,使用 INPUT_ENCODING
68
+ let encrypted = cipher.update(text, INPUT_ENCODING, 'hex');
69
+
70
+ // 3. 完成加密,并应用最终补位
71
+ encrypted += cipher.final('hex');
72
+
73
+ // 4. 将 Hex 转换为 Base64 输出
74
+ return Buffer.from(encrypted, 'hex').toString(OUTPUT_FORMAT);
75
+
76
+ } catch (error) {
77
+ console.error("AES Encryption Error:", error.message);
78
+ throw new Error("Encryption failed.");
79
+ }
80
+ }
81
+
82
+ /**
83
+ * 🔓 AES-128-CBC 解密
84
+ * 使用内置 crypto 模块,自动移除 PKCS7 补位。
85
+ * @param {Object} params
86
+ * @param {string} params.text - Base64 格式的密文
87
+ * @param {string} params.key - 16字节的密钥字符串
88
+ * @param {string} params.iv - 16字节的初始化向量字符串
89
+ * @returns {string} 明文
90
+ */
91
+ function aesDecrypt({ text, key, iv }) {
92
+ try {
93
+ const keyBuffer = Buffer.from(key, INPUT_ENCODING);
94
+ const ivBuffer = Buffer.from(iv, INPUT_ENCODING);
95
+ checkKeyAndIV(keyBuffer, ivBuffer);
96
+
97
+ // 1. 将 Base64 密文转为 Buffer
98
+ const encryptedBuffer = Buffer.from(text, OUTPUT_FORMAT);
99
+
100
+ // 2. 创建解密器,默认自动移除补位 (Auto Padding: true)
101
+ const decipher = crypto.createDecipheriv(ALGORITHM, keyBuffer, ivBuffer);
102
+
103
+ // 3. 解密主体
104
+ // 因为输入是 Buffer,所以第二个参数可以省略或使用 'buffer'
105
+ let decrypted = decipher.update(encryptedBuffer, 'buffer', INPUT_ENCODING);
106
+
107
+ // 4. 完成解密,并移除补位
108
+ decrypted += decipher.final(INPUT_ENCODING);
109
+
110
+ return decrypted;
111
+ } catch (error) {
112
+ // 在解密失败(如密文被篡改)时,decipher.final() 会抛出错误
113
+ console.error("AES Decryption Error:", error.message);
114
+ throw new Error("Decryption failed. Ciphertext may be invalid or tampered with.");
115
+ }
116
+ }
117
+
118
+ export default {
119
+ // 推荐使用
120
+ generateKeyAndIV,
121
+ checkKeyAndIV,
122
+ aesEncrypt,
123
+ aesDecrypt
124
+ };
@@ -0,0 +1,88 @@
1
+ import crypto from 'crypto';
2
+
3
+ // --- 常量定义 ---
4
+ const ALGORITHM = 'RSA-SHA256';
5
+ const INPUT_ENCODING = 'utf8';
6
+ const SIGNATURE_FORMAT = 'base64';
7
+
8
+ /**
9
+ * 🔐 RSA 签名
10
+ * 使用 'RSA-SHA256' 算法,将输入文本进行签名。
11
+ *
12
+ * @param {Object} params
13
+ * @param {string} params.text - 要签名的明文数据。
14
+ * @param {string} params.privateKey - PEM 格式的私钥。
15
+ * @returns {string} Base64 格式的签名结果。
16
+ * @throws {Error} 如果签名失败(如密钥无效或参数缺失)。
17
+ */
18
+ function rsaSign({ text, privateKey }) {
19
+ if (!text || !privateKey) {
20
+ throw new Error("Missing required parameters for signing: text or privateKey.");
21
+ }
22
+
23
+ try {
24
+ // 使用 const 声明,保持不变性
25
+ const signer = crypto.createSign(ALGORITHM);
26
+
27
+ // 优化:将数据直接传递给 update,无需再调用 end()
28
+ signer.update(text, INPUT_ENCODING);
29
+
30
+ // 签名,并指定私钥和输出格式
31
+ // 'base64' 是默认格式,但显式指定更清晰
32
+ const signature = signer.sign(privateKey, SIGNATURE_FORMAT);
33
+
34
+ return signature;
35
+
36
+ } catch (error) {
37
+ // 捕获密钥格式错误、权限错误等
38
+ console.error(`RSA Signing Error (${ALGORITHM}):`, error.message);
39
+ throw new Error("RSA signing failed. Check private key format and validity.");
40
+ }
41
+ }
42
+
43
+ /**
44
+ * 🔓 RSA 验证签名
45
+ * 使用 'RSA-SHA256' 算法验证签名是否有效。
46
+ *
47
+ * @param {Object} params
48
+ * @param {string} params.text - 用于签名的原始明文数据。
49
+ * @param {string} params.signature - Base64 格式的签名结果。
50
+ * @param {string} params.publicKey - PEM 格式的公钥。
51
+ * @returns {boolean} 签名是否有效。
52
+ * @throws {Error} 如果验证过程发生致命错误。
53
+ */
54
+ function rsaVerify({ text, signature, publicKey }) {
55
+ if (!text || !signature || !publicKey) {
56
+ // 参数缺失时,直接返回 false 或抛出错误,这里选择返回 false 兼容原逻辑,但推荐抛出错误
57
+ console.warn("Missing required parameters for verification.");
58
+ return false;
59
+ }
60
+
61
+ try {
62
+ // 使用 const 声明
63
+ const verifier = crypto.createVerify(ALGORITHM);
64
+
65
+ // 优化:将数据直接传递给 update
66
+ verifier.update(text, INPUT_ENCODING);
67
+
68
+ // 验证签名
69
+ // signature 已经是 Base64 格式的 Buffer,不需要再调用 Buffer.from() 转换
70
+ // verifier.verify 会自动处理公钥和签名格式
71
+ return verifier.verify(
72
+ publicKey,
73
+ signature,
74
+ SIGNATURE_FORMAT // 指定签名的输入格式
75
+ );
76
+
77
+ } catch (error) {
78
+ // 捕获公钥格式错误等
79
+ console.error(`RSA Verification Error (${ALGORITHM}):`, error.message);
80
+ // 验证过程失败通常意味着配置或密钥有误,应抛出错误而不是返回 false
81
+ throw new Error("RSA verification failed due to internal error. Check public key format and validity.");
82
+ }
83
+ }
84
+
85
+ export default {
86
+ rsaSign, // 优化后的函数名,更简洁
87
+ rsaVerify
88
+ }