cipher-kit 2.0.0-beta.3 → 2.0.0-beta.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/{chunk-BNQERV4S.js → chunk-LHB5NXYW.js} +71 -84
- package/dist/chunk-LHB5NXYW.js.map +1 -0
- package/dist/{chunk-6GBH7YTP.js → chunk-N5T4PNG7.js} +67 -81
- package/dist/chunk-N5T4PNG7.js.map +1 -0
- package/dist/{chunk-UHP3PPXP.cjs → chunk-OL2AIXWK.cjs} +150 -165
- package/dist/chunk-OL2AIXWK.cjs.map +1 -0
- package/dist/{chunk-ZJ32WGAA.cjs → chunk-UIV6DG54.cjs} +15 -12
- package/dist/chunk-UIV6DG54.cjs.map +1 -0
- package/dist/{chunk-NKLNWTQA.cjs → chunk-XIWV7XVI.cjs} +159 -173
- package/dist/chunk-XIWV7XVI.cjs.map +1 -0
- package/dist/{chunk-YPYDYYV2.js → chunk-YMNOTRET.js} +13 -11
- package/dist/chunk-YMNOTRET.js.map +1 -0
- package/dist/export-D1Vh79Qw.d.ts +417 -0
- package/dist/export-DGrELdz_.d.ts +416 -0
- package/dist/export-DXRl-ncG.d.cts +417 -0
- package/dist/export-Gd8hafl6.d.cts +416 -0
- package/dist/index.cjs +14 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +52 -4
- package/dist/index.d.ts +52 -4
- package/dist/index.js +9 -3
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +34 -42
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/validate-Cb7IOrPo.d.cts +373 -0
- package/dist/validate-Cb7IOrPo.d.ts +373 -0
- package/dist/web-api.cjs +34 -42
- 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-6GBH7YTP.js.map +0 -1
- package/dist/chunk-BNQERV4S.js.map +0 -1
- package/dist/chunk-NKLNWTQA.cjs.map +0 -1
- package/dist/chunk-UHP3PPXP.cjs.map +0 -1
- package/dist/chunk-YPYDYYV2.js.map +0 -1
- package/dist/chunk-ZJ32WGAA.cjs.map +0 -1
- package/dist/export--ndIQ3j3.d.cts +0 -271
- package/dist/export-C2M5UPLX.d.cts +0 -270
- package/dist/export-CPUbAFZA.d.ts +0 -271
- package/dist/export-v9ULdDL0.d.ts +0 -270
- package/dist/validate-CULVlPck.d.cts +0 -157
- package/dist/validate-CULVlPck.d.ts +0 -157
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkUIV6DG54_cjs = require('./chunk-UIV6DG54.cjs');
|
|
4
4
|
|
|
5
5
|
// src/web/kit.ts
|
|
6
6
|
var kit_exports = {};
|
|
7
|
-
|
|
7
|
+
chunkUIV6DG54_cjs.__export(kit_exports, {
|
|
8
8
|
convertBytesToStr: () => convertBytesToStr,
|
|
9
9
|
convertEncoding: () => convertEncoding,
|
|
10
10
|
convertStrToBytes: () => convertStrToBytes,
|
|
@@ -15,8 +15,8 @@ chunkZJ32WGAA_cjs.__export(kit_exports, {
|
|
|
15
15
|
encryptObj: () => encryptObj,
|
|
16
16
|
generateUuid: () => generateUuid,
|
|
17
17
|
hash: () => hash,
|
|
18
|
-
hashObj: () => hashObj,
|
|
19
18
|
hashPassword: () => hashPassword,
|
|
19
|
+
isWebSecretKey: () => isWebSecretKey,
|
|
20
20
|
tryConvertBytesToStr: () => tryConvertBytesToStr,
|
|
21
21
|
tryConvertEncoding: () => tryConvertEncoding,
|
|
22
22
|
tryConvertStrToBytes: () => tryConvertStrToBytes,
|
|
@@ -27,7 +27,6 @@ chunkZJ32WGAA_cjs.__export(kit_exports, {
|
|
|
27
27
|
tryEncryptObj: () => tryEncryptObj,
|
|
28
28
|
tryGenerateUuid: () => tryGenerateUuid,
|
|
29
29
|
tryHash: () => tryHash,
|
|
30
|
-
tryHashObj: () => tryHashObj,
|
|
31
30
|
tryHashPassword: () => tryHashPassword,
|
|
32
31
|
verifyPassword: () => verifyPassword
|
|
33
32
|
});
|
|
@@ -36,34 +35,34 @@ chunkZJ32WGAA_cjs.__export(kit_exports, {
|
|
|
36
35
|
var textEncoder = new TextEncoder();
|
|
37
36
|
var textDecoder = new TextDecoder();
|
|
38
37
|
function $convertStrToBytes(data, inputEncoding = "utf8") {
|
|
39
|
-
if (!
|
|
40
|
-
return
|
|
38
|
+
if (!chunkUIV6DG54_cjs.$isStr(data)) {
|
|
39
|
+
return chunkUIV6DG54_cjs.$err({
|
|
41
40
|
msg: "Crypto Web API - String to Bytes: Empty data",
|
|
42
41
|
desc: "Data must be a non-empty string"
|
|
43
42
|
});
|
|
44
43
|
}
|
|
45
|
-
if (!
|
|
46
|
-
return
|
|
44
|
+
if (!chunkUIV6DG54_cjs.ENCODING.includes(inputEncoding)) {
|
|
45
|
+
return chunkUIV6DG54_cjs.$err({
|
|
47
46
|
msg: `Crypto Web API - String to Bytes: Unsupported encoding: ${inputEncoding}`,
|
|
48
47
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
49
48
|
});
|
|
50
49
|
}
|
|
51
50
|
try {
|
|
52
51
|
const bytes = strToBytes[inputEncoding](data);
|
|
53
|
-
return
|
|
52
|
+
return chunkUIV6DG54_cjs.$ok({ result: bytes });
|
|
54
53
|
} catch (error) {
|
|
55
|
-
return
|
|
54
|
+
return chunkUIV6DG54_cjs.$err({ msg: "Crypto Web API - String to Bytes: Failed to convert data", desc: chunkUIV6DG54_cjs.$fmtError(error) });
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
function $convertBytesToStr(data, outputEncoding = "utf8") {
|
|
59
58
|
if (!(data instanceof ArrayBuffer || data instanceof Uint8Array)) {
|
|
60
|
-
return
|
|
59
|
+
return chunkUIV6DG54_cjs.$err({
|
|
61
60
|
msg: "Crypto Web API - Bytes to String: Invalid data type",
|
|
62
61
|
desc: "Data must be an ArrayBuffer or Uint8Array"
|
|
63
62
|
});
|
|
64
63
|
}
|
|
65
|
-
if (!
|
|
66
|
-
return
|
|
64
|
+
if (!chunkUIV6DG54_cjs.ENCODING.includes(outputEncoding)) {
|
|
65
|
+
return chunkUIV6DG54_cjs.$err({
|
|
67
66
|
msg: `Crypto Web API - Bytes to String: Unsupported encoding: ${outputEncoding}`,
|
|
68
67
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
69
68
|
});
|
|
@@ -71,29 +70,29 @@ function $convertBytesToStr(data, outputEncoding = "utf8") {
|
|
|
71
70
|
try {
|
|
72
71
|
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
73
72
|
const str = bytesToStr[outputEncoding](bytes);
|
|
74
|
-
return
|
|
73
|
+
return chunkUIV6DG54_cjs.$ok(str);
|
|
75
74
|
} catch (error) {
|
|
76
|
-
return
|
|
75
|
+
return chunkUIV6DG54_cjs.$err({ msg: "Crypto Web API - Bytes to String: Failed to convert data", desc: chunkUIV6DG54_cjs.$fmtError(error) });
|
|
77
76
|
}
|
|
78
77
|
}
|
|
79
78
|
function $convertEncoding(data, from, to) {
|
|
80
|
-
if (!
|
|
81
|
-
return
|
|
79
|
+
if (!chunkUIV6DG54_cjs.$isStr(data)) {
|
|
80
|
+
return chunkUIV6DG54_cjs.$err({
|
|
82
81
|
msg: "Crypto Web API - Convert Format: Empty data",
|
|
83
82
|
desc: "Data must be a non-empty string"
|
|
84
83
|
});
|
|
85
84
|
}
|
|
86
|
-
if (!
|
|
87
|
-
return
|
|
85
|
+
if (!chunkUIV6DG54_cjs.ENCODING.includes(from) || !chunkUIV6DG54_cjs.ENCODING.includes(to)) {
|
|
86
|
+
return chunkUIV6DG54_cjs.$err({
|
|
88
87
|
msg: `Crypto Web API - Convert Format: Unsupported encoding: from ${from} to ${to}`,
|
|
89
88
|
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
90
89
|
});
|
|
91
90
|
}
|
|
92
91
|
const bytes = $convertStrToBytes(data, from);
|
|
93
|
-
if (bytes.error) return
|
|
92
|
+
if (bytes.error) return chunkUIV6DG54_cjs.$err({ msg: bytes.error.message, desc: bytes.error.description });
|
|
94
93
|
const str = $convertBytesToStr(bytes.result, to);
|
|
95
|
-
if (str.error) return
|
|
96
|
-
return
|
|
94
|
+
if (str.error) return chunkUIV6DG54_cjs.$err({ msg: str.error.message, desc: str.error.description });
|
|
95
|
+
return chunkUIV6DG54_cjs.$ok({ result: str.result });
|
|
97
96
|
}
|
|
98
97
|
var strToBytes = {
|
|
99
98
|
base64: $fromBase64,
|
|
@@ -159,45 +158,45 @@ function $fromHex(data) {
|
|
|
159
158
|
// src/web/web-encrypt.ts
|
|
160
159
|
function $generateUuid() {
|
|
161
160
|
try {
|
|
162
|
-
return
|
|
161
|
+
return chunkUIV6DG54_cjs.$ok(crypto.randomUUID());
|
|
163
162
|
} catch (error) {
|
|
164
|
-
return
|
|
163
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "UUID Generation")}: Failed to generate UUID`, desc: chunkUIV6DG54_cjs.$fmtError(error) });
|
|
165
164
|
}
|
|
166
165
|
}
|
|
167
166
|
async function $createSecretKey(secret, options = {}) {
|
|
168
|
-
if (!
|
|
169
|
-
return
|
|
167
|
+
if (!chunkUIV6DG54_cjs.$isStr(secret)) {
|
|
168
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "Key Generation")}: Empty Secret`, desc: "Secret must be a non-empty string" });
|
|
170
169
|
}
|
|
171
170
|
const algorithm = options.algorithm ?? "aes256gcm";
|
|
172
|
-
if (!(algorithm in
|
|
173
|
-
return
|
|
174
|
-
msg:
|
|
175
|
-
desc: `Supported algorithms are: ${Object.keys(
|
|
171
|
+
if (!(algorithm in chunkUIV6DG54_cjs.ENCRYPTION_ALGORITHMS)) {
|
|
172
|
+
return chunkUIV6DG54_cjs.$err({
|
|
173
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Key Generation")}: Unsupported algorithm: ${algorithm}`,
|
|
174
|
+
desc: `Supported algorithms are: ${Object.keys(chunkUIV6DG54_cjs.ENCRYPTION_ALGORITHMS).join(", ")}`
|
|
176
175
|
});
|
|
177
176
|
}
|
|
178
177
|
const digest = options.digest ?? "sha256";
|
|
179
|
-
if (!(digest in
|
|
180
|
-
return
|
|
181
|
-
msg:
|
|
182
|
-
desc: `Supported digests are: ${Object.keys(
|
|
178
|
+
if (!(digest in chunkUIV6DG54_cjs.DIGEST_ALGORITHMS)) {
|
|
179
|
+
return chunkUIV6DG54_cjs.$err({
|
|
180
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Key Generation")}: Unsupported digest: ${digest}`,
|
|
181
|
+
desc: `Supported digests are: ${Object.keys(chunkUIV6DG54_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
183
182
|
});
|
|
184
183
|
}
|
|
185
184
|
const salt = options.salt ?? "cipher-kit-salt";
|
|
186
|
-
if (!
|
|
187
|
-
return
|
|
188
|
-
msg: "
|
|
185
|
+
if (!chunkUIV6DG54_cjs.$isStr(salt, 8)) {
|
|
186
|
+
return chunkUIV6DG54_cjs.$err({
|
|
187
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Key Generation")}: Weak salt`,
|
|
189
188
|
desc: "Salt must be a non-empty string with at least 8 characters"
|
|
190
189
|
});
|
|
191
190
|
}
|
|
192
191
|
const info = options.info ?? "cipher-kit";
|
|
193
|
-
if (!
|
|
194
|
-
return
|
|
195
|
-
msg: "
|
|
192
|
+
if (!chunkUIV6DG54_cjs.$isStr(info)) {
|
|
193
|
+
return chunkUIV6DG54_cjs.$err({
|
|
194
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Key Generation")}: Invalid info`,
|
|
196
195
|
desc: "Info must be a non-empty string"
|
|
197
196
|
});
|
|
198
197
|
}
|
|
199
|
-
const encryptAlgo =
|
|
200
|
-
const digestAlgo =
|
|
198
|
+
const encryptAlgo = chunkUIV6DG54_cjs.ENCRYPTION_ALGORITHMS[algorithm];
|
|
199
|
+
const digestAlgo = chunkUIV6DG54_cjs.DIGEST_ALGORITHMS[digest];
|
|
201
200
|
try {
|
|
202
201
|
const ikm = await crypto.subtle.importKey("raw", textEncoder.encode(secret.normalize("NFKC")), "HKDF", false, [
|
|
203
202
|
"deriveKey"
|
|
@@ -220,38 +219,37 @@ async function $createSecretKey(secret, options = {}) {
|
|
|
220
219
|
algorithm,
|
|
221
220
|
key
|
|
222
221
|
});
|
|
223
|
-
return
|
|
222
|
+
return chunkUIV6DG54_cjs.$ok({ result: secretKey });
|
|
224
223
|
} catch (error) {
|
|
225
|
-
return
|
|
226
|
-
msg: "
|
|
227
|
-
desc:
|
|
224
|
+
return chunkUIV6DG54_cjs.$err({
|
|
225
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Key Generation")}: Failed to create secret key`,
|
|
226
|
+
desc: chunkUIV6DG54_cjs.$fmtError(error)
|
|
228
227
|
});
|
|
229
228
|
}
|
|
230
229
|
}
|
|
231
230
|
async function $encrypt(data, secretKey, options = {}) {
|
|
232
|
-
if (!
|
|
233
|
-
return
|
|
234
|
-
msg: "
|
|
231
|
+
if (!chunkUIV6DG54_cjs.$isStr(data)) {
|
|
232
|
+
return chunkUIV6DG54_cjs.$err({
|
|
233
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Encryption")}: Empty data for encryption`,
|
|
235
234
|
desc: "Data must be a non-empty string"
|
|
236
235
|
});
|
|
237
236
|
}
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
237
|
+
const encoding = options.encoding ?? "base64url";
|
|
238
|
+
if (!chunkUIV6DG54_cjs.CIPHER_ENCODING.includes(encoding)) {
|
|
239
|
+
return chunkUIV6DG54_cjs.$err({
|
|
240
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Encryption")}: Unsupported output encoding: ${encoding}`,
|
|
241
|
+
desc: "Use base64, base64url, or hex"
|
|
244
242
|
});
|
|
245
243
|
}
|
|
246
|
-
const injectedKey =
|
|
244
|
+
const injectedKey = chunkUIV6DG54_cjs.$isSecretKey(secretKey, "web");
|
|
247
245
|
if (!injectedKey) {
|
|
248
|
-
return
|
|
249
|
-
msg: "
|
|
246
|
+
return chunkUIV6DG54_cjs.$err({
|
|
247
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Encryption")}: Invalid Secret Key`,
|
|
250
248
|
desc: "Expected a Web SecretKey"
|
|
251
249
|
});
|
|
252
250
|
}
|
|
253
|
-
const { result, error } = $convertStrToBytes(data,
|
|
254
|
-
if (error) return
|
|
251
|
+
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
252
|
+
if (error) return chunkUIV6DG54_cjs.$err(error);
|
|
255
253
|
try {
|
|
256
254
|
const iv = crypto.getRandomValues(new Uint8Array(injectedKey.injected.ivLength));
|
|
257
255
|
const cipherWithTag = await crypto.subtle.encrypt(
|
|
@@ -259,54 +257,53 @@ async function $encrypt(data, secretKey, options = {}) {
|
|
|
259
257
|
injectedKey.key,
|
|
260
258
|
result
|
|
261
259
|
);
|
|
262
|
-
const ivStr = $convertBytesToStr(iv,
|
|
263
|
-
const cipherStr = $convertBytesToStr(cipherWithTag,
|
|
260
|
+
const ivStr = $convertBytesToStr(iv, encoding);
|
|
261
|
+
const cipherStr = $convertBytesToStr(cipherWithTag, encoding);
|
|
264
262
|
if (ivStr.error || cipherStr.error) {
|
|
265
|
-
return
|
|
266
|
-
msg: "
|
|
267
|
-
desc: `Conversion error: ${ivStr.error || cipherStr.error}`
|
|
263
|
+
return chunkUIV6DG54_cjs.$err({
|
|
264
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Encryption")}: Failed to convert IV or encrypted data`,
|
|
265
|
+
desc: `Conversion error: ${chunkUIV6DG54_cjs.$fmtResultErr(ivStr.error || cipherStr.error)}`
|
|
268
266
|
});
|
|
269
267
|
}
|
|
270
|
-
return
|
|
268
|
+
return chunkUIV6DG54_cjs.$ok(`${ivStr.result}.${cipherStr.result}.`);
|
|
271
269
|
} catch (error2) {
|
|
272
|
-
return
|
|
270
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "Encryption")}: Failed to encrypt data`, desc: chunkUIV6DG54_cjs.$fmtError(error2) });
|
|
273
271
|
}
|
|
274
272
|
}
|
|
275
273
|
async function $decrypt(encrypted, secretKey, options = {}) {
|
|
276
|
-
if (
|
|
277
|
-
return
|
|
278
|
-
msg: "
|
|
274
|
+
if (chunkUIV6DG54_cjs.matchPattern(encrypted, "web") === false) {
|
|
275
|
+
return chunkUIV6DG54_cjs.$err({
|
|
276
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Decryption")}: Invalid encrypted data format`,
|
|
279
277
|
desc: 'Encrypted data must be in the format "iv.cipherWithTag."'
|
|
280
278
|
});
|
|
281
279
|
}
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
280
|
+
const encoding = options.encoding ?? "base64url";
|
|
281
|
+
if (!chunkUIV6DG54_cjs.CIPHER_ENCODING.includes(encoding)) {
|
|
282
|
+
return chunkUIV6DG54_cjs.$err({
|
|
283
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Decryption")}: Unsupported input encoding: ${encoding}`,
|
|
284
|
+
desc: "Use base64, base64url, or hex"
|
|
288
285
|
});
|
|
289
286
|
}
|
|
290
287
|
const [iv, encryptedWithTag] = encrypted.split(".", 3);
|
|
291
|
-
if (!
|
|
292
|
-
return
|
|
293
|
-
msg: "
|
|
288
|
+
if (!chunkUIV6DG54_cjs.$isStr(iv) || !chunkUIV6DG54_cjs.$isStr(encryptedWithTag)) {
|
|
289
|
+
return chunkUIV6DG54_cjs.$err({
|
|
290
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Decryption")}: Invalid encrypted data`,
|
|
294
291
|
desc: "Encrypted data must contain valid IV, encrypted and tag components"
|
|
295
292
|
});
|
|
296
293
|
}
|
|
297
|
-
const injectedKey =
|
|
294
|
+
const injectedKey = chunkUIV6DG54_cjs.$isSecretKey(secretKey, "web");
|
|
298
295
|
if (!injectedKey) {
|
|
299
|
-
return
|
|
300
|
-
msg: "
|
|
296
|
+
return chunkUIV6DG54_cjs.$err({
|
|
297
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Decryption")}: Invalid Secret Key`,
|
|
301
298
|
desc: "Expected a Web SecretKey"
|
|
302
299
|
});
|
|
303
300
|
}
|
|
304
|
-
const ivBytes = $convertStrToBytes(iv,
|
|
305
|
-
const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag,
|
|
301
|
+
const ivBytes = $convertStrToBytes(iv, encoding);
|
|
302
|
+
const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, encoding);
|
|
306
303
|
if (ivBytes.error || cipherWithTagBytes.error) {
|
|
307
|
-
return
|
|
308
|
-
msg: "
|
|
309
|
-
desc: `Conversion error: ${ivBytes.error || cipherWithTagBytes.error}`
|
|
304
|
+
return chunkUIV6DG54_cjs.$err({
|
|
305
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Decryption")}: Failed to convert IV or encrypted data`,
|
|
306
|
+
desc: `Conversion error: ${chunkUIV6DG54_cjs.$fmtResultErr(ivBytes.error || cipherWithTagBytes.error)}`
|
|
310
307
|
});
|
|
311
308
|
}
|
|
312
309
|
try {
|
|
@@ -317,93 +314,87 @@ async function $decrypt(encrypted, secretKey, options = {}) {
|
|
|
317
314
|
);
|
|
318
315
|
return $convertBytesToStr(decrypted, "utf8");
|
|
319
316
|
} catch (error) {
|
|
320
|
-
return
|
|
317
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "Decryption")}: Failed to decrypt data`, desc: chunkUIV6DG54_cjs.$fmtError(error) });
|
|
321
318
|
}
|
|
322
319
|
}
|
|
323
320
|
async function $encryptObj(data, secretKey, options = {}) {
|
|
324
|
-
const { result, error } =
|
|
325
|
-
if (error) return
|
|
321
|
+
const { result, error } = chunkUIV6DG54_cjs.$stringifyObj(data);
|
|
322
|
+
if (error) return chunkUIV6DG54_cjs.$err(error);
|
|
326
323
|
return await $encrypt(result, secretKey, options);
|
|
327
324
|
}
|
|
328
325
|
async function $decryptObj(encrypted, secretKey, options = {}) {
|
|
329
326
|
const { result, error } = await $decrypt(encrypted, secretKey, options);
|
|
330
|
-
if (error) return
|
|
331
|
-
return
|
|
327
|
+
if (error) return chunkUIV6DG54_cjs.$err(error);
|
|
328
|
+
return chunkUIV6DG54_cjs.$parseToObj(result);
|
|
332
329
|
}
|
|
333
330
|
async function $hash(data, options = {}) {
|
|
334
|
-
if (!
|
|
335
|
-
return
|
|
336
|
-
}
|
|
337
|
-
const
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
desc: "Use base64, base64url, hex, utf8, or latin1"
|
|
331
|
+
if (!chunkUIV6DG54_cjs.$isStr(data)) {
|
|
332
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "Hashing")}: Empty data for hashing`, desc: "Data must be a non-empty string" });
|
|
333
|
+
}
|
|
334
|
+
const encoding = options.encoding ?? "base64url";
|
|
335
|
+
if (!chunkUIV6DG54_cjs.CIPHER_ENCODING.includes(encoding)) {
|
|
336
|
+
return chunkUIV6DG54_cjs.$err({
|
|
337
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Hashing")}: Unsupported output encoding: ${encoding}`,
|
|
338
|
+
desc: "Use base64, base64url, or hex"
|
|
343
339
|
});
|
|
344
340
|
}
|
|
345
341
|
const digest = options.digest ?? "sha256";
|
|
346
|
-
if (!(digest in
|
|
347
|
-
return
|
|
348
|
-
msg:
|
|
349
|
-
desc: `Supported digests are: ${Object.keys(
|
|
342
|
+
if (!(digest in chunkUIV6DG54_cjs.DIGEST_ALGORITHMS)) {
|
|
343
|
+
return chunkUIV6DG54_cjs.$err({
|
|
344
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Hashing")}: Unsupported digest: ${digest}`,
|
|
345
|
+
desc: `Supported digests are: ${Object.keys(chunkUIV6DG54_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
350
346
|
});
|
|
351
347
|
}
|
|
352
|
-
const digestAlgo =
|
|
353
|
-
const { result, error } = $convertStrToBytes(data,
|
|
354
|
-
if (error) return
|
|
348
|
+
const digestAlgo = chunkUIV6DG54_cjs.DIGEST_ALGORITHMS[digest];
|
|
349
|
+
const { result, error } = $convertStrToBytes(data, "utf8");
|
|
350
|
+
if (error) return chunkUIV6DG54_cjs.$err(error);
|
|
355
351
|
try {
|
|
356
352
|
const hashed = await crypto.subtle.digest(digestAlgo.web, result);
|
|
357
|
-
return $convertBytesToStr(hashed,
|
|
353
|
+
return $convertBytesToStr(hashed, encoding);
|
|
358
354
|
} catch (error2) {
|
|
359
|
-
return
|
|
355
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "Hashing")}: Failed to hash data`, desc: chunkUIV6DG54_cjs.$fmtError(error2) });
|
|
360
356
|
}
|
|
361
357
|
}
|
|
362
|
-
async function $hashObj(data, options = {}) {
|
|
363
|
-
const { result, error } = chunkZJ32WGAA_cjs.$stringifyObj(data);
|
|
364
|
-
if (error) return chunkZJ32WGAA_cjs.$err(error);
|
|
365
|
-
return await $hash(result, options);
|
|
366
|
-
}
|
|
367
358
|
async function $hashPassword(password, options = {}) {
|
|
368
|
-
if (!
|
|
369
|
-
return
|
|
370
|
-
msg: "
|
|
359
|
+
if (!chunkUIV6DG54_cjs.$isStr(password)) {
|
|
360
|
+
return chunkUIV6DG54_cjs.$err({
|
|
361
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Empty password`,
|
|
371
362
|
desc: "Password must be a non-empty string"
|
|
372
363
|
});
|
|
373
364
|
}
|
|
374
365
|
const digest = options.digest ?? "sha512";
|
|
375
|
-
if (!(digest in
|
|
376
|
-
return
|
|
377
|
-
msg:
|
|
378
|
-
desc: `Supported digests are: ${Object.keys(
|
|
366
|
+
if (!(digest in chunkUIV6DG54_cjs.DIGEST_ALGORITHMS)) {
|
|
367
|
+
return chunkUIV6DG54_cjs.$err({
|
|
368
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Unsupported digest: ${digest}`,
|
|
369
|
+
desc: `Supported digests are: ${Object.keys(chunkUIV6DG54_cjs.DIGEST_ALGORITHMS).join(", ")}`
|
|
379
370
|
});
|
|
380
371
|
}
|
|
381
|
-
const digestAlgo =
|
|
382
|
-
const
|
|
383
|
-
if (!
|
|
384
|
-
return
|
|
385
|
-
msg:
|
|
386
|
-
desc: "Use base64, base64url,
|
|
372
|
+
const digestAlgo = chunkUIV6DG54_cjs.DIGEST_ALGORITHMS[digest];
|
|
373
|
+
const encoding = options.encoding ?? "base64url";
|
|
374
|
+
if (!chunkUIV6DG54_cjs.CIPHER_ENCODING.includes(encoding)) {
|
|
375
|
+
return chunkUIV6DG54_cjs.$err({
|
|
376
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Unsupported output encoding: ${encoding}`,
|
|
377
|
+
desc: "Use base64, base64url, or hex"
|
|
387
378
|
});
|
|
388
379
|
}
|
|
389
380
|
const saltLength = options.saltLength ?? 16;
|
|
390
381
|
if (typeof saltLength !== "number" || saltLength < 8) {
|
|
391
|
-
return
|
|
392
|
-
msg: "
|
|
382
|
+
return chunkUIV6DG54_cjs.$err({
|
|
383
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Weak salt length`,
|
|
393
384
|
desc: "Salt length must be a number and at least 8 bytes (recommended 16)"
|
|
394
385
|
});
|
|
395
386
|
}
|
|
396
387
|
const iterations = options.iterations ?? 32e4;
|
|
397
388
|
if (typeof iterations !== "number" || iterations < 1e3) {
|
|
398
|
-
return
|
|
399
|
-
msg: "
|
|
389
|
+
return chunkUIV6DG54_cjs.$err({
|
|
390
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Weak iteration count`,
|
|
400
391
|
desc: "Iterations must be a number and at least 1000 (recommended 320,000 or more)"
|
|
401
392
|
});
|
|
402
393
|
}
|
|
403
394
|
const keyLength = options.keyLength ?? 64;
|
|
404
395
|
if (typeof keyLength !== "number" || keyLength < 16) {
|
|
405
|
-
return
|
|
406
|
-
msg: "
|
|
396
|
+
return chunkUIV6DG54_cjs.$err({
|
|
397
|
+
msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Weak key length`,
|
|
407
398
|
desc: "Key length must be a number and at least 16 bytes (recommended 64)"
|
|
408
399
|
});
|
|
409
400
|
}
|
|
@@ -421,29 +412,29 @@ async function $hashPassword(password, options = {}) {
|
|
|
421
412
|
baseKey,
|
|
422
413
|
keyLength * 8
|
|
423
414
|
);
|
|
424
|
-
const saltStr = $convertBytesToStr(salt,
|
|
425
|
-
if (saltStr.error) return
|
|
426
|
-
const hashedPasswordStr = $convertBytesToStr(bits,
|
|
427
|
-
if (hashedPasswordStr.error) return
|
|
428
|
-
return
|
|
415
|
+
const saltStr = $convertBytesToStr(salt, encoding);
|
|
416
|
+
if (saltStr.error) return chunkUIV6DG54_cjs.$err(saltStr.error);
|
|
417
|
+
const hashedPasswordStr = $convertBytesToStr(bits, encoding);
|
|
418
|
+
if (hashedPasswordStr.error) return chunkUIV6DG54_cjs.$err(hashedPasswordStr.error);
|
|
419
|
+
return chunkUIV6DG54_cjs.$ok({ hash: hashedPasswordStr.result, salt: saltStr.result });
|
|
429
420
|
} catch (error) {
|
|
430
|
-
return
|
|
421
|
+
return chunkUIV6DG54_cjs.$err({ msg: `${chunkUIV6DG54_cjs.title("web", "Password Hashing")}: Failed to hash password`, desc: chunkUIV6DG54_cjs.$fmtError(error) });
|
|
431
422
|
}
|
|
432
423
|
}
|
|
433
424
|
async function $verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
434
|
-
if (!
|
|
425
|
+
if (!chunkUIV6DG54_cjs.$isStr(password) || !chunkUIV6DG54_cjs.$isStr(hashedPassword) || !chunkUIV6DG54_cjs.$isStr(salt)) return false;
|
|
435
426
|
const digest = options.digest ?? "sha512";
|
|
436
|
-
if (!(digest in
|
|
437
|
-
const digestAlgo =
|
|
438
|
-
const
|
|
439
|
-
if (!
|
|
427
|
+
if (!(digest in chunkUIV6DG54_cjs.DIGEST_ALGORITHMS)) return false;
|
|
428
|
+
const digestAlgo = chunkUIV6DG54_cjs.DIGEST_ALGORITHMS[digest];
|
|
429
|
+
const encoding = options.encoding ?? "base64url";
|
|
430
|
+
if (!chunkUIV6DG54_cjs.CIPHER_ENCODING.includes(encoding)) return false;
|
|
440
431
|
const iterations = options.iterations ?? 32e4;
|
|
441
432
|
if (typeof iterations !== "number" || iterations < 1e3) return false;
|
|
442
433
|
const keyLength = options.keyLength ?? 64;
|
|
443
434
|
if (typeof keyLength !== "number" || keyLength < 16) return false;
|
|
444
|
-
const saltBytes = $convertStrToBytes(salt,
|
|
435
|
+
const saltBytes = $convertStrToBytes(salt, encoding);
|
|
445
436
|
if (saltBytes.error) return false;
|
|
446
|
-
const hashedPasswordBytes = $convertStrToBytes(hashedPassword,
|
|
437
|
+
const hashedPasswordBytes = $convertStrToBytes(hashedPassword, encoding);
|
|
447
438
|
if (hashedPasswordBytes.error) return false;
|
|
448
439
|
try {
|
|
449
440
|
const baseKey = await crypto.subtle.importKey(
|
|
@@ -465,24 +456,28 @@ async function $verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
|
465
456
|
keyLength * 8
|
|
466
457
|
)
|
|
467
458
|
);
|
|
459
|
+
if (bits === void 0 || hashedPasswordBytes.result === void 0) return false;
|
|
468
460
|
if (bits.length !== hashedPasswordBytes.result.length) return false;
|
|
469
|
-
let
|
|
461
|
+
let diff = 0;
|
|
470
462
|
for (let i = 0; i < bits.length; i++) {
|
|
471
|
-
|
|
463
|
+
diff |= bits[i] ^ hashedPasswordBytes.result[i];
|
|
472
464
|
}
|
|
473
|
-
return
|
|
465
|
+
return diff === 0;
|
|
474
466
|
} catch {
|
|
475
467
|
return false;
|
|
476
468
|
}
|
|
477
469
|
}
|
|
478
470
|
|
|
479
471
|
// src/web/kit.ts
|
|
472
|
+
function isWebSecretKey(x) {
|
|
473
|
+
return chunkUIV6DG54_cjs.$isSecretKey(x, "web") !== null;
|
|
474
|
+
}
|
|
480
475
|
function tryGenerateUuid() {
|
|
481
476
|
return $generateUuid();
|
|
482
477
|
}
|
|
483
478
|
function generateUuid() {
|
|
484
479
|
const { result, error } = $generateUuid();
|
|
485
|
-
if (error) throw new Error(
|
|
480
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
486
481
|
return result;
|
|
487
482
|
}
|
|
488
483
|
async function tryCreateSecretKey(secret, options = {}) {
|
|
@@ -490,7 +485,7 @@ async function tryCreateSecretKey(secret, options = {}) {
|
|
|
490
485
|
}
|
|
491
486
|
async function createSecretKey(secret, options = {}) {
|
|
492
487
|
const { result, error } = await $createSecretKey(secret, options);
|
|
493
|
-
if (error) throw new Error(
|
|
488
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
494
489
|
return result;
|
|
495
490
|
}
|
|
496
491
|
async function tryEncrypt(data, secretKey, options = {}) {
|
|
@@ -498,7 +493,7 @@ async function tryEncrypt(data, secretKey, options = {}) {
|
|
|
498
493
|
}
|
|
499
494
|
async function encrypt(data, secretKey, options = {}) {
|
|
500
495
|
const { result, error } = await $encrypt(data, secretKey, options);
|
|
501
|
-
if (error) throw new Error(
|
|
496
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
502
497
|
return result;
|
|
503
498
|
}
|
|
504
499
|
async function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
@@ -506,7 +501,7 @@ async function tryDecrypt(encrypted, secretKey, options = {}) {
|
|
|
506
501
|
}
|
|
507
502
|
async function decrypt(encrypted, secretKey, options = {}) {
|
|
508
503
|
const { result, error } = await $decrypt(encrypted, secretKey, options);
|
|
509
|
-
if (error) throw new Error(
|
|
504
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
510
505
|
return result;
|
|
511
506
|
}
|
|
512
507
|
async function tryEncryptObj(data, secretKey, options = {}) {
|
|
@@ -514,7 +509,7 @@ async function tryEncryptObj(data, secretKey, options = {}) {
|
|
|
514
509
|
}
|
|
515
510
|
async function encryptObj(data, secretKey, options = {}) {
|
|
516
511
|
const { result, error } = await $encryptObj(data, secretKey, options);
|
|
517
|
-
if (error) throw new Error(
|
|
512
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
518
513
|
return result;
|
|
519
514
|
}
|
|
520
515
|
async function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
@@ -522,7 +517,7 @@ async function tryDecryptObj(encrypted, secretKey, options = {}) {
|
|
|
522
517
|
}
|
|
523
518
|
async function decryptObj(encrypted, secretKey, options = {}) {
|
|
524
519
|
const { result, error } = await $decryptObj(encrypted, secretKey, options);
|
|
525
|
-
if (error) throw new Error(
|
|
520
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
526
521
|
return result;
|
|
527
522
|
}
|
|
528
523
|
async function tryHash(data, options = {}) {
|
|
@@ -530,15 +525,7 @@ async function tryHash(data, options = {}) {
|
|
|
530
525
|
}
|
|
531
526
|
async function hash(data, options = {}) {
|
|
532
527
|
const { result, error } = await $hash(data, options);
|
|
533
|
-
if (error) throw new Error(
|
|
534
|
-
return result;
|
|
535
|
-
}
|
|
536
|
-
async function tryHashObj(data, options = {}) {
|
|
537
|
-
return await $hashObj(data, options);
|
|
538
|
-
}
|
|
539
|
-
async function hashObj(data, options = {}) {
|
|
540
|
-
const { result, error } = await $hashObj(data, options);
|
|
541
|
-
if (error) throw new Error(chunkZJ32WGAA_cjs.$fmtResultErr(error));
|
|
528
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
542
529
|
return result;
|
|
543
530
|
}
|
|
544
531
|
async function tryHashPassword(password, options = {}) {
|
|
@@ -546,7 +533,7 @@ async function tryHashPassword(password, options = {}) {
|
|
|
546
533
|
}
|
|
547
534
|
async function hashPassword(password, options = {}) {
|
|
548
535
|
const { hash: hash2, salt, error } = await $hashPassword(password, options);
|
|
549
|
-
if (error) throw new Error(
|
|
536
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
550
537
|
return { hash: hash2, salt };
|
|
551
538
|
}
|
|
552
539
|
async function verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
@@ -557,7 +544,7 @@ function tryConvertStrToBytes(data, inputEncoding = "utf8") {
|
|
|
557
544
|
}
|
|
558
545
|
function convertStrToBytes(data, inputEncoding = "utf8") {
|
|
559
546
|
const { result, error } = $convertStrToBytes(data, inputEncoding);
|
|
560
|
-
if (error) throw new Error(
|
|
547
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
561
548
|
return result;
|
|
562
549
|
}
|
|
563
550
|
function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
@@ -565,7 +552,7 @@ function tryConvertBytesToStr(data, outputEncoding = "utf8") {
|
|
|
565
552
|
}
|
|
566
553
|
function convertBytesToStr(data, outputEncoding = "utf8") {
|
|
567
554
|
const { result, error } = $convertBytesToStr(data, outputEncoding);
|
|
568
|
-
if (error) throw new Error(
|
|
555
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
569
556
|
return result;
|
|
570
557
|
}
|
|
571
558
|
function tryConvertEncoding(data, from, to) {
|
|
@@ -573,7 +560,7 @@ function tryConvertEncoding(data, from, to) {
|
|
|
573
560
|
}
|
|
574
561
|
function convertEncoding(data, from, to) {
|
|
575
562
|
const { result, error } = $convertEncoding(data, from, to);
|
|
576
|
-
if (error) throw new Error(
|
|
563
|
+
if (error) throw new Error(chunkUIV6DG54_cjs.$fmtResultErr(error));
|
|
577
564
|
return result;
|
|
578
565
|
}
|
|
579
566
|
|
|
@@ -587,8 +574,8 @@ exports.encrypt = encrypt;
|
|
|
587
574
|
exports.encryptObj = encryptObj;
|
|
588
575
|
exports.generateUuid = generateUuid;
|
|
589
576
|
exports.hash = hash;
|
|
590
|
-
exports.hashObj = hashObj;
|
|
591
577
|
exports.hashPassword = hashPassword;
|
|
578
|
+
exports.isWebSecretKey = isWebSecretKey;
|
|
592
579
|
exports.kit_exports = kit_exports;
|
|
593
580
|
exports.tryConvertBytesToStr = tryConvertBytesToStr;
|
|
594
581
|
exports.tryConvertEncoding = tryConvertEncoding;
|
|
@@ -600,8 +587,7 @@ exports.tryEncrypt = tryEncrypt;
|
|
|
600
587
|
exports.tryEncryptObj = tryEncryptObj;
|
|
601
588
|
exports.tryGenerateUuid = tryGenerateUuid;
|
|
602
589
|
exports.tryHash = tryHash;
|
|
603
|
-
exports.tryHashObj = tryHashObj;
|
|
604
590
|
exports.tryHashPassword = tryHashPassword;
|
|
605
591
|
exports.verifyPassword = verifyPassword;
|
|
606
|
-
//# sourceMappingURL=chunk-
|
|
607
|
-
//# sourceMappingURL=chunk-
|
|
592
|
+
//# sourceMappingURL=chunk-XIWV7XVI.cjs.map
|
|
593
|
+
//# sourceMappingURL=chunk-XIWV7XVI.cjs.map
|