@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/README.md +10 -0
- package/dist/crypto.cjs +184 -252
- package/dist/crypto.d.mts +50 -103
- package/dist/crypto.d.ts +50 -103
- package/dist/crypto.js +170 -238
- package/dist/{index-DchqyDBQ.d.mts → index-j5EqxJaC.d.mts} +8 -2
- package/dist/{index-DchqyDBQ.d.ts → index-j5EqxJaC.d.ts} +8 -2
- package/dist/index.cjs +488 -1008
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +479 -986
- package/dist/services.cjs +82 -60
- package/dist/services.d.mts +1 -1
- package/dist/services.d.ts +1 -1
- package/dist/services.js +82 -60
- package/dist/storage.cjs +328 -894
- package/dist/storage.d.mts +69 -127
- package/dist/storage.d.ts +69 -127
- package/dist/storage.js +320 -887
- package/dist/upload.cjs +56 -14
- package/dist/upload.d.mts +1 -1
- package/dist/upload.d.ts +1 -1
- package/dist/upload.js +56 -14
- package/package.json +11 -3
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
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @returns 解码后的字符串
|
|
14
|
+
* 生成随机 AES 密钥(字符串形式)
|
|
15
|
+
* 注意:仅用于对称加密场景(例如 SecureStorage),不直接作为 RSA 密钥使用
|
|
20
16
|
*/
|
|
21
|
-
declare function
|
|
17
|
+
declare function generateRandomAESKeyString(): string;
|
|
22
18
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @
|
|
19
|
+
* 使用 AES 加密任意可序列化数据(内部使用 CryptoJS)
|
|
20
|
+
* @param data - 任意可 JSON 序列化的数据
|
|
21
|
+
* @param aesKey - AES 密钥字符串
|
|
22
|
+
* @returns 密文字符串
|
|
25
23
|
*/
|
|
26
|
-
declare function
|
|
24
|
+
declare function encryptJsonWithAES(data: unknown, aesKey: string): string;
|
|
27
25
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @param
|
|
30
|
-
* @param
|
|
31
|
-
* @returns
|
|
26
|
+
* 使用 AES 解密为 JSON 数据(内部使用 CryptoJS)
|
|
27
|
+
* @param encryptedData - 密文字符串
|
|
28
|
+
* @param aesKey - AES 密钥字符串
|
|
29
|
+
* @returns 还原后的数据
|
|
32
30
|
*/
|
|
33
|
-
declare function
|
|
31
|
+
declare function decryptJsonWithAES<T = unknown>(encryptedData: string, aesKey: string): T;
|
|
34
32
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
33
|
+
* 字符串形式的 RSA 密钥对(用于浏览器端加解密)
|
|
34
|
+
* - publicKey / privateKey 均为字符串(PEM 或 Base64 格式)
|
|
35
|
+
* - 主要用于生成和配置环境变量(例如 VITE_RSA_PUBLIC_KEY / VITE_RSA_PRIVATE_KEY)
|
|
38
36
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
*/
|
|
43
|
-
interface RSAKeyPair {
|
|
44
|
-
publicKey: CryptoKey;
|
|
45
|
-
privateKey: CryptoKey;
|
|
37
|
+
interface RSAKeyPairStrings {
|
|
38
|
+
publicKey: string;
|
|
39
|
+
privateKey: string;
|
|
46
40
|
}
|
|
47
41
|
/**
|
|
48
|
-
*
|
|
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
|
|
44
|
+
declare function getEnvPublicKey(): string;
|
|
85
45
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @param keyData - Base64编码的私钥
|
|
88
|
-
* @returns Promise<CryptoKey> 私钥对象
|
|
46
|
+
* 从环境变量中读取 RSA 私钥
|
|
89
47
|
*/
|
|
90
|
-
declare function
|
|
48
|
+
declare function getEnvPrivateKey(): string;
|
|
91
49
|
/**
|
|
92
|
-
*
|
|
93
|
-
* @returns Promise<CryptoKey> HMAC 密钥
|
|
50
|
+
* 使用指定公钥进行 RSA 加密(内部工具函数)
|
|
94
51
|
*/
|
|
95
|
-
declare function
|
|
52
|
+
declare function rsaEncrypt(plain: string, publicKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
|
|
96
53
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param data - 要签名的数据(字符串或ArrayBuffer)
|
|
99
|
-
* @param key - HMAC密钥
|
|
100
|
-
* @returns Promise<string> Base64编码的HMAC签名
|
|
54
|
+
* 使用指定私钥进行 RSA 解密(内部工具函数)
|
|
101
55
|
*/
|
|
102
|
-
declare function
|
|
56
|
+
declare function rsaDecrypt(cipher: string, privateKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
|
|
103
57
|
/**
|
|
104
|
-
*
|
|
105
|
-
* @param data - 原始数据(字符串或ArrayBuffer)
|
|
106
|
-
* @param signature - Base64编码的HMAC签名
|
|
107
|
-
* @param key - HMAC密钥
|
|
108
|
-
* @returns Promise<boolean> 签名是否有效
|
|
58
|
+
* 使用环境变量中的公钥进行 RSA 加密
|
|
109
59
|
*/
|
|
110
|
-
declare function
|
|
60
|
+
declare function encryptWithEnvPublicKey(plain: string): Promise<string>;
|
|
111
61
|
/**
|
|
112
|
-
*
|
|
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
|
|
64
|
+
declare function decryptWithEnvPrivateKey(cipher: string): Promise<string>;
|
|
120
65
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @returns Promise<{encrypted: string, iv: string}> 加密后的数据和初始向量(Base64编码)
|
|
66
|
+
* 生成 RSA 密钥对(导出为 PEM 格式字符串)
|
|
67
|
+
* 注意:JSEncrypt 不支持直接生成密钥对,需要使用 WebCrypto 生成后导出为 PEM
|
|
68
|
+
* 该函数仅用于在构建/初始化阶段生成一对密钥,供配置到环境变量中使用
|
|
125
69
|
*/
|
|
126
|
-
declare function
|
|
127
|
-
|
|
128
|
-
|
|
70
|
+
declare function generateRSAKeyPair(modulusLength?: number): Promise<{
|
|
71
|
+
publicKey: string;
|
|
72
|
+
privateKey: string;
|
|
129
73
|
}>;
|
|
130
74
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* @param key - AES密钥
|
|
135
|
-
* @returns Promise<string> 解密后的字符串
|
|
75
|
+
* 默认导出一个简单的函数集合,方便按实例方式使用(非必须)
|
|
76
|
+
* - encrypt: 使用环境变量公钥加密
|
|
77
|
+
* - decrypt: 使用环境变量私钥解密
|
|
136
78
|
*/
|
|
137
|
-
declare
|
|
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
|
|
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
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @returns 解码后的字符串
|
|
14
|
+
* 生成随机 AES 密钥(字符串形式)
|
|
15
|
+
* 注意:仅用于对称加密场景(例如 SecureStorage),不直接作为 RSA 密钥使用
|
|
20
16
|
*/
|
|
21
|
-
declare function
|
|
17
|
+
declare function generateRandomAESKeyString(): string;
|
|
22
18
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @
|
|
19
|
+
* 使用 AES 加密任意可序列化数据(内部使用 CryptoJS)
|
|
20
|
+
* @param data - 任意可 JSON 序列化的数据
|
|
21
|
+
* @param aesKey - AES 密钥字符串
|
|
22
|
+
* @returns 密文字符串
|
|
25
23
|
*/
|
|
26
|
-
declare function
|
|
24
|
+
declare function encryptJsonWithAES(data: unknown, aesKey: string): string;
|
|
27
25
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @param
|
|
30
|
-
* @param
|
|
31
|
-
* @returns
|
|
26
|
+
* 使用 AES 解密为 JSON 数据(内部使用 CryptoJS)
|
|
27
|
+
* @param encryptedData - 密文字符串
|
|
28
|
+
* @param aesKey - AES 密钥字符串
|
|
29
|
+
* @returns 还原后的数据
|
|
32
30
|
*/
|
|
33
|
-
declare function
|
|
31
|
+
declare function decryptJsonWithAES<T = unknown>(encryptedData: string, aesKey: string): T;
|
|
34
32
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
33
|
+
* 字符串形式的 RSA 密钥对(用于浏览器端加解密)
|
|
34
|
+
* - publicKey / privateKey 均为字符串(PEM 或 Base64 格式)
|
|
35
|
+
* - 主要用于生成和配置环境变量(例如 VITE_RSA_PUBLIC_KEY / VITE_RSA_PRIVATE_KEY)
|
|
38
36
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
*/
|
|
43
|
-
interface RSAKeyPair {
|
|
44
|
-
publicKey: CryptoKey;
|
|
45
|
-
privateKey: CryptoKey;
|
|
37
|
+
interface RSAKeyPairStrings {
|
|
38
|
+
publicKey: string;
|
|
39
|
+
privateKey: string;
|
|
46
40
|
}
|
|
47
41
|
/**
|
|
48
|
-
*
|
|
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
|
|
44
|
+
declare function getEnvPublicKey(): string;
|
|
85
45
|
/**
|
|
86
|
-
*
|
|
87
|
-
* @param keyData - Base64编码的私钥
|
|
88
|
-
* @returns Promise<CryptoKey> 私钥对象
|
|
46
|
+
* 从环境变量中读取 RSA 私钥
|
|
89
47
|
*/
|
|
90
|
-
declare function
|
|
48
|
+
declare function getEnvPrivateKey(): string;
|
|
91
49
|
/**
|
|
92
|
-
*
|
|
93
|
-
* @returns Promise<CryptoKey> HMAC 密钥
|
|
50
|
+
* 使用指定公钥进行 RSA 加密(内部工具函数)
|
|
94
51
|
*/
|
|
95
|
-
declare function
|
|
52
|
+
declare function rsaEncrypt(plain: string, publicKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
|
|
96
53
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param data - 要签名的数据(字符串或ArrayBuffer)
|
|
99
|
-
* @param key - HMAC密钥
|
|
100
|
-
* @returns Promise<string> Base64编码的HMAC签名
|
|
54
|
+
* 使用指定私钥进行 RSA 解密(内部工具函数)
|
|
101
55
|
*/
|
|
102
|
-
declare function
|
|
56
|
+
declare function rsaDecrypt(cipher: string, privateKeyPem: string | CryptoJS.lib.WordArray | CryptoKey): Promise<string>;
|
|
103
57
|
/**
|
|
104
|
-
*
|
|
105
|
-
* @param data - 原始数据(字符串或ArrayBuffer)
|
|
106
|
-
* @param signature - Base64编码的HMAC签名
|
|
107
|
-
* @param key - HMAC密钥
|
|
108
|
-
* @returns Promise<boolean> 签名是否有效
|
|
58
|
+
* 使用环境变量中的公钥进行 RSA 加密
|
|
109
59
|
*/
|
|
110
|
-
declare function
|
|
60
|
+
declare function encryptWithEnvPublicKey(plain: string): Promise<string>;
|
|
111
61
|
/**
|
|
112
|
-
*
|
|
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
|
|
64
|
+
declare function decryptWithEnvPrivateKey(cipher: string): Promise<string>;
|
|
120
65
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @returns Promise<{encrypted: string, iv: string}> 加密后的数据和初始向量(Base64编码)
|
|
66
|
+
* 生成 RSA 密钥对(导出为 PEM 格式字符串)
|
|
67
|
+
* 注意:JSEncrypt 不支持直接生成密钥对,需要使用 WebCrypto 生成后导出为 PEM
|
|
68
|
+
* 该函数仅用于在构建/初始化阶段生成一对密钥,供配置到环境变量中使用
|
|
125
69
|
*/
|
|
126
|
-
declare function
|
|
127
|
-
|
|
128
|
-
|
|
70
|
+
declare function generateRSAKeyPair(modulusLength?: number): Promise<{
|
|
71
|
+
publicKey: string;
|
|
72
|
+
privateKey: string;
|
|
129
73
|
}>;
|
|
130
74
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* @param key - AES密钥
|
|
135
|
-
* @returns Promise<string> 解密后的字符串
|
|
75
|
+
* 默认导出一个简单的函数集合,方便按实例方式使用(非必须)
|
|
76
|
+
* - encrypt: 使用环境变量公钥加密
|
|
77
|
+
* - decrypt: 使用环境变量私钥解密
|
|
136
78
|
*/
|
|
137
|
-
declare
|
|
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
|
|
86
|
+
export { type RSAKeyPairStrings, base64Encode, decryptJsonWithAES, decryptWithEnvPrivateKey, rsaEnv as default, encryptJsonWithAES, encryptWithEnvPublicKey, generateRSAKeyPair, generateRandomAESKeyString, getEnvPrivateKey, getEnvPublicKey, rsaDecrypt, rsaEncrypt };
|