cipher-kit 2.1.1 → 2.1.3
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-LTVOIZP5.cjs → chunk-BMX42IZM.cjs} +145 -150
- package/dist/chunk-BMX42IZM.cjs.map +1 -0
- package/dist/{chunk-X4CS7UXE.cjs → chunk-HTRGOBZF.cjs} +10 -2
- package/dist/chunk-HTRGOBZF.cjs.map +1 -0
- package/dist/{chunk-6C4NIWQ4.js → chunk-LU7QOSQH.js} +9 -3
- package/dist/chunk-LU7QOSQH.js.map +1 -0
- package/dist/{chunk-PWTFVMW6.js → chunk-S6SNCTU6.js} +4 -8
- package/dist/chunk-S6SNCTU6.js.map +1 -0
- package/dist/{chunk-FSEA3UXJ.js → chunk-T36BEDPY.js} +4 -8
- package/dist/chunk-T36BEDPY.js.map +1 -0
- package/dist/{chunk-56PVVFVM.cjs → chunk-ZNM5M6RD.cjs} +140 -145
- package/dist/chunk-ZNM5M6RD.cjs.map +1 -0
- package/dist/{export-DUgIcobC.d.ts → export-BaM_OTFk.d.ts} +13 -31
- package/dist/{export-C4DbO5zM.d.ts → export-CCTGAosO.d.ts} +13 -31
- package/dist/{export-CpZ7s25O.d.cts → export-FYHgb-8E.d.cts} +13 -31
- package/dist/{export-DO9n7Np-.d.cts → export-KFT0YyMg.d.cts} +13 -31
- package/dist/index.cjs +19 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +35 -35
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-cJEdGlj1.d.cts → validate-lkJAHCeJ.d.cts} +39 -11
- package/dist/{validate-cJEdGlj1.d.ts → validate-lkJAHCeJ.d.ts} +39 -11
- package/dist/web-api.cjs +35 -35
- package/dist/web-api.d.cts +2 -2
- package/dist/web-api.d.ts +2 -2
- package/dist/web-api.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-56PVVFVM.cjs.map +0 -1
- package/dist/chunk-6C4NIWQ4.js.map +0 -1
- package/dist/chunk-FSEA3UXJ.js.map +0 -1
- package/dist/chunk-LTVOIZP5.cjs.map +0 -1
- package/dist/chunk-PWTFVMW6.js.map +0 -1
- package/dist/chunk-X4CS7UXE.cjs.map +0 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkHTRGOBZF_cjs = require('./chunk-HTRGOBZF.cjs');
|
|
4
4
|
|
|
5
5
|
// src/web/kit.ts
|
|
6
6
|
var kit_exports = {};
|
|
7
|
-
|
|
7
|
+
chunkHTRGOBZF_cjs.__export(kit_exports, {
|
|
8
8
|
convertBytesToStr: () => convertBytesToStr,
|
|
9
9
|
convertEncoding: () => convertEncoding,
|
|
10
10
|
convertStrToBytes: () => convertStrToBytes,
|
|
@@ -16,7 +16,6 @@ chunkX4CS7UXE_cjs.__export(kit_exports, {
|
|
|
16
16
|
generateUuid: () => generateUuid,
|
|
17
17
|
hash: () => hash,
|
|
18
18
|
hashPassword: () => hashPassword,
|
|
19
|
-
isWebSecretKey: () => isWebSecretKey,
|
|
20
19
|
tryConvertBytesToStr: () => tryConvertBytesToStr,
|
|
21
20
|
tryConvertEncoding: () => tryConvertEncoding,
|
|
22
21
|
tryConvertStrToBytes: () => tryConvertStrToBytes,
|
|
@@ -35,34 +34,34 @@ chunkX4CS7UXE_cjs.__export(kit_exports, {
|
|
|
35
34
|
var textEncoder = new TextEncoder();
|
|
36
35
|
var textDecoder = new TextDecoder();
|
|
37
36
|
function $convertStrToBytes(data, inputEncoding = "utf8") {
|
|
38
|
-
if (!
|
|
39
|
-
return
|
|
37
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
38
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
40
39
|
msg: "Crypto Web API - String to Bytes: Empty data",
|
|
41
40
|
desc: "Data must be a non-empty string"
|
|
42
41
|
});
|
|
43
42
|
}
|
|
44
|
-
if (!
|
|
45
|
-
return
|
|
43
|
+
if (!chunkHTRGOBZF_cjs.ENCODING.includes(inputEncoding)) {
|
|
44
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
46
45
|
msg: `Crypto Web API - String to Bytes: Unsupported encoding: ${inputEncoding}`,
|
|
47
46
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
48
47
|
});
|
|
49
48
|
}
|
|
50
49
|
try {
|
|
51
50
|
const bytes = strToBytes[inputEncoding](data);
|
|
52
|
-
return
|
|
51
|
+
return chunkHTRGOBZF_cjs.$ok({ result: bytes });
|
|
53
52
|
} catch (error) {
|
|
54
|
-
return
|
|
53
|
+
return chunkHTRGOBZF_cjs.$err({ msg: "Crypto Web API - String to Bytes: Failed to convert data", desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
function $convertBytesToStr(data, outputEncoding = "utf8") {
|
|
58
57
|
if (!(data instanceof ArrayBuffer || data instanceof Uint8Array)) {
|
|
59
|
-
return
|
|
58
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
60
59
|
msg: "Crypto Web API - Bytes to String: Invalid data type",
|
|
61
60
|
desc: "Data must be an ArrayBuffer or Uint8Array"
|
|
62
61
|
});
|
|
63
62
|
}
|
|
64
|
-
if (!
|
|
65
|
-
return
|
|
63
|
+
if (!chunkHTRGOBZF_cjs.ENCODING.includes(outputEncoding)) {
|
|
64
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
66
65
|
msg: `Crypto Web API - Bytes to String: Unsupported encoding: ${outputEncoding}`,
|
|
67
66
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
68
67
|
});
|
|
@@ -70,29 +69,29 @@ function $convertBytesToStr(data, outputEncoding = "utf8") {
|
|
|
70
69
|
try {
|
|
71
70
|
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
72
71
|
const str = bytesToStr[outputEncoding](bytes);
|
|
73
|
-
return
|
|
72
|
+
return chunkHTRGOBZF_cjs.$ok(str);
|
|
74
73
|
} catch (error) {
|
|
75
|
-
return
|
|
74
|
+
return chunkHTRGOBZF_cjs.$err({ msg: "Crypto Web API - Bytes to String: Failed to convert data", desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
76
75
|
}
|
|
77
76
|
}
|
|
78
77
|
function $convertEncoding(data, from, to) {
|
|
79
|
-
if (!
|
|
80
|
-
return
|
|
78
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
79
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
81
80
|
msg: "Crypto Web API - Convert Format: Empty data",
|
|
82
81
|
desc: "Data must be a non-empty string"
|
|
83
82
|
});
|
|
84
83
|
}
|
|
85
|
-
if (!
|
|
86
|
-
return
|
|
84
|
+
if (!chunkHTRGOBZF_cjs.ENCODING.includes(from) || !chunkHTRGOBZF_cjs.ENCODING.includes(to)) {
|
|
85
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
87
86
|
msg: `Crypto Web API - Convert Format: Unsupported encoding: from ${from} to ${to}`,
|
|
88
87
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
89
88
|
});
|
|
90
89
|
}
|
|
91
90
|
const bytes = $convertStrToBytes(data, from);
|
|
92
|
-
if (bytes.error) return
|
|
91
|
+
if (bytes.error) return chunkHTRGOBZF_cjs.$err({ msg: bytes.error.message, desc: bytes.error.description });
|
|
93
92
|
const str = $convertBytesToStr(bytes.result, to);
|
|
94
|
-
if (str.error) return
|
|
95
|
-
return
|
|
93
|
+
if (str.error) return chunkHTRGOBZF_cjs.$err({ msg: str.error.message, desc: str.error.description });
|
|
94
|
+
return chunkHTRGOBZF_cjs.$ok({ result: str.result });
|
|
96
95
|
}
|
|
97
96
|
var strToBytes = {
|
|
98
97
|
base64: $fromBase64,
|
|
@@ -158,54 +157,54 @@ function $fromHex(data) {
|
|
|
158
157
|
// src/web/web-encrypt.ts
|
|
159
158
|
function $generateUuid() {
|
|
160
159
|
try {
|
|
161
|
-
return
|
|
160
|
+
return chunkHTRGOBZF_cjs.$ok(crypto.randomUUID());
|
|
162
161
|
} catch (error) {
|
|
163
|
-
return
|
|
162
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("web", "UUID Generation")}: Failed to generate UUID`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
164
163
|
}
|
|
165
164
|
}
|
|
166
165
|
async function $createSecretKey(secret, options) {
|
|
167
|
-
if (!
|
|
168
|
-
return
|
|
169
|
-
msg: `${
|
|
166
|
+
if (!chunkHTRGOBZF_cjs.$isStr(secret, 8)) {
|
|
167
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
168
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Empty Secret`,
|
|
170
169
|
desc: "Secret must be a non-empty string with at least 8 characters"
|
|
171
170
|
});
|
|
172
171
|
}
|
|
173
|
-
if (!
|
|
174
|
-
return
|
|
175
|
-
msg: `${
|
|
172
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
173
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
174
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Invalid options`,
|
|
176
175
|
desc: "Options must be an object"
|
|
177
176
|
});
|
|
178
177
|
}
|
|
179
178
|
const algorithm = options.algorithm ?? "aes256gcm";
|
|
180
|
-
if (!(algorithm in
|
|
181
|
-
return
|
|
182
|
-
msg: `${
|
|
183
|
-
desc: `Supported algorithms are: ${Object.keys(
|
|
179
|
+
if (!(algorithm in chunkHTRGOBZF_cjs.ENCRYPTION_ALGORITHMS)) {
|
|
180
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
181
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Unsupported algorithm: ${algorithm}`,
|
|
182
|
+
desc: `Supported algorithms are: ${Object.keys(chunkHTRGOBZF_cjs.ENCRYPTION_ALGORITHMS).join(", ")}`
|
|
184
183
|
});
|
|
185
184
|
}
|
|
186
185
|
const digest = options.digest ?? "sha256";
|
|
187
|
-
if (!(digest in
|
|
188
|
-
return
|
|
189
|
-
msg: `${
|
|
190
|
-
desc: `Supported digests are: ${Object.keys(
|
|
186
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) {
|
|
187
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
188
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Unsupported digest: ${digest}`,
|
|
189
|
+
desc: `Supported digests are: ${Object.keys(chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
191
190
|
});
|
|
192
191
|
}
|
|
193
192
|
const salt = options.salt ?? "cipher-kit-salt";
|
|
194
|
-
if (!
|
|
195
|
-
return
|
|
196
|
-
msg: `${
|
|
193
|
+
if (!chunkHTRGOBZF_cjs.$isStr(salt, 8)) {
|
|
194
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
195
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Weak salt`,
|
|
197
196
|
desc: "Salt must be a non-empty string with at least 8 characters"
|
|
198
197
|
});
|
|
199
198
|
}
|
|
200
199
|
const info = options.info ?? "cipher-kit";
|
|
201
|
-
if (!
|
|
202
|
-
return
|
|
203
|
-
msg: `${
|
|
200
|
+
if (!chunkHTRGOBZF_cjs.$isStr(info)) {
|
|
201
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
202
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Invalid info`,
|
|
204
203
|
desc: "Info must be a non-empty string"
|
|
205
204
|
});
|
|
206
205
|
}
|
|
207
|
-
const encryptAlgo =
|
|
208
|
-
const digestAlgo =
|
|
206
|
+
const encryptAlgo = chunkHTRGOBZF_cjs.ENCRYPTION_ALGORITHMS[algorithm];
|
|
207
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
209
208
|
try {
|
|
210
209
|
const ikm = await crypto.subtle.importKey("raw", textEncoder.encode(secret.normalize("NFKC")), "HKDF", false, [
|
|
211
210
|
"deriveKey"
|
|
@@ -228,43 +227,43 @@ async function $createSecretKey(secret, options) {
|
|
|
228
227
|
algorithm,
|
|
229
228
|
key
|
|
230
229
|
});
|
|
231
|
-
return
|
|
230
|
+
return chunkHTRGOBZF_cjs.$ok({ result: secretKey });
|
|
232
231
|
} catch (error) {
|
|
233
|
-
return
|
|
234
|
-
msg: `${
|
|
235
|
-
desc:
|
|
232
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
233
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Key Generation")}: Failed to create secret key`,
|
|
234
|
+
desc: chunkHTRGOBZF_cjs.$fmtError(error)
|
|
236
235
|
});
|
|
237
236
|
}
|
|
238
237
|
}
|
|
239
238
|
async function $encrypt(data, secretKey, options) {
|
|
240
|
-
if (!
|
|
241
|
-
return
|
|
242
|
-
msg: `${
|
|
239
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
240
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
241
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Encryption")}: Empty data for encryption`,
|
|
243
242
|
desc: "Data must be a non-empty string"
|
|
244
243
|
});
|
|
245
244
|
}
|
|
246
|
-
if (!
|
|
247
|
-
return
|
|
248
|
-
msg: `${
|
|
245
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
246
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
247
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Encryption")}: Invalid options`,
|
|
249
248
|
desc: "Options must be an object"
|
|
250
249
|
});
|
|
251
250
|
}
|
|
252
251
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
253
|
-
if (!
|
|
254
|
-
return
|
|
255
|
-
msg: `${
|
|
252
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
253
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
254
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Encryption")}: Unsupported output encoding: ${outputEncoding}`,
|
|
256
255
|
desc: "Use base64, base64url, or hex"
|
|
257
256
|
});
|
|
258
257
|
}
|
|
259
|
-
const injectedKey =
|
|
258
|
+
const injectedKey = chunkHTRGOBZF_cjs.$isSecretKey(secretKey, "web");
|
|
260
259
|
if (!injectedKey) {
|
|
261
|
-
return
|
|
262
|
-
msg: `${
|
|
260
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
261
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Encryption")}: Invalid Secret Key`,
|
|
263
262
|
desc: "Expected a Web SecretKey"
|
|
264
263
|
});
|
|
265
264
|
}
|
|
266
265
|
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
267
|
-
if (error) return
|
|
266
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
268
267
|
try {
|
|
269
268
|
const iv = crypto.getRandomValues(new Uint8Array(injectedKey.injected.ivLength));
|
|
270
269
|
const cipherWithTag = await crypto.subtle.encrypt(
|
|
@@ -275,56 +274,56 @@ async function $encrypt(data, secretKey, options) {
|
|
|
275
274
|
const ivStr = $convertBytesToStr(iv, outputEncoding);
|
|
276
275
|
const cipherStr = $convertBytesToStr(cipherWithTag, outputEncoding);
|
|
277
276
|
if (ivStr.error || cipherStr.error) {
|
|
278
|
-
return
|
|
279
|
-
msg: `${
|
|
280
|
-
desc: `Conversion error: ${
|
|
277
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
278
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Encryption")}: Failed to convert IV or encrypted data`,
|
|
279
|
+
desc: `Conversion error: ${chunkHTRGOBZF_cjs.$fmtResultErr(ivStr.error || cipherStr.error)}`
|
|
281
280
|
});
|
|
282
281
|
}
|
|
283
|
-
return
|
|
282
|
+
return chunkHTRGOBZF_cjs.$ok(`${ivStr.result}.${cipherStr.result}.`);
|
|
284
283
|
} catch (error2) {
|
|
285
|
-
return
|
|
284
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("web", "Encryption")}: Failed to encrypt data`, desc: chunkHTRGOBZF_cjs.$fmtError(error2) });
|
|
286
285
|
}
|
|
287
286
|
}
|
|
288
287
|
async function $decrypt(encrypted, secretKey, options) {
|
|
289
|
-
if (!
|
|
290
|
-
return
|
|
291
|
-
msg: `${
|
|
288
|
+
if (!chunkHTRGOBZF_cjs.matchEncryptedPattern(encrypted, "web")) {
|
|
289
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
290
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Invalid encrypted data format`,
|
|
292
291
|
desc: 'Encrypted data must be in the format "iv.cipherWithTag."'
|
|
293
292
|
});
|
|
294
293
|
}
|
|
295
|
-
if (!
|
|
296
|
-
return
|
|
297
|
-
msg: `${
|
|
294
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
295
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
296
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Invalid options`,
|
|
298
297
|
desc: "Options must be an object"
|
|
299
298
|
});
|
|
300
299
|
}
|
|
301
300
|
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
302
|
-
if (!
|
|
303
|
-
return
|
|
304
|
-
msg: `${
|
|
301
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(inputEncoding)) {
|
|
302
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
303
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Unsupported input encoding: ${inputEncoding}`,
|
|
305
304
|
desc: "Use base64, base64url, or hex"
|
|
306
305
|
});
|
|
307
306
|
}
|
|
308
307
|
const [iv, encryptedWithTag] = encrypted.split(".", 3);
|
|
309
|
-
if (!
|
|
310
|
-
return
|
|
311
|
-
msg: `${
|
|
308
|
+
if (!chunkHTRGOBZF_cjs.$isStr(iv) || !chunkHTRGOBZF_cjs.$isStr(encryptedWithTag)) {
|
|
309
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
310
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Invalid encrypted data`,
|
|
312
311
|
desc: "Encrypted data must contain valid IV, encrypted and tag components"
|
|
313
312
|
});
|
|
314
313
|
}
|
|
315
|
-
const injectedKey =
|
|
314
|
+
const injectedKey = chunkHTRGOBZF_cjs.$isSecretKey(secretKey, "web");
|
|
316
315
|
if (!injectedKey) {
|
|
317
|
-
return
|
|
318
|
-
msg: `${
|
|
316
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
317
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Invalid Secret Key`,
|
|
319
318
|
desc: "Expected a Web SecretKey"
|
|
320
319
|
});
|
|
321
320
|
}
|
|
322
321
|
const ivBytes = $convertStrToBytes(iv, inputEncoding);
|
|
323
322
|
const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, inputEncoding);
|
|
324
323
|
if (ivBytes.error || cipherWithTagBytes.error) {
|
|
325
|
-
return
|
|
326
|
-
msg: `${
|
|
327
|
-
desc: `Conversion error: ${
|
|
324
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
325
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Failed to convert IV or encrypted data`,
|
|
326
|
+
desc: `Conversion error: ${chunkHTRGOBZF_cjs.$fmtResultErr(ivBytes.error || cipherWithTagBytes.error)}`
|
|
328
327
|
});
|
|
329
328
|
}
|
|
330
329
|
try {
|
|
@@ -335,99 +334,99 @@ async function $decrypt(encrypted, secretKey, options) {
|
|
|
335
334
|
);
|
|
336
335
|
return $convertBytesToStr(decrypted, "utf8");
|
|
337
336
|
} catch (error) {
|
|
338
|
-
return
|
|
337
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("web", "Decryption")}: Failed to decrypt data`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
339
338
|
}
|
|
340
339
|
}
|
|
341
340
|
async function $encryptObj(data, secretKey, options) {
|
|
342
|
-
const { result, error } =
|
|
343
|
-
if (error) return
|
|
341
|
+
const { result, error } = chunkHTRGOBZF_cjs.$stringifyObj(data);
|
|
342
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
344
343
|
return await $encrypt(result, secretKey, options);
|
|
345
344
|
}
|
|
346
345
|
async function $decryptObj(encrypted, secretKey, options) {
|
|
347
346
|
const { result, error } = await $decrypt(encrypted, secretKey, options);
|
|
348
|
-
if (error) return
|
|
349
|
-
return
|
|
347
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
348
|
+
return chunkHTRGOBZF_cjs.$parseToObj(result);
|
|
350
349
|
}
|
|
351
350
|
async function $hash(data, options) {
|
|
352
|
-
if (!
|
|
353
|
-
return
|
|
351
|
+
if (!chunkHTRGOBZF_cjs.$isStr(data)) {
|
|
352
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("web", "Hashing")}: Empty data for hashing`, desc: "Data must be a non-empty string" });
|
|
354
353
|
}
|
|
355
|
-
if (!
|
|
356
|
-
return
|
|
357
|
-
msg: `${
|
|
354
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
355
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
356
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Hashing")}: Invalid options`,
|
|
358
357
|
desc: "Options must be an object"
|
|
359
358
|
});
|
|
360
359
|
}
|
|
361
360
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
362
|
-
if (!
|
|
363
|
-
return
|
|
364
|
-
msg: `${
|
|
361
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
362
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
363
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Hashing")}: Unsupported output encoding: ${outputEncoding}`,
|
|
365
364
|
desc: "Use base64, base64url, or hex"
|
|
366
365
|
});
|
|
367
366
|
}
|
|
368
367
|
const digest = options.digest ?? "sha256";
|
|
369
|
-
if (!(digest in
|
|
370
|
-
return
|
|
371
|
-
msg: `${
|
|
372
|
-
desc: `Supported digests are: ${Object.keys(
|
|
368
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) {
|
|
369
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
370
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Hashing")}: Unsupported digest: ${digest}`,
|
|
371
|
+
desc: `Supported digests are: ${Object.keys(chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
373
372
|
});
|
|
374
373
|
}
|
|
375
|
-
const digestAlgo =
|
|
374
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
376
375
|
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
377
|
-
if (error) return
|
|
376
|
+
if (error) return chunkHTRGOBZF_cjs.$err(error);
|
|
378
377
|
try {
|
|
379
378
|
const hashed = await crypto.subtle.digest(digestAlgo.web, result);
|
|
380
379
|
return $convertBytesToStr(hashed, outputEncoding);
|
|
381
380
|
} catch (error2) {
|
|
382
|
-
return
|
|
381
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("web", "Hashing")}: Failed to hash data`, desc: chunkHTRGOBZF_cjs.$fmtError(error2) });
|
|
383
382
|
}
|
|
384
383
|
}
|
|
385
384
|
async function $hashPassword(password, options) {
|
|
386
|
-
if (!
|
|
387
|
-
return
|
|
388
|
-
msg: `${
|
|
385
|
+
if (!chunkHTRGOBZF_cjs.$isStr(password)) {
|
|
386
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
387
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Empty password`,
|
|
389
388
|
desc: "Password must be a non-empty string"
|
|
390
389
|
});
|
|
391
390
|
}
|
|
392
|
-
if (!
|
|
393
|
-
return
|
|
394
|
-
msg: `${
|
|
391
|
+
if (!chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
392
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
393
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Invalid options`,
|
|
395
394
|
desc: "Options must be an object"
|
|
396
395
|
});
|
|
397
396
|
}
|
|
398
397
|
const digest = options.digest ?? "sha512";
|
|
399
|
-
if (!(digest in
|
|
400
|
-
return
|
|
401
|
-
msg: `${
|
|
402
|
-
desc: `Supported digests are: ${Object.keys(
|
|
398
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) {
|
|
399
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
400
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Unsupported digest: ${digest}`,
|
|
401
|
+
desc: `Supported digests are: ${Object.keys(chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
403
402
|
});
|
|
404
403
|
}
|
|
405
|
-
const digestAlgo =
|
|
404
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
406
405
|
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
407
|
-
if (!
|
|
408
|
-
return
|
|
409
|
-
msg: `${
|
|
406
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(outputEncoding)) {
|
|
407
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
408
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Unsupported output encoding: ${outputEncoding}`,
|
|
410
409
|
desc: "Use base64, base64url, or hex"
|
|
411
410
|
});
|
|
412
411
|
}
|
|
413
412
|
const saltLength = options.saltLength ?? 16;
|
|
414
413
|
if (typeof saltLength !== "number" || saltLength < 8) {
|
|
415
|
-
return
|
|
416
|
-
msg: `${
|
|
414
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
415
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Weak salt length`,
|
|
417
416
|
desc: "Salt length must be a number and at least 8 bytes (recommended 16)"
|
|
418
417
|
});
|
|
419
418
|
}
|
|
420
419
|
const iterations = options.iterations ?? 32e4;
|
|
421
420
|
if (typeof iterations !== "number" || iterations < 1e3) {
|
|
422
|
-
return
|
|
423
|
-
msg: `${
|
|
421
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
422
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Weak iteration count`,
|
|
424
423
|
desc: "Iterations must be a number and at least 1000 (recommended 320,000 or more)"
|
|
425
424
|
});
|
|
426
425
|
}
|
|
427
426
|
const keyLength = options.keyLength ?? 64;
|
|
428
427
|
if (typeof keyLength !== "number" || keyLength < 16) {
|
|
429
|
-
return
|
|
430
|
-
msg: `${
|
|
428
|
+
return chunkHTRGOBZF_cjs.$err({
|
|
429
|
+
msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Weak key length`,
|
|
431
430
|
desc: "Key length must be a number and at least 16 bytes (recommended 64)"
|
|
432
431
|
});
|
|
433
432
|
}
|
|
@@ -446,23 +445,23 @@ async function $hashPassword(password, options) {
|
|
|
446
445
|
keyLength * 8
|
|
447
446
|
);
|
|
448
447
|
const saltStr = $convertBytesToStr(salt, outputEncoding);
|
|
449
|
-
if (saltStr.error) return
|
|
448
|
+
if (saltStr.error) return chunkHTRGOBZF_cjs.$err(saltStr.error);
|
|
450
449
|
const hashedPasswordStr = $convertBytesToStr(bits, outputEncoding);
|
|
451
|
-
if (hashedPasswordStr.error) return
|
|
452
|
-
return
|
|
450
|
+
if (hashedPasswordStr.error) return chunkHTRGOBZF_cjs.$err(hashedPasswordStr.error);
|
|
451
|
+
return chunkHTRGOBZF_cjs.$ok({ result: hashedPasswordStr.result, salt: saltStr.result });
|
|
453
452
|
} catch (error) {
|
|
454
|
-
return
|
|
453
|
+
return chunkHTRGOBZF_cjs.$err({ msg: `${chunkHTRGOBZF_cjs.title("web", "Password Hashing")}: Failed to hash password`, desc: chunkHTRGOBZF_cjs.$fmtError(error) });
|
|
455
454
|
}
|
|
456
455
|
}
|
|
457
456
|
async function $verifyPassword(password, hashedPassword, salt, options) {
|
|
458
|
-
if (!
|
|
457
|
+
if (!chunkHTRGOBZF_cjs.$isStr(password) || !chunkHTRGOBZF_cjs.$isStr(hashedPassword) || !chunkHTRGOBZF_cjs.$isStr(salt) || !chunkHTRGOBZF_cjs.$isPlainObj(options)) {
|
|
459
458
|
return false;
|
|
460
459
|
}
|
|
461
460
|
const digest = options.digest ?? "sha512";
|
|
462
|
-
if (!(digest in
|
|
463
|
-
const digestAlgo =
|
|
461
|
+
if (!(digest in chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS)) return false;
|
|
462
|
+
const digestAlgo = chunkHTRGOBZF_cjs.DIGEST_ALGORITHMS[digest];
|
|
464
463
|
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
465
|
-
if (!
|
|
464
|
+
if (!chunkHTRGOBZF_cjs.CIPHER_ENCODING.includes(inputEncoding)) return false;
|
|
466
465
|
const iterations = options.iterations ?? 32e4;
|
|
467
466
|
if (typeof iterations !== "number" || iterations < 1e3) return false;
|
|
468
467
|
const keyLength = options.keyLength ?? 64;
|
|
@@ -504,15 +503,12 @@ async function $verifyPassword(password, hashedPassword, salt, options) {
|
|
|
504
503
|
}
|
|
505
504
|
|
|
506
505
|
// src/web/kit.ts
|
|
507
|
-
function isWebSecretKey(x) {
|
|
508
|
-
return chunkX4CS7UXE_cjs.$isSecretKey(x, "web") !== null;
|
|
509
|
-
}
|
|
510
506
|
function tryGenerateUuid() {
|
|
511
507
|
return $generateUuid();
|
|
512
508
|
}
|
|
513
509
|
function generateUuid() {
|
|
514
510
|
const { result, error } = $generateUuid();
|
|
515
|
-
if (error) throw new Error(
|
|
511
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
516
512
|
return result;
|
|
517
513
|
}
|
|
518
514
|
async function tryCreateSecretKey(secret, options = {}) {
|
|
@@ -520,7 +516,7 @@ async function tryCreateSecretKey(secret, options = {}) {
|
|
|
520
516
|
}
|
|
521
517
|
async function createSecretKey(secret, options = {}) {
|
|
522
518
|
const { result, error } = await $createSecretKey(secret, options);
|
|
523
|
-
if (error) throw new Error(
|
|
519
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
524
520
|
return result;
|
|
525
521
|
}
|
|
526
522
|
async function tryEncrypt(data, secretKey, options = {}) {
|
|
@@ -528,7 +524,7 @@ async function tryEncrypt(data, secretKey, options = {}) {
|
|
|
528
524
|
}
|
|
529
525
|
async function encrypt(data, secretKey, options = {}) {
|
|
530
526
|
const { result, error } = await $encrypt(data, secretKey, options);
|
|
531
|
-
if (error) throw new Error(
|
|
527
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
532
528
|
return result;
|
|
533
529
|
}
|
|
534
530
|
async function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
@@ -536,7 +532,7 @@ async function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
|
536
532
|
}
|
|
537
533
|
async function decrypt(encrypted, secretKey, options = {}) {
|
|
538
534
|
const { result, error } = await $decrypt(encrypted, secretKey, options);
|
|
539
|
-
if (error) throw new Error(
|
|
535
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
540
536
|
return result;
|
|
541
537
|
}
|
|
542
538
|
async function tryEncryptObj(obj, secretKey, options = {}) {
|
|
@@ -544,7 +540,7 @@ async function tryEncryptObj(obj, secretKey, options = {}) {
|
|
|
544
540
|
}
|
|
545
541
|
async function encryptObj(obj, secretKey, options = {}) {
|
|
546
542
|
const { result, error } = await $encryptObj(obj, secretKey, options);
|
|
547
|
-
if (error) throw new Error(
|
|
543
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
548
544
|
return result;
|
|
549
545
|
}
|
|
550
546
|
async function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
@@ -552,7 +548,7 @@ async function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
|
552
548
|
}
|
|
553
549
|
async function decryptObj(encrypted, secretKey, options = {}) {
|
|
554
550
|
const { result, error } = await $decryptObj(encrypted, secretKey, options);
|
|
555
|
-
if (error) throw new Error(
|
|
551
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
556
552
|
return result;
|
|
557
553
|
}
|
|
558
554
|
async function tryHash(data, options = {}) {
|
|
@@ -560,7 +556,7 @@ async function tryHash(data, options = {}) {
|
|
|
560
556
|
}
|
|
561
557
|
async function hash(data, options = {}) {
|
|
562
558
|
const { result, error } = await $hash(data, options);
|
|
563
|
-
if (error) throw new Error(
|
|
559
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
564
560
|
return result;
|
|
565
561
|
}
|
|
566
562
|
async function tryHashPassword(password, options = {}) {
|
|
@@ -568,7 +564,7 @@ async function tryHashPassword(password, options = {}) {
|
|
|
568
564
|
}
|
|
569
565
|
async function hashPassword(password, options = {}) {
|
|
570
566
|
const { result, salt, error } = await $hashPassword(password, options);
|
|
571
|
-
if (error) throw new Error(
|
|
567
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
572
568
|
return { result, salt };
|
|
573
569
|
}
|
|
574
570
|
async function verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
@@ -579,7 +575,7 @@ function tryConvertStrToBytes(data, inputEncoding = "utf8") {
|
|
|
579
575
|
}
|
|
580
576
|
function convertStrToBytes(data, inputEncoding = "utf8") {
|
|
581
577
|
const { result, error } = $convertStrToBytes(data, inputEncoding);
|
|
582
|
-
if (error) throw new Error(
|
|
578
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
583
579
|
return result;
|
|
584
580
|
}
|
|
585
581
|
function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
@@ -587,7 +583,7 @@ function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
|
587
583
|
}
|
|
588
584
|
function convertBytesToStr(data, outputEncoding = "utf8") {
|
|
589
585
|
const { result, error } = $convertBytesToStr(data, outputEncoding);
|
|
590
|
-
if (error) throw new Error(
|
|
586
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
591
587
|
return result;
|
|
592
588
|
}
|
|
593
589
|
function tryConvertEncoding(data, from, to) {
|
|
@@ -595,7 +591,7 @@ function tryConvertEncoding(data, from, to) {
|
|
|
595
591
|
}
|
|
596
592
|
function convertEncoding(data, from, to) {
|
|
597
593
|
const { result, error } = $convertEncoding(data, from, to);
|
|
598
|
-
if (error) throw new Error(
|
|
594
|
+
if (error) throw new Error(chunkHTRGOBZF_cjs.$fmtResultErr(error));
|
|
599
595
|
return result;
|
|
600
596
|
}
|
|
601
597
|
|
|
@@ -610,7 +606,6 @@ exports.encryptObj = encryptObj;
|
|
|
610
606
|
exports.generateUuid = generateUuid;
|
|
611
607
|
exports.hash = hash;
|
|
612
608
|
exports.hashPassword = hashPassword;
|
|
613
|
-
exports.isWebSecretKey = isWebSecretKey;
|
|
614
609
|
exports.kit_exports = kit_exports;
|
|
615
610
|
exports.tryConvertBytesToStr = tryConvertBytesToStr;
|
|
616
611
|
exports.tryConvertEncoding = tryConvertEncoding;
|
|
@@ -624,5 +619,5 @@ exports.tryGenerateUuid = tryGenerateUuid;
|
|
|
624
619
|
exports.tryHash = tryHash;
|
|
625
620
|
exports.tryHashPassword = tryHashPassword;
|
|
626
621
|
exports.verifyPassword = verifyPassword;
|
|
627
|
-
//# sourceMappingURL=chunk-
|
|
628
|
-
//# sourceMappingURL=chunk-
|
|
622
|
+
//# sourceMappingURL=chunk-BMX42IZM.cjs.map
|
|
623
|
+
//# sourceMappingURL=chunk-BMX42IZM.cjs.map
|