@alibarbar/common 1.0.9 → 1.1.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/dist/{chunk-O3O67R4I.js → algorithm.cjs} +1 -3
- package/dist/algorithm.js +130 -44
- package/dist/{chunk-LOAZSPGQ.js → array.cjs} +1 -3
- package/dist/array.js +135 -84
- package/dist/{chunk-Y364QIQH.js → color.cjs} +1 -3
- package/dist/color.js +127 -40
- package/dist/{chunk-DYBSRI7V.js → crypto.cjs} +109 -3
- package/dist/crypto.d.mts +48 -1
- package/dist/crypto.d.ts +48 -1
- package/dist/crypto.js +274 -60
- package/dist/{chunk-BHCRFURU.js → data-structure.cjs} +1 -3
- package/dist/data-structure.js +481 -32
- package/dist/{chunk-I3L42475.js → date.cjs} +1 -3
- package/dist/date.js +125 -72
- package/dist/{chunk-JK2SE3I2.js → dom.cjs} +1 -3
- package/dist/dom.js +84 -56
- package/dist/{chunk-7V5UQXIO.js → file.cjs} +1 -3
- package/dist/file.js +79 -32
- package/dist/{chunk-OX5PLOWB.js → i18n.cjs} +1 -3
- package/dist/i18n.js +78 -40
- package/dist/index.cjs +4543 -0
- package/dist/index.d.mts +6 -162
- package/dist/index.d.ts +6 -162
- package/dist/index.js +3712 -17374
- package/dist/{chunk-4RGXV4SJ.js → network.cjs} +1 -3
- package/dist/network.js +97 -28
- package/dist/{chunk-3FRYJPHG.js → number.cjs} +1 -3
- package/dist/number.js +77 -56
- package/dist/{chunk-27UDDVLZ.js → object.cjs} +1 -3
- package/dist/object.js +237 -80
- package/dist/{chunk-JQZBPAPO.js → performance.cjs} +1 -3
- package/dist/performance.js +145 -40
- package/dist/services.cjs +291 -0
- package/dist/services.d.mts +161 -0
- package/dist/services.d.ts +161 -0
- package/dist/services.js +281 -0
- package/dist/storage.cjs +943 -0
- package/dist/storage.d.mts +46 -1
- package/dist/storage.d.ts +46 -1
- package/dist/storage.js +930 -32
- package/dist/{chunk-D7CS5EKF.js → string.cjs} +1 -3
- package/dist/string.js +91 -68
- package/dist/{chunk-56W6YECK.js → tracking.cjs} +1 -3
- package/dist/tracking.js +359 -52
- package/dist/{chunk-ZDMFMUDR.js → transform.cjs} +1 -3
- package/dist/transform.js +299 -32
- package/dist/{chunk-NJARVI6X.mjs → upload.cjs} +42 -15
- package/dist/upload.d.mts +1 -1
- package/dist/upload.d.ts +1 -1
- package/dist/upload.js +402 -20
- package/dist/{chunk-KGFTD255.js → url.cjs} +1 -3
- package/dist/url.js +91 -44
- package/dist/{chunk-TQN37HIN.js → validation.cjs} +1 -3
- package/dist/validation.js +77 -60
- package/package.json +7 -2
- package/dist/algorithm.js.map +0 -1
- package/dist/algorithm.mjs +0 -4
- package/dist/algorithm.mjs.map +0 -1
- package/dist/array.js.map +0 -1
- package/dist/array.mjs +0 -4
- package/dist/array.mjs.map +0 -1
- package/dist/chunk-27UDDVLZ.js.map +0 -1
- package/dist/chunk-2R2QWFJC.mjs +0 -138
- package/dist/chunk-2R2QWFJC.mjs.map +0 -1
- package/dist/chunk-3FRYJPHG.js.map +0 -1
- package/dist/chunk-4RGXV4SJ.js.map +0 -1
- package/dist/chunk-56W6YECK.js.map +0 -1
- package/dist/chunk-5BGSUGTI.mjs +0 -128
- package/dist/chunk-5BGSUGTI.mjs.map +0 -1
- package/dist/chunk-7E6GELHJ.mjs +0 -302
- package/dist/chunk-7E6GELHJ.mjs.map +0 -1
- package/dist/chunk-7V5UQXIO.js.map +0 -1
- package/dist/chunk-A4SWQXX7.mjs +0 -484
- package/dist/chunk-A4SWQXX7.mjs.map +0 -1
- package/dist/chunk-BHCRFURU.js.map +0 -1
- package/dist/chunk-CDSGEAOK.mjs +0 -80
- package/dist/chunk-CDSGEAOK.mjs.map +0 -1
- package/dist/chunk-D7CS5EKF.js.map +0 -1
- package/dist/chunk-DYBSRI7V.js.map +0 -1
- package/dist/chunk-FEBKPX5A.js +0 -386
- package/dist/chunk-FEBKPX5A.js.map +0 -1
- package/dist/chunk-FJ6ZGZIA.mjs +0 -39
- package/dist/chunk-FJ6ZGZIA.mjs.map +0 -1
- package/dist/chunk-HLDFI7R2.mjs +0 -175
- package/dist/chunk-HLDFI7R2.mjs.map +0 -1
- package/dist/chunk-I3L42475.js.map +0 -1
- package/dist/chunk-JBLX27WD.mjs +0 -240
- package/dist/chunk-JBLX27WD.mjs.map +0 -1
- package/dist/chunk-JHZ7M2MR.mjs +0 -133
- package/dist/chunk-JHZ7M2MR.mjs.map +0 -1
- package/dist/chunk-JK2SE3I2.js.map +0 -1
- package/dist/chunk-JQZBPAPO.js.map +0 -1
- package/dist/chunk-JXYGC2C5.mjs +0 -100
- package/dist/chunk-JXYGC2C5.mjs.map +0 -1
- package/dist/chunk-KGFTD255.js.map +0 -1
- package/dist/chunk-LBHBNPNJ.mjs +0 -148
- package/dist/chunk-LBHBNPNJ.mjs.map +0 -1
- package/dist/chunk-LF4CILQS.mjs +0 -87
- package/dist/chunk-LF4CILQS.mjs.map +0 -1
- package/dist/chunk-LOAZSPGQ.js.map +0 -1
- package/dist/chunk-NJARVI6X.mjs.map +0 -1
- package/dist/chunk-NSSDYX2U.mjs +0 -80
- package/dist/chunk-NSSDYX2U.mjs.map +0 -1
- package/dist/chunk-O3O67R4I.js.map +0 -1
- package/dist/chunk-OIXQ3E6W.mjs +0 -354
- package/dist/chunk-OIXQ3E6W.mjs.map +0 -1
- package/dist/chunk-OX5PLOWB.js.map +0 -1
- package/dist/chunk-PJ7UCTX4.mjs +0 -362
- package/dist/chunk-PJ7UCTX4.mjs.map +0 -1
- package/dist/chunk-PR4QN5HX.js +0 -44
- package/dist/chunk-PR4QN5HX.js.map +0 -1
- package/dist/chunk-QIBE7GVN.mjs +0 -81
- package/dist/chunk-QIBE7GVN.mjs.map +0 -1
- package/dist/chunk-QIOC54LQ.mjs +0 -130
- package/dist/chunk-QIOC54LQ.mjs.map +0 -1
- package/dist/chunk-TQN37HIN.js.map +0 -1
- package/dist/chunk-WZDOPUJW.js +0 -361
- package/dist/chunk-WZDOPUJW.js.map +0 -1
- package/dist/chunk-XJTZDXSR.mjs +0 -94
- package/dist/chunk-XJTZDXSR.mjs.map +0 -1
- package/dist/chunk-Y364QIQH.js.map +0 -1
- package/dist/chunk-YXM6Q4JS.mjs +0 -94
- package/dist/chunk-YXM6Q4JS.mjs.map +0 -1
- package/dist/chunk-ZDMFMUDR.js.map +0 -1
- package/dist/chunk-ZVJ6NQUM.mjs +0 -82
- package/dist/chunk-ZVJ6NQUM.mjs.map +0 -1
- package/dist/color.js.map +0 -1
- package/dist/color.mjs +0 -4
- package/dist/color.mjs.map +0 -1
- package/dist/crypto.js.map +0 -1
- package/dist/crypto.mjs +0 -4
- package/dist/crypto.mjs.map +0 -1
- package/dist/data-structure.js.map +0 -1
- package/dist/data-structure.mjs +0 -4
- package/dist/data-structure.mjs.map +0 -1
- package/dist/date.js.map +0 -1
- package/dist/date.mjs +0 -4
- package/dist/date.mjs.map +0 -1
- package/dist/dom.js.map +0 -1
- package/dist/dom.mjs +0 -4
- package/dist/dom.mjs.map +0 -1
- package/dist/file.js.map +0 -1
- package/dist/file.mjs +0 -4
- package/dist/file.mjs.map +0 -1
- package/dist/i18n.js.map +0 -1
- package/dist/i18n.mjs +0 -4
- package/dist/i18n.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -17155
- package/dist/index.mjs.map +0 -1
- package/dist/network.js.map +0 -1
- package/dist/network.mjs +0 -4
- package/dist/network.mjs.map +0 -1
- package/dist/number.js.map +0 -1
- package/dist/number.mjs +0 -4
- package/dist/number.mjs.map +0 -1
- package/dist/object.js.map +0 -1
- package/dist/object.mjs +0 -4
- package/dist/object.mjs.map +0 -1
- package/dist/performance.js.map +0 -1
- package/dist/performance.mjs +0 -4
- package/dist/performance.mjs.map +0 -1
- package/dist/storage.js.map +0 -1
- package/dist/storage.mjs +0 -5
- package/dist/storage.mjs.map +0 -1
- package/dist/string.js.map +0 -1
- package/dist/string.mjs +0 -4
- package/dist/string.mjs.map +0 -1
- package/dist/tracking.js.map +0 -1
- package/dist/tracking.mjs +0 -4
- package/dist/tracking.mjs.map +0 -1
- package/dist/transform.js.map +0 -1
- package/dist/transform.mjs +0 -4
- package/dist/transform.mjs.map +0 -1
- package/dist/upload.js.map +0 -1
- package/dist/upload.mjs +0 -5
- package/dist/upload.mjs.map +0 -1
- package/dist/url.js.map +0 -1
- package/dist/url.mjs +0 -4
- package/dist/url.mjs.map +0 -1
- package/dist/validation.js.map +0 -1
- package/dist/validation.mjs +0 -4
- package/dist/validation.mjs.map +0 -1
- /package/dist/{upload-DchqyDBQ.d.mts → index-DchqyDBQ.d.mts} +0 -0
- /package/dist/{upload-DchqyDBQ.d.ts → index-DchqyDBQ.d.ts} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// src/helper/crypto.ts
|
|
3
|
+
// src/helper/crypto/index.ts
|
|
4
4
|
async function sha256(data) {
|
|
5
5
|
const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
6
6
|
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
|
|
@@ -171,11 +171,118 @@ async function importPrivateKey(keyData) {
|
|
|
171
171
|
["decrypt"]
|
|
172
172
|
);
|
|
173
173
|
}
|
|
174
|
+
async function generateHMACKey() {
|
|
175
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
176
|
+
throw new Error("Web Crypto API is not available");
|
|
177
|
+
}
|
|
178
|
+
return crypto.subtle.generateKey(
|
|
179
|
+
{
|
|
180
|
+
name: "HMAC",
|
|
181
|
+
hash: "SHA-256"
|
|
182
|
+
},
|
|
183
|
+
true,
|
|
184
|
+
["sign", "verify"]
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
async function computeHMAC(data, key) {
|
|
188
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
189
|
+
throw new Error("Web Crypto API is not available");
|
|
190
|
+
}
|
|
191
|
+
const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
192
|
+
const signature = await crypto.subtle.sign("HMAC", key, buffer);
|
|
193
|
+
return base64Encode(signature);
|
|
194
|
+
}
|
|
195
|
+
async function verifyHMAC(data, signature, key) {
|
|
196
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
197
|
+
throw new Error("Web Crypto API is not available");
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
const dataBuffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
201
|
+
const signatureBuffer = new Uint8Array(
|
|
202
|
+
atob(signature).split("").map((char) => char.charCodeAt(0))
|
|
203
|
+
);
|
|
204
|
+
return await crypto.subtle.verify("HMAC", key, signatureBuffer, dataBuffer);
|
|
205
|
+
} catch {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async function deriveKeyFromPassword(password, salt, iterations = 1e5, keyLength = 256) {
|
|
210
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
211
|
+
throw new Error("Web Crypto API is not available");
|
|
212
|
+
}
|
|
213
|
+
const saltBuffer = typeof salt === "string" ? new TextEncoder().encode(salt) : salt;
|
|
214
|
+
const passwordKey = await crypto.subtle.importKey(
|
|
215
|
+
"raw",
|
|
216
|
+
new TextEncoder().encode(password),
|
|
217
|
+
"PBKDF2",
|
|
218
|
+
false,
|
|
219
|
+
["deriveBits", "deriveKey"]
|
|
220
|
+
);
|
|
221
|
+
return crypto.subtle.deriveKey(
|
|
222
|
+
{
|
|
223
|
+
name: "PBKDF2",
|
|
224
|
+
salt: saltBuffer,
|
|
225
|
+
iterations,
|
|
226
|
+
hash: "SHA-256"
|
|
227
|
+
},
|
|
228
|
+
passwordKey,
|
|
229
|
+
{
|
|
230
|
+
name: "AES-GCM",
|
|
231
|
+
length: keyLength
|
|
232
|
+
},
|
|
233
|
+
false,
|
|
234
|
+
["encrypt", "decrypt"]
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
async function aesGCMEncrypt(data, key) {
|
|
238
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
239
|
+
throw new Error("Web Crypto API is not available");
|
|
240
|
+
}
|
|
241
|
+
const dataBuffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
242
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
243
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
244
|
+
{
|
|
245
|
+
name: "AES-GCM",
|
|
246
|
+
iv
|
|
247
|
+
},
|
|
248
|
+
key,
|
|
249
|
+
dataBuffer
|
|
250
|
+
);
|
|
251
|
+
return {
|
|
252
|
+
encrypted: base64Encode(encrypted),
|
|
253
|
+
iv: base64Encode(iv.buffer)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
async function aesGCMDecrypt(encryptedData, iv, key) {
|
|
257
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
258
|
+
throw new Error("Web Crypto API is not available");
|
|
259
|
+
}
|
|
260
|
+
const encryptedBuffer = new Uint8Array(
|
|
261
|
+
atob(encryptedData).split("").map((char) => char.charCodeAt(0))
|
|
262
|
+
);
|
|
263
|
+
const ivBuffer = new Uint8Array(
|
|
264
|
+
atob(iv).split("").map((char) => char.charCodeAt(0))
|
|
265
|
+
);
|
|
266
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
267
|
+
{
|
|
268
|
+
name: "AES-GCM",
|
|
269
|
+
iv: ivBuffer
|
|
270
|
+
},
|
|
271
|
+
key,
|
|
272
|
+
encryptedBuffer
|
|
273
|
+
);
|
|
274
|
+
return new TextDecoder().decode(decrypted);
|
|
275
|
+
}
|
|
174
276
|
|
|
277
|
+
exports.aesGCMDecrypt = aesGCMDecrypt;
|
|
278
|
+
exports.aesGCMEncrypt = aesGCMEncrypt;
|
|
175
279
|
exports.base64Decode = base64Decode;
|
|
176
280
|
exports.base64Encode = base64Encode;
|
|
281
|
+
exports.computeHMAC = computeHMAC;
|
|
282
|
+
exports.deriveKeyFromPassword = deriveKeyFromPassword;
|
|
177
283
|
exports.exportPrivateKey = exportPrivateKey;
|
|
178
284
|
exports.exportPublicKey = exportPublicKey;
|
|
285
|
+
exports.generateHMACKey = generateHMACKey;
|
|
179
286
|
exports.generateRSAKeyPair = generateRSAKeyPair;
|
|
180
287
|
exports.generateRandomString = generateRandomString;
|
|
181
288
|
exports.generateUUID = generateUUID;
|
|
@@ -185,5 +292,4 @@ exports.importPublicKey = importPublicKey;
|
|
|
185
292
|
exports.rsaDecrypt = rsaDecrypt;
|
|
186
293
|
exports.rsaEncrypt = rsaEncrypt;
|
|
187
294
|
exports.sha256 = sha256;
|
|
188
|
-
|
|
189
|
-
//# sourceMappingURL=chunk-DYBSRI7V.js.map
|
|
295
|
+
exports.verifyHMAC = verifyHMAC;
|
package/dist/crypto.d.mts
CHANGED
|
@@ -88,5 +88,52 @@ declare function importPublicKey(keyData: string): Promise<CryptoKey>;
|
|
|
88
88
|
* @returns Promise<CryptoKey> 私钥对象
|
|
89
89
|
*/
|
|
90
90
|
declare function importPrivateKey(keyData: string): Promise<CryptoKey>;
|
|
91
|
+
/**
|
|
92
|
+
* 生成 HMAC 密钥
|
|
93
|
+
* @returns Promise<CryptoKey> HMAC 密钥
|
|
94
|
+
*/
|
|
95
|
+
declare function generateHMACKey(): Promise<CryptoKey>;
|
|
96
|
+
/**
|
|
97
|
+
* 计算 HMAC 签名
|
|
98
|
+
* @param data - 要签名的数据(字符串或ArrayBuffer)
|
|
99
|
+
* @param key - HMAC密钥
|
|
100
|
+
* @returns Promise<string> Base64编码的HMAC签名
|
|
101
|
+
*/
|
|
102
|
+
declare function computeHMAC(data: string | ArrayBuffer, key: CryptoKey): Promise<string>;
|
|
103
|
+
/**
|
|
104
|
+
* 验证 HMAC 签名
|
|
105
|
+
* @param data - 原始数据(字符串或ArrayBuffer)
|
|
106
|
+
* @param signature - Base64编码的HMAC签名
|
|
107
|
+
* @param key - HMAC密钥
|
|
108
|
+
* @returns Promise<boolean> 签名是否有效
|
|
109
|
+
*/
|
|
110
|
+
declare function verifyHMAC(data: string | ArrayBuffer, signature: string, key: CryptoKey): Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* 使用 PBKDF2 派生密钥
|
|
113
|
+
* @param password - 密码(字符串)
|
|
114
|
+
* @param salt - 盐值(ArrayBuffer 或字符串)
|
|
115
|
+
* @param iterations - 迭代次数,默认为 100000
|
|
116
|
+
* @param keyLength - 密钥长度(位),默认为 256
|
|
117
|
+
* @returns Promise<CryptoKey> 派生的密钥
|
|
118
|
+
*/
|
|
119
|
+
declare function deriveKeyFromPassword(password: string, salt: ArrayBuffer | string, iterations?: number, keyLength?: number): Promise<CryptoKey>;
|
|
120
|
+
/**
|
|
121
|
+
* 使用 AES-GCM 加密数据
|
|
122
|
+
* @param data - 要加密的数据(字符串或ArrayBuffer)
|
|
123
|
+
* @param key - AES密钥
|
|
124
|
+
* @returns Promise<{encrypted: string, iv: string}> 加密后的数据和初始向量(Base64编码)
|
|
125
|
+
*/
|
|
126
|
+
declare function aesGCMEncrypt(data: string | ArrayBuffer, key: CryptoKey): Promise<{
|
|
127
|
+
encrypted: string;
|
|
128
|
+
iv: string;
|
|
129
|
+
}>;
|
|
130
|
+
/**
|
|
131
|
+
* 使用 AES-GCM 解密数据
|
|
132
|
+
* @param encryptedData - Base64编码的加密数据
|
|
133
|
+
* @param iv - Base64编码的初始向量
|
|
134
|
+
* @param key - AES密钥
|
|
135
|
+
* @returns Promise<string> 解密后的字符串
|
|
136
|
+
*/
|
|
137
|
+
declare function aesGCMDecrypt(encryptedData: string, iv: string, key: CryptoKey): Promise<string>;
|
|
91
138
|
|
|
92
|
-
export { type RSAKeyPair, base64Decode, base64Encode, exportPrivateKey, exportPublicKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256 };
|
|
139
|
+
export { type RSAKeyPair, aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, computeHMAC, deriveKeyFromPassword, exportPrivateKey, exportPublicKey, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256, verifyHMAC };
|
package/dist/crypto.d.ts
CHANGED
|
@@ -88,5 +88,52 @@ declare function importPublicKey(keyData: string): Promise<CryptoKey>;
|
|
|
88
88
|
* @returns Promise<CryptoKey> 私钥对象
|
|
89
89
|
*/
|
|
90
90
|
declare function importPrivateKey(keyData: string): Promise<CryptoKey>;
|
|
91
|
+
/**
|
|
92
|
+
* 生成 HMAC 密钥
|
|
93
|
+
* @returns Promise<CryptoKey> HMAC 密钥
|
|
94
|
+
*/
|
|
95
|
+
declare function generateHMACKey(): Promise<CryptoKey>;
|
|
96
|
+
/**
|
|
97
|
+
* 计算 HMAC 签名
|
|
98
|
+
* @param data - 要签名的数据(字符串或ArrayBuffer)
|
|
99
|
+
* @param key - HMAC密钥
|
|
100
|
+
* @returns Promise<string> Base64编码的HMAC签名
|
|
101
|
+
*/
|
|
102
|
+
declare function computeHMAC(data: string | ArrayBuffer, key: CryptoKey): Promise<string>;
|
|
103
|
+
/**
|
|
104
|
+
* 验证 HMAC 签名
|
|
105
|
+
* @param data - 原始数据(字符串或ArrayBuffer)
|
|
106
|
+
* @param signature - Base64编码的HMAC签名
|
|
107
|
+
* @param key - HMAC密钥
|
|
108
|
+
* @returns Promise<boolean> 签名是否有效
|
|
109
|
+
*/
|
|
110
|
+
declare function verifyHMAC(data: string | ArrayBuffer, signature: string, key: CryptoKey): Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* 使用 PBKDF2 派生密钥
|
|
113
|
+
* @param password - 密码(字符串)
|
|
114
|
+
* @param salt - 盐值(ArrayBuffer 或字符串)
|
|
115
|
+
* @param iterations - 迭代次数,默认为 100000
|
|
116
|
+
* @param keyLength - 密钥长度(位),默认为 256
|
|
117
|
+
* @returns Promise<CryptoKey> 派生的密钥
|
|
118
|
+
*/
|
|
119
|
+
declare function deriveKeyFromPassword(password: string, salt: ArrayBuffer | string, iterations?: number, keyLength?: number): Promise<CryptoKey>;
|
|
120
|
+
/**
|
|
121
|
+
* 使用 AES-GCM 加密数据
|
|
122
|
+
* @param data - 要加密的数据(字符串或ArrayBuffer)
|
|
123
|
+
* @param key - AES密钥
|
|
124
|
+
* @returns Promise<{encrypted: string, iv: string}> 加密后的数据和初始向量(Base64编码)
|
|
125
|
+
*/
|
|
126
|
+
declare function aesGCMEncrypt(data: string | ArrayBuffer, key: CryptoKey): Promise<{
|
|
127
|
+
encrypted: string;
|
|
128
|
+
iv: string;
|
|
129
|
+
}>;
|
|
130
|
+
/**
|
|
131
|
+
* 使用 AES-GCM 解密数据
|
|
132
|
+
* @param encryptedData - Base64编码的加密数据
|
|
133
|
+
* @param iv - Base64编码的初始向量
|
|
134
|
+
* @param key - AES密钥
|
|
135
|
+
* @returns Promise<string> 解密后的字符串
|
|
136
|
+
*/
|
|
137
|
+
declare function aesGCMDecrypt(encryptedData: string, iv: string, key: CryptoKey): Promise<string>;
|
|
91
138
|
|
|
92
|
-
export { type RSAKeyPair, base64Decode, base64Encode, exportPrivateKey, exportPublicKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256 };
|
|
139
|
+
export { type RSAKeyPair, aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, computeHMAC, deriveKeyFromPassword, exportPrivateKey, exportPublicKey, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256, verifyHMAC };
|
package/dist/crypto.js
CHANGED
|
@@ -1,61 +1,275 @@
|
|
|
1
|
-
|
|
1
|
+
// src/helper/crypto/index.ts
|
|
2
|
+
async function sha256(data) {
|
|
3
|
+
const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
4
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
|
|
5
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
6
|
+
return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
7
|
+
}
|
|
8
|
+
function base64Encode(data) {
|
|
9
|
+
if (typeof data === "string") {
|
|
10
|
+
return btoa(unescape(encodeURIComponent(data)));
|
|
11
|
+
}
|
|
12
|
+
const bytes = new Uint8Array(data);
|
|
13
|
+
let binary = "";
|
|
14
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
15
|
+
binary += String.fromCharCode(bytes[i]);
|
|
16
|
+
}
|
|
17
|
+
return btoa(binary);
|
|
18
|
+
}
|
|
19
|
+
function base64Decode(data) {
|
|
20
|
+
try {
|
|
21
|
+
return decodeURIComponent(escape(atob(data)));
|
|
22
|
+
} catch {
|
|
23
|
+
throw new Error("Invalid Base64 string");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function generateUUID() {
|
|
27
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
28
|
+
return crypto.randomUUID();
|
|
29
|
+
}
|
|
30
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
|
31
|
+
const r = Math.random() * 16 | 0;
|
|
32
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
33
|
+
return v.toString(16);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function generateRandomString(length, charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
|
|
37
|
+
let result = "";
|
|
38
|
+
for (let i = 0; i < length; i++) {
|
|
39
|
+
result += charset.charAt(Math.floor(Math.random() * charset.length));
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
function hash(data) {
|
|
44
|
+
let hashValue = 0;
|
|
45
|
+
for (let i = 0; i < data.length; i++) {
|
|
46
|
+
const char = data.charCodeAt(i);
|
|
47
|
+
hashValue = (hashValue << 5) - hashValue + char;
|
|
48
|
+
hashValue = hashValue & hashValue;
|
|
49
|
+
}
|
|
50
|
+
return Math.abs(hashValue);
|
|
51
|
+
}
|
|
52
|
+
async function generateRSAKeyPair(modulusLength = 2048) {
|
|
53
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
54
|
+
throw new Error("Web Crypto API is not available");
|
|
55
|
+
}
|
|
56
|
+
const keyPair = await crypto.subtle.generateKey(
|
|
57
|
+
{
|
|
58
|
+
name: "RSA-OAEP",
|
|
59
|
+
modulusLength,
|
|
60
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
61
|
+
hash: "SHA-256"
|
|
62
|
+
},
|
|
63
|
+
true,
|
|
64
|
+
["encrypt", "decrypt"]
|
|
65
|
+
);
|
|
66
|
+
return {
|
|
67
|
+
publicKey: keyPair.publicKey,
|
|
68
|
+
privateKey: keyPair.privateKey
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async function rsaEncrypt(data, publicKey) {
|
|
72
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
73
|
+
throw new Error("Web Crypto API is not available");
|
|
74
|
+
}
|
|
75
|
+
const encoder = new TextEncoder();
|
|
76
|
+
const dataBuffer = encoder.encode(data);
|
|
77
|
+
const maxChunkSize = 245;
|
|
78
|
+
const chunks = [];
|
|
79
|
+
for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
|
|
80
|
+
const chunk = dataBuffer.slice(i, i + maxChunkSize);
|
|
81
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
82
|
+
{
|
|
83
|
+
name: "RSA-OAEP"
|
|
84
|
+
},
|
|
85
|
+
publicKey,
|
|
86
|
+
chunk
|
|
87
|
+
);
|
|
88
|
+
chunks.push(encrypted);
|
|
89
|
+
}
|
|
90
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
|
|
91
|
+
const merged = new Uint8Array(totalLength);
|
|
92
|
+
let offset = 0;
|
|
93
|
+
for (const chunk of chunks) {
|
|
94
|
+
merged.set(new Uint8Array(chunk), offset);
|
|
95
|
+
offset += chunk.byteLength;
|
|
96
|
+
}
|
|
97
|
+
return base64Encode(merged.buffer);
|
|
98
|
+
}
|
|
99
|
+
async function rsaDecrypt(encryptedData, privateKey) {
|
|
100
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
101
|
+
throw new Error("Web Crypto API is not available");
|
|
102
|
+
}
|
|
103
|
+
const binaryString = atob(encryptedData);
|
|
104
|
+
const encryptedArray = new Uint8Array(binaryString.length);
|
|
105
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
106
|
+
encryptedArray[i] = binaryString.charCodeAt(i);
|
|
107
|
+
}
|
|
108
|
+
const chunkSize = 256;
|
|
109
|
+
const chunks = [];
|
|
110
|
+
for (let i = 0; i < encryptedArray.length; i += chunkSize) {
|
|
111
|
+
const chunk = encryptedArray.slice(i, i + chunkSize);
|
|
112
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
113
|
+
{
|
|
114
|
+
name: "RSA-OAEP"
|
|
115
|
+
},
|
|
116
|
+
privateKey,
|
|
117
|
+
chunk
|
|
118
|
+
);
|
|
119
|
+
const decoder = new TextDecoder();
|
|
120
|
+
chunks.push(decoder.decode(decrypted));
|
|
121
|
+
}
|
|
122
|
+
return chunks.join("");
|
|
123
|
+
}
|
|
124
|
+
async function exportPublicKey(publicKey) {
|
|
125
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
126
|
+
throw new Error("Web Crypto API is not available");
|
|
127
|
+
}
|
|
128
|
+
const exported = await crypto.subtle.exportKey("spki", publicKey);
|
|
129
|
+
return base64Encode(exported);
|
|
130
|
+
}
|
|
131
|
+
async function exportPrivateKey(privateKey) {
|
|
132
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
133
|
+
throw new Error("Web Crypto API is not available");
|
|
134
|
+
}
|
|
135
|
+
const exported = await crypto.subtle.exportKey("pkcs8", privateKey);
|
|
136
|
+
return base64Encode(exported);
|
|
137
|
+
}
|
|
138
|
+
async function importPublicKey(keyData) {
|
|
139
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
140
|
+
throw new Error("Web Crypto API is not available");
|
|
141
|
+
}
|
|
142
|
+
const keyBuffer = base64Decode(keyData);
|
|
143
|
+
const keyArray = new Uint8Array(keyBuffer.split("").map((char) => char.charCodeAt(0)));
|
|
144
|
+
return crypto.subtle.importKey(
|
|
145
|
+
"spki",
|
|
146
|
+
keyArray.buffer,
|
|
147
|
+
{
|
|
148
|
+
name: "RSA-OAEP",
|
|
149
|
+
hash: "SHA-256"
|
|
150
|
+
},
|
|
151
|
+
true,
|
|
152
|
+
["encrypt"]
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
async function importPrivateKey(keyData) {
|
|
156
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
157
|
+
throw new Error("Web Crypto API is not available");
|
|
158
|
+
}
|
|
159
|
+
const keyBuffer = base64Decode(keyData);
|
|
160
|
+
const keyArray = new Uint8Array(keyBuffer.split("").map((char) => char.charCodeAt(0)));
|
|
161
|
+
return crypto.subtle.importKey(
|
|
162
|
+
"pkcs8",
|
|
163
|
+
keyArray.buffer,
|
|
164
|
+
{
|
|
165
|
+
name: "RSA-OAEP",
|
|
166
|
+
hash: "SHA-256"
|
|
167
|
+
},
|
|
168
|
+
true,
|
|
169
|
+
["decrypt"]
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
async function generateHMACKey() {
|
|
173
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
174
|
+
throw new Error("Web Crypto API is not available");
|
|
175
|
+
}
|
|
176
|
+
return crypto.subtle.generateKey(
|
|
177
|
+
{
|
|
178
|
+
name: "HMAC",
|
|
179
|
+
hash: "SHA-256"
|
|
180
|
+
},
|
|
181
|
+
true,
|
|
182
|
+
["sign", "verify"]
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
async function computeHMAC(data, key) {
|
|
186
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
187
|
+
throw new Error("Web Crypto API is not available");
|
|
188
|
+
}
|
|
189
|
+
const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
190
|
+
const signature = await crypto.subtle.sign("HMAC", key, buffer);
|
|
191
|
+
return base64Encode(signature);
|
|
192
|
+
}
|
|
193
|
+
async function verifyHMAC(data, signature, key) {
|
|
194
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
195
|
+
throw new Error("Web Crypto API is not available");
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
const dataBuffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
199
|
+
const signatureBuffer = new Uint8Array(
|
|
200
|
+
atob(signature).split("").map((char) => char.charCodeAt(0))
|
|
201
|
+
);
|
|
202
|
+
return await crypto.subtle.verify("HMAC", key, signatureBuffer, dataBuffer);
|
|
203
|
+
} catch {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
async function deriveKeyFromPassword(password, salt, iterations = 1e5, keyLength = 256) {
|
|
208
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
209
|
+
throw new Error("Web Crypto API is not available");
|
|
210
|
+
}
|
|
211
|
+
const saltBuffer = typeof salt === "string" ? new TextEncoder().encode(salt) : salt;
|
|
212
|
+
const passwordKey = await crypto.subtle.importKey(
|
|
213
|
+
"raw",
|
|
214
|
+
new TextEncoder().encode(password),
|
|
215
|
+
"PBKDF2",
|
|
216
|
+
false,
|
|
217
|
+
["deriveBits", "deriveKey"]
|
|
218
|
+
);
|
|
219
|
+
return crypto.subtle.deriveKey(
|
|
220
|
+
{
|
|
221
|
+
name: "PBKDF2",
|
|
222
|
+
salt: saltBuffer,
|
|
223
|
+
iterations,
|
|
224
|
+
hash: "SHA-256"
|
|
225
|
+
},
|
|
226
|
+
passwordKey,
|
|
227
|
+
{
|
|
228
|
+
name: "AES-GCM",
|
|
229
|
+
length: keyLength
|
|
230
|
+
},
|
|
231
|
+
false,
|
|
232
|
+
["encrypt", "decrypt"]
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
async function aesGCMEncrypt(data, key) {
|
|
236
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
237
|
+
throw new Error("Web Crypto API is not available");
|
|
238
|
+
}
|
|
239
|
+
const dataBuffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
240
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
241
|
+
const encrypted = await crypto.subtle.encrypt(
|
|
242
|
+
{
|
|
243
|
+
name: "AES-GCM",
|
|
244
|
+
iv
|
|
245
|
+
},
|
|
246
|
+
key,
|
|
247
|
+
dataBuffer
|
|
248
|
+
);
|
|
249
|
+
return {
|
|
250
|
+
encrypted: base64Encode(encrypted),
|
|
251
|
+
iv: base64Encode(iv.buffer)
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
async function aesGCMDecrypt(encryptedData, iv, key) {
|
|
255
|
+
if (typeof crypto === "undefined" || !crypto.subtle) {
|
|
256
|
+
throw new Error("Web Crypto API is not available");
|
|
257
|
+
}
|
|
258
|
+
const encryptedBuffer = new Uint8Array(
|
|
259
|
+
atob(encryptedData).split("").map((char) => char.charCodeAt(0))
|
|
260
|
+
);
|
|
261
|
+
const ivBuffer = new Uint8Array(
|
|
262
|
+
atob(iv).split("").map((char) => char.charCodeAt(0))
|
|
263
|
+
);
|
|
264
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
265
|
+
{
|
|
266
|
+
name: "AES-GCM",
|
|
267
|
+
iv: ivBuffer
|
|
268
|
+
},
|
|
269
|
+
key,
|
|
270
|
+
encryptedBuffer
|
|
271
|
+
);
|
|
272
|
+
return new TextDecoder().decode(decrypted);
|
|
273
|
+
}
|
|
2
274
|
|
|
3
|
-
|
|
4
|
-
require('./chunk-PR4QN5HX.js');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(exports, "base64Decode", {
|
|
9
|
-
enumerable: true,
|
|
10
|
-
get: function () { return chunkDYBSRI7V_js.base64Decode; }
|
|
11
|
-
});
|
|
12
|
-
Object.defineProperty(exports, "base64Encode", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
get: function () { return chunkDYBSRI7V_js.base64Encode; }
|
|
15
|
-
});
|
|
16
|
-
Object.defineProperty(exports, "exportPrivateKey", {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return chunkDYBSRI7V_js.exportPrivateKey; }
|
|
19
|
-
});
|
|
20
|
-
Object.defineProperty(exports, "exportPublicKey", {
|
|
21
|
-
enumerable: true,
|
|
22
|
-
get: function () { return chunkDYBSRI7V_js.exportPublicKey; }
|
|
23
|
-
});
|
|
24
|
-
Object.defineProperty(exports, "generateRSAKeyPair", {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return chunkDYBSRI7V_js.generateRSAKeyPair; }
|
|
27
|
-
});
|
|
28
|
-
Object.defineProperty(exports, "generateRandomString", {
|
|
29
|
-
enumerable: true,
|
|
30
|
-
get: function () { return chunkDYBSRI7V_js.generateRandomString; }
|
|
31
|
-
});
|
|
32
|
-
Object.defineProperty(exports, "generateUUID", {
|
|
33
|
-
enumerable: true,
|
|
34
|
-
get: function () { return chunkDYBSRI7V_js.generateUUID; }
|
|
35
|
-
});
|
|
36
|
-
Object.defineProperty(exports, "hash", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
get: function () { return chunkDYBSRI7V_js.hash; }
|
|
39
|
-
});
|
|
40
|
-
Object.defineProperty(exports, "importPrivateKey", {
|
|
41
|
-
enumerable: true,
|
|
42
|
-
get: function () { return chunkDYBSRI7V_js.importPrivateKey; }
|
|
43
|
-
});
|
|
44
|
-
Object.defineProperty(exports, "importPublicKey", {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
get: function () { return chunkDYBSRI7V_js.importPublicKey; }
|
|
47
|
-
});
|
|
48
|
-
Object.defineProperty(exports, "rsaDecrypt", {
|
|
49
|
-
enumerable: true,
|
|
50
|
-
get: function () { return chunkDYBSRI7V_js.rsaDecrypt; }
|
|
51
|
-
});
|
|
52
|
-
Object.defineProperty(exports, "rsaEncrypt", {
|
|
53
|
-
enumerable: true,
|
|
54
|
-
get: function () { return chunkDYBSRI7V_js.rsaEncrypt; }
|
|
55
|
-
});
|
|
56
|
-
Object.defineProperty(exports, "sha256", {
|
|
57
|
-
enumerable: true,
|
|
58
|
-
get: function () { return chunkDYBSRI7V_js.sha256; }
|
|
59
|
-
});
|
|
60
|
-
//# sourceMappingURL=crypto.js.map
|
|
61
|
-
//# sourceMappingURL=crypto.js.map
|
|
275
|
+
export { aesGCMDecrypt, aesGCMEncrypt, base64Decode, base64Encode, computeHMAC, deriveKeyFromPassword, exportPrivateKey, exportPublicKey, generateHMACKey, generateRSAKeyPair, generateRandomString, generateUUID, hash, importPrivateKey, importPublicKey, rsaDecrypt, rsaEncrypt, sha256, verifyHMAC };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
// src/data/data-structure.ts
|
|
3
|
+
// src/data/data-structure/index.ts
|
|
4
4
|
var Stack = class {
|
|
5
5
|
constructor() {
|
|
6
6
|
this.items = [];
|
|
@@ -487,5 +487,3 @@ exports.Graph = Graph;
|
|
|
487
487
|
exports.LRUCache = LRUCache;
|
|
488
488
|
exports.LinkedList = LinkedList;
|
|
489
489
|
exports.Stack = Stack;
|
|
490
|
-
//# sourceMappingURL=chunk-BHCRFURU.js.map
|
|
491
|
-
//# sourceMappingURL=chunk-BHCRFURU.js.map
|