@alibarbar/common 1.1.2 → 1.1.4

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/dist/crypto.d.mts CHANGED
@@ -1,12 +1,9 @@
1
+ import CryptoJS from 'crypto-js';
2
+
1
3
  /**
2
4
  * 加密工具函数
3
5
  */
4
- /**
5
- * SHA256加密
6
- * @param data - 要加密的数据(字符串或ArrayBuffer)
7
- * @returns Promise<string> 加密后的十六进制字符串
8
- */
9
- declare function sha256(data: string | ArrayBuffer): Promise<string>;
6
+
10
7
  /**
11
8
  * Base64编码
12
9
  * @param data - 要编码的数据(字符串或ArrayBuffer)
@@ -14,126 +11,76 @@ declare function sha256(data: string | ArrayBuffer): Promise<string>;
14
11
  */
15
12
  declare function base64Encode(data: string | ArrayBuffer): string;
16
13
  /**
17
- * Base64解码
18
- * @param data - Base64编码字符串
19
- * @returns 解码后的字符串
14
+ * 生成随机 AES 密钥(字符串形式)
15
+ * 注意:仅用于对称加密场景(例如 SecureStorage),不直接作为 RSA 密钥使用
20
16
  */
21
- declare function base64Decode(data: string): string;
17
+ declare function generateRandomAESKeyString(): string;
22
18
  /**
23
- * 生成UUID v4
24
- * @returns UUID字符串
19
+ * 使用 AES 加密任意可序列化数据(内部使用 CryptoJS)
20
+ * @param data - 任意可 JSON 序列化的数据
21
+ * @param aesKey - AES 密钥字符串
22
+ * @returns 密文字符串
25
23
  */
26
- declare function generateUUID(): string;
24
+ declare function encryptJsonWithAES(data: unknown, aesKey: string): string;
27
25
  /**
28
- * 生成随机字符串
29
- * @param length - 字符串长度
30
- * @param charset - 字符集,默认为 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
31
- * @returns 随机字符串
26
+ * 使用 AES 解密为 JSON 数据(内部使用 CryptoJS)
27
+ * @param encryptedData - 密文字符串
28
+ * @param aesKey - AES 密钥字符串
29
+ * @returns 还原后的数据
32
30
  */
33
- declare function generateRandomString(length: number, charset?: string): string;
31
+ declare function decryptJsonWithAES<T = unknown>(encryptedData: string, aesKey: string): T;
34
32
  /**
35
- * 通用哈希函数(简单实现)
36
- * @param data - 要哈希的数据
37
- * @returns 哈希值
33
+ * 字符串形式的 RSA 密钥对(用于浏览器端加解密)
34
+ * - publicKey / privateKey 均为字符串(PEM 或 Base64 格式)
35
+ * - 主要用于生成和配置环境变量(例如 VITE_RSA_PUBLIC_KEY / VITE_RSA_PRIVATE_KEY)
38
36
  */
39
- declare function hash(data: string): number;
40
- /**
41
- * RSA密钥对类型
42
- */
43
- interface RSAKeyPair {
44
- publicKey: CryptoKey;
45
- privateKey: CryptoKey;
37
+ interface RSAKeyPairStrings {
38
+ publicKey: string;
39
+ privateKey: string;
46
40
  }
47
41
  /**
48
- * 生成RSA密钥对
49
- * @param modulusLength - 密钥长度,默认为 2048
50
- * @returns Promise<RSAKeyPair> RSA密钥对
51
- */
52
- declare function generateRSAKeyPair(modulusLength?: number): Promise<RSAKeyPair>;
53
- /**
54
- * RSA加密
55
- * @param data - 要加密的数据(字符串)
56
- * @param publicKey - 公钥
57
- * @returns Promise<string> Base64编码的加密数据
58
- */
59
- declare function rsaEncrypt(data: string, publicKey: CryptoKey): Promise<string>;
60
- /**
61
- * RSA解密
62
- * @param encryptedData - Base64编码的加密数据
63
- * @param privateKey - 私钥
64
- * @returns Promise<string> 解密后的字符串
65
- */
66
- declare function rsaDecrypt(encryptedData: string, privateKey: CryptoKey): Promise<string>;
67
- /**
68
- * 导出公钥为Base64字符串
69
- * @param publicKey - 公钥
70
- * @returns Promise<string> Base64编码的公钥
71
- */
72
- declare function exportPublicKey(publicKey: CryptoKey): Promise<string>;
73
- /**
74
- * 导出私钥为Base64字符串
75
- * @param privateKey - 私钥
76
- * @returns Promise<string> Base64编码的私钥
77
- */
78
- declare function exportPrivateKey(privateKey: CryptoKey): Promise<string>;
79
- /**
80
- * 从Base64字符串导入公钥
81
- * @param keyData - Base64编码的公钥
82
- * @returns Promise<CryptoKey> 公钥对象
42
+ * 从环境变量中读取 RSA 公钥
83
43
  */
84
- declare function importPublicKey(keyData: string): Promise<CryptoKey>;
44
+ declare function getEnvPublicKey(): string;
85
45
  /**
86
- * 从Base64字符串导入私钥
87
- * @param keyData - Base64编码的私钥
88
- * @returns Promise<CryptoKey> 私钥对象
46
+ * 从环境变量中读取 RSA 私钥
89
47
  */
90
- declare function importPrivateKey(keyData: string): Promise<CryptoKey>;
48
+ declare function getEnvPrivateKey(): string;
91
49
  /**
92
- * 生成 HMAC 密钥
93
- * @returns Promise<CryptoKey> HMAC 密钥
50
+ * 使用指定公钥进行 RSA 加密(内部工具函数)
94
51
  */
95
- declare function generateHMACKey(): Promise<CryptoKey>;
52
+ declare function rsaEncrypt(plain: string, publicKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
96
53
  /**
97
- * 计算 HMAC 签名
98
- * @param data - 要签名的数据(字符串或ArrayBuffer)
99
- * @param key - HMAC密钥
100
- * @returns Promise<string> Base64编码的HMAC签名
54
+ * 使用指定私钥进行 RSA 解密(内部工具函数)
101
55
  */
102
- declare function computeHMAC(data: string | ArrayBuffer, key: CryptoKey): Promise<string>;
56
+ declare function rsaDecrypt(cipher: string, privateKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
103
57
  /**
104
- * 验证 HMAC 签名
105
- * @param data - 原始数据(字符串或ArrayBuffer)
106
- * @param signature - Base64编码的HMAC签名
107
- * @param key - HMAC密钥
108
- * @returns Promise<boolean> 签名是否有效
58
+ * 使用环境变量中的公钥进行 RSA 加密
109
59
  */
110
- declare function verifyHMAC(data: string | ArrayBuffer, signature: string, key: CryptoKey): Promise<boolean>;
60
+ declare function encryptWithEnvPublicKey(plain: string): Promise<string>;
111
61
  /**
112
- * 使用 PBKDF2 派生密钥
113
- * @param password - 密码(字符串)
114
- * @param salt - 盐值(ArrayBuffer 或字符串)
115
- * @param iterations - 迭代次数,默认为 100000
116
- * @param keyLength - 密钥长度(位),默认为 256
117
- * @returns Promise<CryptoKey> 派生的密钥
62
+ * 使用环境变量中的私钥进行 RSA 解密
118
63
  */
119
- declare function deriveKeyFromPassword(password: string, salt: ArrayBuffer | string, iterations?: number, keyLength?: number): Promise<CryptoKey>;
64
+ declare function decryptWithEnvPrivateKey(cipher: string): Promise<string>;
120
65
  /**
121
- * 使用 AES-GCM 加密数据
122
- * @param data - 要加密的数据(字符串或ArrayBuffer)
123
- * @param key - AES密钥
124
- * @returns Promise<{encrypted: string, iv: string}> 加密后的数据和初始向量(Base64编码)
66
+ * 生成 RSA 密钥对(导出为 PEM 格式字符串)
67
+ * 注意:JSEncrypt 不支持直接生成密钥对,需要使用 WebCrypto 生成后导出为 PEM
68
+ * 该函数仅用于在构建/初始化阶段生成一对密钥,供配置到环境变量中使用
125
69
  */
126
- declare function aesGCMEncrypt(data: string | ArrayBuffer, key: CryptoKey): Promise<{
127
- encrypted: string;
128
- iv: string;
70
+ declare function generateRSAKeyPair(modulusLength?: number): Promise<{
71
+ publicKey: string;
72
+ privateKey: string;
129
73
  }>;
130
74
  /**
131
- * 使用 AES-GCM 解密数据
132
- * @param encryptedData - Base64编码的加密数据
133
- * @param iv - Base64编码的初始向量
134
- * @param key - AES密钥
135
- * @returns Promise<string> 解密后的字符串
75
+ * 默认导出一个简单的函数集合,方便按实例方式使用(非必须)
76
+ * - encrypt: 使用环境变量公钥加密
77
+ * - decrypt: 使用环境变量私钥解密
136
78
  */
137
- declare function aesGCMDecrypt(encryptedData: string, iv: string, key: CryptoKey): Promise<string>;
79
+ declare const rsaEnv: {
80
+ encrypt: typeof encryptWithEnvPublicKey;
81
+ decrypt: typeof decryptWithEnvPrivateKey;
82
+ getPublicKey: typeof getEnvPublicKey;
83
+ getPrivateKey: typeof getEnvPrivateKey;
84
+ };
138
85
 
139
- export { type RSAKeyPair, aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, computeHMAC, deriveKeyFromPassword, exportPrivateKey, exportPublicKey, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256, verifyHMAC };
86
+ export { type RSAKeyPairStrings, base64Encode, decryptJsonWithAES, decryptWithEnvPrivateKey, rsaEnv as default, encryptJsonWithAES, encryptWithEnvPublicKey, generateRSAKeyPair, generateRandomAESKeyString, getEnvPrivateKey, getEnvPublicKey, rsaDecrypt, rsaEncrypt };
package/dist/crypto.d.ts CHANGED
@@ -1,12 +1,9 @@
1
+ import CryptoJS from 'crypto-js';
2
+
1
3
  /**
2
4
  * 加密工具函数
3
5
  */
4
- /**
5
- * SHA256加密
6
- * @param data - 要加密的数据(字符串或ArrayBuffer)
7
- * @returns Promise<string> 加密后的十六进制字符串
8
- */
9
- declare function sha256(data: string | ArrayBuffer): Promise<string>;
6
+
10
7
  /**
11
8
  * Base64编码
12
9
  * @param data - 要编码的数据(字符串或ArrayBuffer)
@@ -14,126 +11,76 @@ declare function sha256(data: string | ArrayBuffer): Promise<string>;
14
11
  */
15
12
  declare function base64Encode(data: string | ArrayBuffer): string;
16
13
  /**
17
- * Base64解码
18
- * @param data - Base64编码字符串
19
- * @returns 解码后的字符串
14
+ * 生成随机 AES 密钥(字符串形式)
15
+ * 注意:仅用于对称加密场景(例如 SecureStorage),不直接作为 RSA 密钥使用
20
16
  */
21
- declare function base64Decode(data: string): string;
17
+ declare function generateRandomAESKeyString(): string;
22
18
  /**
23
- * 生成UUID v4
24
- * @returns UUID字符串
19
+ * 使用 AES 加密任意可序列化数据(内部使用 CryptoJS)
20
+ * @param data - 任意可 JSON 序列化的数据
21
+ * @param aesKey - AES 密钥字符串
22
+ * @returns 密文字符串
25
23
  */
26
- declare function generateUUID(): string;
24
+ declare function encryptJsonWithAES(data: unknown, aesKey: string): string;
27
25
  /**
28
- * 生成随机字符串
29
- * @param length - 字符串长度
30
- * @param charset - 字符集,默认为 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
31
- * @returns 随机字符串
26
+ * 使用 AES 解密为 JSON 数据(内部使用 CryptoJS)
27
+ * @param encryptedData - 密文字符串
28
+ * @param aesKey - AES 密钥字符串
29
+ * @returns 还原后的数据
32
30
  */
33
- declare function generateRandomString(length: number, charset?: string): string;
31
+ declare function decryptJsonWithAES<T = unknown>(encryptedData: string, aesKey: string): T;
34
32
  /**
35
- * 通用哈希函数(简单实现)
36
- * @param data - 要哈希的数据
37
- * @returns 哈希值
33
+ * 字符串形式的 RSA 密钥对(用于浏览器端加解密)
34
+ * - publicKey / privateKey 均为字符串(PEM 或 Base64 格式)
35
+ * - 主要用于生成和配置环境变量(例如 VITE_RSA_PUBLIC_KEY / VITE_RSA_PRIVATE_KEY)
38
36
  */
39
- declare function hash(data: string): number;
40
- /**
41
- * RSA密钥对类型
42
- */
43
- interface RSAKeyPair {
44
- publicKey: CryptoKey;
45
- privateKey: CryptoKey;
37
+ interface RSAKeyPairStrings {
38
+ publicKey: string;
39
+ privateKey: string;
46
40
  }
47
41
  /**
48
- * 生成RSA密钥对
49
- * @param modulusLength - 密钥长度,默认为 2048
50
- * @returns Promise<RSAKeyPair> RSA密钥对
51
- */
52
- declare function generateRSAKeyPair(modulusLength?: number): Promise<RSAKeyPair>;
53
- /**
54
- * RSA加密
55
- * @param data - 要加密的数据(字符串)
56
- * @param publicKey - 公钥
57
- * @returns Promise<string> Base64编码的加密数据
58
- */
59
- declare function rsaEncrypt(data: string, publicKey: CryptoKey): Promise<string>;
60
- /**
61
- * RSA解密
62
- * @param encryptedData - Base64编码的加密数据
63
- * @param privateKey - 私钥
64
- * @returns Promise<string> 解密后的字符串
65
- */
66
- declare function rsaDecrypt(encryptedData: string, privateKey: CryptoKey): Promise<string>;
67
- /**
68
- * 导出公钥为Base64字符串
69
- * @param publicKey - 公钥
70
- * @returns Promise<string> Base64编码的公钥
71
- */
72
- declare function exportPublicKey(publicKey: CryptoKey): Promise<string>;
73
- /**
74
- * 导出私钥为Base64字符串
75
- * @param privateKey - 私钥
76
- * @returns Promise<string> Base64编码的私钥
77
- */
78
- declare function exportPrivateKey(privateKey: CryptoKey): Promise<string>;
79
- /**
80
- * 从Base64字符串导入公钥
81
- * @param keyData - Base64编码的公钥
82
- * @returns Promise<CryptoKey> 公钥对象
42
+ * 从环境变量中读取 RSA 公钥
83
43
  */
84
- declare function importPublicKey(keyData: string): Promise<CryptoKey>;
44
+ declare function getEnvPublicKey(): string;
85
45
  /**
86
- * 从Base64字符串导入私钥
87
- * @param keyData - Base64编码的私钥
88
- * @returns Promise<CryptoKey> 私钥对象
46
+ * 从环境变量中读取 RSA 私钥
89
47
  */
90
- declare function importPrivateKey(keyData: string): Promise<CryptoKey>;
48
+ declare function getEnvPrivateKey(): string;
91
49
  /**
92
- * 生成 HMAC 密钥
93
- * @returns Promise<CryptoKey> HMAC 密钥
50
+ * 使用指定公钥进行 RSA 加密(内部工具函数)
94
51
  */
95
- declare function generateHMACKey(): Promise<CryptoKey>;
52
+ declare function rsaEncrypt(plain: string, publicKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
96
53
  /**
97
- * 计算 HMAC 签名
98
- * @param data - 要签名的数据(字符串或ArrayBuffer)
99
- * @param key - HMAC密钥
100
- * @returns Promise<string> Base64编码的HMAC签名
54
+ * 使用指定私钥进行 RSA 解密(内部工具函数)
101
55
  */
102
- declare function computeHMAC(data: string | ArrayBuffer, key: CryptoKey): Promise<string>;
56
+ declare function rsaDecrypt(cipher: string, privateKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
103
57
  /**
104
- * 验证 HMAC 签名
105
- * @param data - 原始数据(字符串或ArrayBuffer)
106
- * @param signature - Base64编码的HMAC签名
107
- * @param key - HMAC密钥
108
- * @returns Promise<boolean> 签名是否有效
58
+ * 使用环境变量中的公钥进行 RSA 加密
109
59
  */
110
- declare function verifyHMAC(data: string | ArrayBuffer, signature: string, key: CryptoKey): Promise<boolean>;
60
+ declare function encryptWithEnvPublicKey(plain: string): Promise<string>;
111
61
  /**
112
- * 使用 PBKDF2 派生密钥
113
- * @param password - 密码(字符串)
114
- * @param salt - 盐值(ArrayBuffer 或字符串)
115
- * @param iterations - 迭代次数,默认为 100000
116
- * @param keyLength - 密钥长度(位),默认为 256
117
- * @returns Promise<CryptoKey> 派生的密钥
62
+ * 使用环境变量中的私钥进行 RSA 解密
118
63
  */
119
- declare function deriveKeyFromPassword(password: string, salt: ArrayBuffer | string, iterations?: number, keyLength?: number): Promise<CryptoKey>;
64
+ declare function decryptWithEnvPrivateKey(cipher: string): Promise<string>;
120
65
  /**
121
- * 使用 AES-GCM 加密数据
122
- * @param data - 要加密的数据(字符串或ArrayBuffer)
123
- * @param key - AES密钥
124
- * @returns Promise<{encrypted: string, iv: string}> 加密后的数据和初始向量(Base64编码)
66
+ * 生成 RSA 密钥对(导出为 PEM 格式字符串)
67
+ * 注意:JSEncrypt 不支持直接生成密钥对,需要使用 WebCrypto 生成后导出为 PEM
68
+ * 该函数仅用于在构建/初始化阶段生成一对密钥,供配置到环境变量中使用
125
69
  */
126
- declare function aesGCMEncrypt(data: string | ArrayBuffer, key: CryptoKey): Promise<{
127
- encrypted: string;
128
- iv: string;
70
+ declare function generateRSAKeyPair(modulusLength?: number): Promise<{
71
+ publicKey: string;
72
+ privateKey: string;
129
73
  }>;
130
74
  /**
131
- * 使用 AES-GCM 解密数据
132
- * @param encryptedData - Base64编码的加密数据
133
- * @param iv - Base64编码的初始向量
134
- * @param key - AES密钥
135
- * @returns Promise<string> 解密后的字符串
75
+ * 默认导出一个简单的函数集合,方便按实例方式使用(非必须)
76
+ * - encrypt: 使用环境变量公钥加密
77
+ * - decrypt: 使用环境变量私钥解密
136
78
  */
137
- declare function aesGCMDecrypt(encryptedData: string, iv: string, key: CryptoKey): Promise<string>;
79
+ declare const rsaEnv: {
80
+ encrypt: typeof encryptWithEnvPublicKey;
81
+ decrypt: typeof decryptWithEnvPrivateKey;
82
+ getPublicKey: typeof getEnvPublicKey;
83
+ getPrivateKey: typeof getEnvPrivateKey;
84
+ };
138
85
 
139
- export { type RSAKeyPair, aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, computeHMAC, deriveKeyFromPassword, exportPrivateKey, exportPublicKey, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256, verifyHMAC };
86
+ export { type RSAKeyPairStrings, base64Encode, decryptJsonWithAES, decryptWithEnvPrivateKey, rsaEnv as default, encryptJsonWithAES, encryptWithEnvPublicKey, generateRSAKeyPair, generateRandomAESKeyString, getEnvPrivateKey, getEnvPublicKey, rsaDecrypt, rsaEncrypt };