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