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.
Files changed (45) hide show
  1. package/dist/{chunk-SUGN4VDZ.js → chunk-LHB5NXYW.js} +85 -77
  2. package/dist/chunk-LHB5NXYW.js.map +1 -0
  3. package/dist/{chunk-AEQNI5EZ.js → chunk-N5T4PNG7.js} +75 -72
  4. package/dist/chunk-N5T4PNG7.js.map +1 -0
  5. package/dist/{chunk-3XGARINH.cjs → chunk-OL2AIXWK.cjs} +156 -152
  6. package/dist/chunk-OL2AIXWK.cjs.map +1 -0
  7. package/dist/{chunk-RXXVBCWL.cjs → chunk-UIV6DG54.cjs} +37 -38
  8. package/dist/chunk-UIV6DG54.cjs.map +1 -0
  9. package/dist/{chunk-WIZT7AYM.cjs → chunk-XIWV7XVI.cjs} +171 -162
  10. package/dist/chunk-XIWV7XVI.cjs.map +1 -0
  11. package/dist/{chunk-LOJKJJX5.js → chunk-YMNOTRET.js} +34 -37
  12. package/dist/chunk-YMNOTRET.js.map +1 -0
  13. package/dist/export-D1Vh79Qw.d.ts +417 -0
  14. package/dist/export-DGrELdz_.d.ts +416 -0
  15. package/dist/export-DXRl-ncG.d.cts +417 -0
  16. package/dist/export-Gd8hafl6.d.cts +416 -0
  17. package/dist/index.cjs +14 -22
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.cts +52 -4
  20. package/dist/index.d.ts +52 -4
  21. package/dist/index.js +9 -3
  22. package/dist/index.js.map +1 -1
  23. package/dist/node.cjs +35 -35
  24. package/dist/node.d.cts +2 -2
  25. package/dist/node.d.ts +2 -2
  26. package/dist/node.js +2 -2
  27. package/dist/validate-Cb7IOrPo.d.cts +373 -0
  28. package/dist/validate-Cb7IOrPo.d.ts +373 -0
  29. package/dist/web-api.cjs +35 -35
  30. package/dist/web-api.d.cts +2 -2
  31. package/dist/web-api.d.ts +2 -2
  32. package/dist/web-api.js +2 -2
  33. package/package.json +1 -1
  34. package/dist/chunk-3XGARINH.cjs.map +0 -1
  35. package/dist/chunk-AEQNI5EZ.js.map +0 -1
  36. package/dist/chunk-LOJKJJX5.js.map +0 -1
  37. package/dist/chunk-RXXVBCWL.cjs.map +0 -1
  38. package/dist/chunk-SUGN4VDZ.js.map +0 -1
  39. package/dist/chunk-WIZT7AYM.cjs.map +0 -1
  40. package/dist/export-Bq9tslUU.d.ts +0 -252
  41. package/dist/export-C0WDJZUy.d.ts +0 -251
  42. package/dist/export-LPOfeH2z.d.cts +0 -251
  43. package/dist/export-RD2Af4CJ.d.cts +0 -252
  44. package/dist/validate-CS4PFmY1.d.cts +0 -159
  45. package/dist/validate-CS4PFmY1.d.ts +0 -159
@@ -1,10 +1,10 @@
1
1
  'use strict';
2
2
 
3
- var chunkRXXVBCWL_cjs = require('./chunk-RXXVBCWL.cjs');
3
+ var chunkUIV6DG54_cjs = require('./chunk-UIV6DG54.cjs');
4
4
 
5
5
  // src/web/kit.ts
6
6
  var kit_exports = {};
7
- chunkRXXVBCWL_cjs.__export(kit_exports, {
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 (!chunkRXXVBCWL_cjs.$isStr(data)) {
38
- return chunkRXXVBCWL_cjs.$err({
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 (!chunkRXXVBCWL_cjs.ENCODINGS.includes(inputEncoding)) {
44
- return chunkRXXVBCWL_cjs.$err({
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 chunkRXXVBCWL_cjs.$ok({ result: bytes });
52
+ return chunkUIV6DG54_cjs.$ok({ result: bytes });
52
53
  } catch (error) {
53
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - String to Bytes: Failed to convert data", desc: chunkRXXVBCWL_cjs.$fmtError(error) });
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 chunkRXXVBCWL_cjs.$err({
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 (!chunkRXXVBCWL_cjs.ENCODINGS.includes(outputEncoding)) {
64
- return chunkRXXVBCWL_cjs.$err({
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 chunkRXXVBCWL_cjs.$ok(str);
73
+ return chunkUIV6DG54_cjs.$ok(str);
73
74
  } catch (error) {
74
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Bytes to String: Failed to convert data", desc: chunkRXXVBCWL_cjs.$fmtError(error) });
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 (!chunkRXXVBCWL_cjs.$isStr(data)) {
79
- return chunkRXXVBCWL_cjs.$err({
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 (!chunkRXXVBCWL_cjs.ENCODINGS.includes(from) || !chunkRXXVBCWL_cjs.ENCODINGS.includes(to)) {
85
- return chunkRXXVBCWL_cjs.$err({
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 chunkRXXVBCWL_cjs.$err({ msg: bytes.error.message, desc: bytes.error.description });
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 chunkRXXVBCWL_cjs.$err({ msg: str.error.message, desc: str.error.description });
94
- return chunkRXXVBCWL_cjs.$ok({ result: str.result });
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 chunkRXXVBCWL_cjs.$ok(crypto.randomUUID());
161
+ return chunkUIV6DG54_cjs.$ok(crypto.randomUUID());
161
162
  } catch (error) {
162
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - UUID Generation: Failed to generate UUID", desc: chunkRXXVBCWL_cjs.$fmtError(error) });
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 (!chunkRXXVBCWL_cjs.$isStr(secret)) {
167
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Key Generation: Empty Secret", desc: "Secret must be a non-empty string" });
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 chunkRXXVBCWL_cjs.ENCRYPTION_ALGORITHMS)) {
171
- return chunkRXXVBCWL_cjs.$err({
172
- msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,
173
- desc: `Supported algorithms are: ${Object.keys(chunkRXXVBCWL_cjs.ENCRYPTION_ALGORITHMS).join(", ")}`
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 chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS)) {
178
- return chunkRXXVBCWL_cjs.$err({
179
- msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,
180
- desc: `Supported digests are: ${Object.keys(chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS).join(", ")}`
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 (!chunkRXXVBCWL_cjs.$isStr(salt, 8)) {
185
- return chunkRXXVBCWL_cjs.$err({
186
- msg: "Crypto NodeJS API - Key Generation: Weak salt",
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 (!chunkRXXVBCWL_cjs.$isStr(info)) {
192
- return chunkRXXVBCWL_cjs.$err({
193
- msg: "Crypto NodeJS API - Key Generation: Invalid info",
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 = chunkRXXVBCWL_cjs.ENCRYPTION_ALGORITHMS[algorithm];
198
- const digestAlgo = chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS[digest];
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
- algo: encryptAlgo,
219
+ algorithm,
219
220
  key
220
221
  });
221
- return chunkRXXVBCWL_cjs.$ok({ result: secretKey });
222
+ return chunkUIV6DG54_cjs.$ok({ result: secretKey });
222
223
  } catch (error) {
223
- return chunkRXXVBCWL_cjs.$err({
224
- msg: "Crypto Web API - Key Generation: Failed to create secret key",
225
- desc: chunkRXXVBCWL_cjs.$fmtError(error)
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 (!chunkRXXVBCWL_cjs.$isStr(data)) {
231
- return chunkRXXVBCWL_cjs.$err({
232
- msg: "Crypto Web API - Encryption: Empty data for encryption",
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 inputEncoding = options.inputEncoding ?? "utf8";
237
- const outputEncoding = options.outputEncoding ?? "base64url";
238
- if (!chunkRXXVBCWL_cjs.ENCODINGS.includes(inputEncoding) || !chunkRXXVBCWL_cjs.ENCODINGS.includes(outputEncoding)) {
239
- return chunkRXXVBCWL_cjs.$err({
240
- msg: `Crypto Web API - Encryption: Unsupported encoding: input ${inputEncoding} or output ${outputEncoding}`,
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
- if (!chunkRXXVBCWL_cjs.isSecretKey(secretKey, "web")) {
245
- return chunkRXXVBCWL_cjs.$err({
246
- msg: "Crypto Web API - Encryption: Invalid Secret Key",
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, inputEncoding);
251
- if (error) return chunkRXXVBCWL_cjs.$err(error);
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(secretKey.algo.ivLength));
254
- const cipherWithTag = await crypto.subtle.encrypt({ name: secretKey.algo.web, iv }, secretKey.key, result);
255
- const ivStr = $convertBytesToStr(iv, outputEncoding);
256
- const cipherStr = $convertBytesToStr(cipherWithTag, outputEncoding);
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 chunkRXXVBCWL_cjs.$err({
259
- msg: "Crypto Web API - Encryption: Failed to convert IV or encrypted data",
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 chunkRXXVBCWL_cjs.$ok(`${ivStr.result}.${cipherStr.result}.`);
268
+ return chunkUIV6DG54_cjs.$ok(`${ivStr.result}.${cipherStr.result}.`);
264
269
  } catch (error2) {
265
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Encryption: Failed to encrypt data", desc: chunkRXXVBCWL_cjs.$fmtError(error2) });
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 (chunkRXXVBCWL_cjs.matchPattern(encrypted, "web") === false) {
270
- return chunkRXXVBCWL_cjs.$err({
271
- msg: "Crypto Web API - Decryption: Invalid encrypted data format",
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 inputEncoding = options.inputEncoding ?? "base64url";
276
- const outputEncoding = options.outputEncoding ?? "utf8";
277
- if (!chunkRXXVBCWL_cjs.ENCODINGS.includes(inputEncoding) || !chunkRXXVBCWL_cjs.ENCODINGS.includes(outputEncoding)) {
278
- return chunkRXXVBCWL_cjs.$err({
279
- msg: `Crypto Web API - Decryption: Unsupported encoding: input ${inputEncoding} or output ${outputEncoding}`,
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 (!chunkRXXVBCWL_cjs.$isStr(iv) || !chunkRXXVBCWL_cjs.$isStr(encryptedWithTag)) {
285
- return chunkRXXVBCWL_cjs.$err({
286
- msg: "Crypto Web API - Decryption: Invalid encrypted data",
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
- if (!chunkRXXVBCWL_cjs.isSecretKey(secretKey, "web")) {
291
- return chunkRXXVBCWL_cjs.$err({
292
- msg: "Crypto Web API - Decryption: Invalid Secret Key",
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, inputEncoding);
297
- const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, inputEncoding);
301
+ const ivBytes = $convertStrToBytes(iv, encoding);
302
+ const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, encoding);
298
303
  if (ivBytes.error || cipherWithTagBytes.error) {
299
- return chunkRXXVBCWL_cjs.$err({
300
- msg: "Crypto Web API - Decryption: Failed to convert IV or encrypted data",
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: secretKey.algo.web, iv: ivBytes.result },
307
- secretKey.key,
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 chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Decryption: Failed to decrypt data", desc: chunkRXXVBCWL_cjs.$fmtError(error) });
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 } = chunkRXXVBCWL_cjs.$stringifyObj(data);
317
- if (error) return chunkRXXVBCWL_cjs.$err(error);
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 chunkRXXVBCWL_cjs.$err(error);
323
- return chunkRXXVBCWL_cjs.$parseToObj(result);
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 (!chunkRXXVBCWL_cjs.$isStr(data)) {
327
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Hashing: Empty data for hashing", desc: "Data must be a non-empty string" });
328
- }
329
- const inputEncoding = options.inputEncoding ?? "utf8";
330
- const outputEncoding = options.outputEncoding ?? "base64url";
331
- if (!chunkRXXVBCWL_cjs.ENCODINGS.includes(inputEncoding) || !chunkRXXVBCWL_cjs.ENCODINGS.includes(outputEncoding)) {
332
- return chunkRXXVBCWL_cjs.$err({
333
- msg: `Crypto Web API - Hashing: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,
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 chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS)) {
339
- return chunkRXXVBCWL_cjs.$err({
340
- msg: `Crypto Web API - Hashing: Unsupported digest: ${digest}`,
341
- desc: `Supported digests are: ${Object.keys(chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS).join(", ")}`
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 = chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS[digest];
345
- const { result, error } = $convertStrToBytes(data, inputEncoding);
346
- if (error) return chunkRXXVBCWL_cjs.$err(error);
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, outputEncoding);
353
+ return $convertBytesToStr(hashed, encoding);
350
354
  } catch (error2) {
351
- return chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Hashing: Failed to hash data", desc: chunkRXXVBCWL_cjs.$fmtError(error2) });
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 (!chunkRXXVBCWL_cjs.$isStr(password)) {
356
- return chunkRXXVBCWL_cjs.$err({
357
- msg: "Crypto Web API - Password Hashing: Empty password",
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 chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS)) {
363
- return chunkRXXVBCWL_cjs.$err({
364
- msg: `Crypto Web API - Password Hashing: Unsupported digest: ${digest}`,
365
- desc: `Supported digests are: ${Object.keys(chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS).join(", ")}`
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 = chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS[digest];
369
- const outputEncoding = options.outputEncoding ?? "base64url";
370
- if (!chunkRXXVBCWL_cjs.ENCODINGS.includes(outputEncoding)) {
371
- return chunkRXXVBCWL_cjs.$err({
372
- msg: `Crypto Web API - Password Hashing: Unsupported output encoding: ${outputEncoding}`,
373
- desc: "Use base64, base64url, hex, utf8, or latin1"
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 chunkRXXVBCWL_cjs.$err({
379
- msg: "Crypto Web API - Password Hashing: Weak salt length",
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 chunkRXXVBCWL_cjs.$err({
386
- msg: "Crypto Web API - Password Hashing: Weak iteration count",
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 chunkRXXVBCWL_cjs.$err({
393
- msg: "Crypto Web API - Password Hashing: Weak key length",
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, outputEncoding);
412
- if (saltStr.error) return chunkRXXVBCWL_cjs.$err(saltStr.error);
413
- const hashedPasswordStr = $convertBytesToStr(bits, outputEncoding);
414
- if (hashedPasswordStr.error) return chunkRXXVBCWL_cjs.$err(hashedPasswordStr.error);
415
- return chunkRXXVBCWL_cjs.$ok({ hash: hashedPasswordStr.result, salt: saltStr.result });
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 chunkRXXVBCWL_cjs.$err({ msg: "Crypto Web API - Password Hashing: Failed to hash password", desc: chunkRXXVBCWL_cjs.$fmtError(error) });
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 (!chunkRXXVBCWL_cjs.$isStr(password) || !chunkRXXVBCWL_cjs.$isStr(hashedPassword) || !chunkRXXVBCWL_cjs.$isStr(salt)) return false;
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 chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS)) return false;
424
- const digestAlgo = chunkRXXVBCWL_cjs.DIGEST_ALGORITHMS[digest];
425
- const inputEncoding = options.inputEncoding ?? "base64url";
426
- if (!chunkRXXVBCWL_cjs.ENCODINGS.includes(inputEncoding)) return false;
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, "base64url");
435
+ const saltBytes = $convertStrToBytes(salt, encoding);
432
436
  if (saltBytes.error) return false;
433
- const hashedPasswordBytes = $convertStrToBytes(hashedPassword, "base64url");
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 isMatch = true;
461
+ let diff = 0;
457
462
  for (let i = 0; i < bits.length; i++) {
458
- if (bits[i] !== hashedPasswordBytes.result[i]) isMatch = false;
463
+ diff |= bits[i] ^ hashedPasswordBytes.result[i];
459
464
  }
460
- return isMatch;
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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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(chunkRXXVBCWL_cjs.$fmtResultErr(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-WIZT7AYM.cjs.map
584
- //# sourceMappingURL=chunk-WIZT7AYM.cjs.map
592
+ //# sourceMappingURL=chunk-XIWV7XVI.cjs.map
593
+ //# sourceMappingURL=chunk-XIWV7XVI.cjs.map