cipher-kit 2.0.0-beta.2 → 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/{chunk-AEQNI5EZ.js → chunk-6GBH7YTP.js} +27 -10
  2. package/dist/chunk-6GBH7YTP.js.map +1 -0
  3. package/dist/{chunk-SUGN4VDZ.js → chunk-BNQERV4S.js} +33 -12
  4. package/dist/chunk-BNQERV4S.js.map +1 -0
  5. package/dist/{chunk-WIZT7AYM.cjs → chunk-NKLNWTQA.cjs} +134 -111
  6. package/dist/chunk-NKLNWTQA.cjs.map +1 -0
  7. package/dist/{chunk-3XGARINH.cjs → chunk-UHP3PPXP.cjs} +125 -106
  8. package/dist/chunk-UHP3PPXP.cjs.map +1 -0
  9. package/dist/{chunk-LOJKJJX5.js → chunk-YPYDYYV2.js} +27 -32
  10. package/dist/chunk-YPYDYYV2.js.map +1 -0
  11. package/dist/{chunk-RXXVBCWL.cjs → chunk-ZJ32WGAA.cjs} +27 -31
  12. package/dist/chunk-ZJ32WGAA.cjs.map +1 -0
  13. package/dist/{export-Bq9tslUU.d.ts → export--ndIQ3j3.d.cts} +22 -3
  14. package/dist/{export-C0WDJZUy.d.ts → export-C2M5UPLX.d.cts} +22 -3
  15. package/dist/{export-RD2Af4CJ.d.cts → export-CPUbAFZA.d.ts} +22 -3
  16. package/dist/{export-LPOfeH2z.d.cts → export-v9ULdDL0.d.ts} +22 -3
  17. package/dist/index.cjs +12 -12
  18. package/dist/index.d.cts +3 -3
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +3 -3
  21. package/dist/node.cjs +40 -32
  22. package/dist/node.d.cts +2 -2
  23. package/dist/node.d.ts +2 -2
  24. package/dist/node.js +2 -2
  25. package/dist/{validate-CS4PFmY1.d.cts → validate-CULVlPck.d.cts} +9 -11
  26. package/dist/{validate-CS4PFmY1.d.ts → validate-CULVlPck.d.ts} +9 -11
  27. package/dist/web-api.cjs +40 -32
  28. package/dist/web-api.d.cts +2 -2
  29. package/dist/web-api.d.ts +2 -2
  30. package/dist/web-api.js +2 -2
  31. package/package.json +1 -1
  32. package/dist/chunk-3XGARINH.cjs.map +0 -1
  33. package/dist/chunk-AEQNI5EZ.js.map +0 -1
  34. package/dist/chunk-LOJKJJX5.js.map +0 -1
  35. package/dist/chunk-RXXVBCWL.cjs.map +0 -1
  36. package/dist/chunk-SUGN4VDZ.js.map +0 -1
  37. package/dist/chunk-WIZT7AYM.cjs.map +0 -1
@@ -1,4 +1,4 @@
1
- import { __export, $fmtResultErr, $ok, $err, $fmtError, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, ENCODINGS, isSecretKey, matchPattern, $stringifyObj, $parseToObj } from './chunk-LOJKJJX5.js';
1
+ import { __export, $fmtResultErr, $ok, $err, $fmtError, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, ENCODINGS, $isSecretKey, matchPattern, $stringifyObj, $parseToObj } from './chunk-YPYDYYV2.js';
2
2
  import { Buffer } from 'buffer';
3
3
  import nodeCrypto from 'crypto';
4
4
 
@@ -15,6 +15,7 @@ __export(kit_exports, {
15
15
  encryptObj: () => encryptObj,
16
16
  generateUuid: () => generateUuid,
17
17
  hash: () => hash,
18
+ hashObj: () => hashObj,
18
19
  hashPassword: () => hashPassword,
19
20
  tryConvertBytesToStr: () => tryConvertBytesToStr,
20
21
  tryConvertEncoding: () => tryConvertEncoding,
@@ -26,6 +27,7 @@ __export(kit_exports, {
26
27
  tryEncryptObj: () => tryEncryptObj,
27
28
  tryGenerateUuid: () => tryGenerateUuid,
28
29
  tryHash: () => tryHash,
30
+ tryHashObj: () => tryHashObj,
29
31
  tryHashPassword: () => tryHashPassword,
30
32
  verifyPassword: () => verifyPassword
31
33
  });
@@ -139,7 +141,7 @@ function $createSecretKey(secret, options = {}) {
139
141
  const secretKey = Object.freeze({
140
142
  platform: "node",
141
143
  digest,
142
- algo: encryptAlgo,
144
+ algorithm,
143
145
  key
144
146
  });
145
147
  return $ok({ result: secretKey });
@@ -162,7 +164,8 @@ function $encrypt(data, secretKey, options = {}) {
162
164
  desc: "Use base64, base64url, hex, utf8, or latin1"
163
165
  });
164
166
  }
165
- if (!isSecretKey(secretKey, "node")) {
167
+ const injectedKey = $isSecretKey(secretKey, "node");
168
+ if (!injectedKey) {
166
169
  return $err({
167
170
  msg: "Crypto NodeJS API - Encryption: Invalid Secret Key",
168
171
  desc: "Expected a Node SecretKey"
@@ -171,8 +174,8 @@ function $encrypt(data, secretKey, options = {}) {
171
174
  const { result, error } = $convertStrToBytes(data, inputEncoding);
172
175
  if (error) return $err(error);
173
176
  try {
174
- const iv = nodeCrypto.randomBytes(secretKey.algo.ivLength);
175
- const cipher = nodeCrypto.createCipheriv(secretKey.algo.node, secretKey.key, iv);
177
+ const iv = nodeCrypto.randomBytes(injectedKey.injected.ivLength);
178
+ const cipher = nodeCrypto.createCipheriv(injectedKey.injected.node, injectedKey.key, iv);
176
179
  const encrypted = Buffer.concat([cipher.update(result), cipher.final()]);
177
180
  const tag = cipher.getAuthTag();
178
181
  const ivStr = $convertBytesToStr(iv, outputEncoding);
@@ -211,7 +214,8 @@ function $decrypt(encrypted, secretKey, options = {}) {
211
214
  desc: "Encrypted data must contain valid IV, encrypted data, and tag components"
212
215
  });
213
216
  }
214
- if (!isSecretKey(secretKey, "node")) {
217
+ const injectedKey = $isSecretKey(secretKey, "node");
218
+ if (!injectedKey) {
215
219
  return $err({
216
220
  msg: "Crypto NodeJS API - Decryption: Invalid Secret Key",
217
221
  desc: "Expected a Node SecretKey"
@@ -227,7 +231,7 @@ function $decrypt(encrypted, secretKey, options = {}) {
227
231
  });
228
232
  }
229
233
  try {
230
- const decipher = nodeCrypto.createDecipheriv(secretKey.algo.node, secretKey.key, ivBytes.result);
234
+ const decipher = nodeCrypto.createDecipheriv(injectedKey.injected.node, injectedKey.key, ivBytes.result);
231
235
  decipher.setAuthTag(tagBytes.result);
232
236
  const decrypted = Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);
233
237
  return $convertBytesToStr(decrypted, outputEncoding);
@@ -277,6 +281,11 @@ function $hash(data, options = {}) {
277
281
  return $err({ msg: "Crypto NodeJS API - Hashing: Failed to hash data with Crypto NodeJS", desc: $fmtError(error2) });
278
282
  }
279
283
  }
284
+ function $hashObj(data, options = {}) {
285
+ const { result, error } = $stringifyObj(data);
286
+ if (error) return $err(error);
287
+ return $hash(result, options);
288
+ }
280
289
  function $hashPassword(password, options = {}) {
281
290
  if (!$isStr(password)) {
282
291
  return $err({
@@ -410,6 +419,14 @@ function hash(data, options = {}) {
410
419
  if (error) throw new Error($fmtResultErr(error));
411
420
  return result;
412
421
  }
422
+ function tryHashObj(data, options = {}) {
423
+ return $hashObj(data, options);
424
+ }
425
+ function hashObj(data, options = {}) {
426
+ const { result, error } = $hashObj(data, options);
427
+ if (error) throw new Error($fmtResultErr(error));
428
+ return result;
429
+ }
413
430
  function tryHashPassword(password, options = {}) {
414
431
  return $hashPassword(password, options);
415
432
  }
@@ -446,6 +463,6 @@ function convertEncoding(data, from, to) {
446
463
  return result;
447
464
  }
448
465
 
449
- export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, kit_exports, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword };
450
- //# sourceMappingURL=chunk-AEQNI5EZ.js.map
451
- //# sourceMappingURL=chunk-AEQNI5EZ.js.map
466
+ export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashObj, hashPassword, kit_exports, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashObj, tryHashPassword, verifyPassword };
467
+ //# sourceMappingURL=chunk-6GBH7YTP.js.map
468
+ //# sourceMappingURL=chunk-6GBH7YTP.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/node/kit.ts","../src/node/node-encode.ts","../src/node/node-encrypt.ts"],"names":["Buffer","error","hash"],"mappings":";;;;;AAAA,IAAA,WAAA,GAAA;AAAA,QAAA,CAAA,WAAA,EAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,IAAA,EAAA,MAAA,IAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,cAAA,EAAA,MAAA;AAAA,CAAA,CAAA;ACMO,SAAS,kBAAA,CAAmB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7YTP.js","sourcesContent":["import type { Buffer } from 'node:buffer';\r\nimport { $fmtResultErr, type Result } from '~/helpers/error';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n Encoding,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $convertBytesToStr, $convertEncoding, $convertStrToBytes } from './node-encode';\r\nimport {\r\n $createSecretKey,\r\n $decrypt,\r\n $decryptObj,\r\n $encrypt,\r\n $encryptObj,\r\n $generateUuid,\r\n $hash,\r\n $hashObj,\r\n $hashPassword,\r\n $verifyPassword,\r\n} from './node-encrypt';\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A Result containing a string representing the generated UUID or an error.\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A string representing the generated UUID.\r\n * @throws {Error} If UUID generation fails.\r\n */\r\nexport function generateUuid(): string {\r\n const { result, error } = $generateUuid();\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A Result containing a SecretKey object representing the derived secret key or an error.\r\n */\r\nexport function tryCreateSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Result<{ result: SecretKey<'node'> }> {\r\n return $createSecretKey(secret, options);\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A SecretKey object representing the derived secret key.\r\n * @throws {Error} If the input key is invalid or key generation fails.\r\n */\r\nexport function createSecretKey(secret: string, options: CreateSecretKeyOptions = {}): SecretKey<'node'> {\r\n const { result, error } = $createSecretKey(secret, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted data in the specified format or an error.\r\n */\r\nexport function tryEncrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): Result<string> {\r\n return $encrypt(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted data in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport function encrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): string {\r\n const { result, error } = $encrypt(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing a string representing the decrypted data or an error.\r\n */\r\nexport function tryDecrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<string> {\r\n return $decrypt(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A string representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport function decrypt(encrypted: string, secretKey: SecretKey<'node'>, options: DecryptOptions = {}): string {\r\n const { result, error } = $decrypt(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted object in the specified format or an error.\r\n */\r\nexport function tryEncryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): Result<string> {\r\n return $encryptObj(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted object in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport function encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): string {\r\n const { result, error } = $encryptObj(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing an object representing the decrypted data or an error.\r\n */\r\nexport function tryDecryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<{ result: T }> {\r\n return $decryptObj<T>(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns An object representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport function decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): T {\r\n const { result, error } = $decryptObj<T>(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash in base64url format or an error.\r\n */\r\nexport function tryHash(data: string, options: HashOptions = {}): Result<string> {\r\n return $hash(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A string representing the SHA-256 hash in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport function hash(data: string, options: HashOptions = {}): string {\r\n const { result, error } = $hash(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input object by first serializing it to a JSON string using stable key ordering,\r\n * then hashing the string using SHA-256 and returning the hash in base64url format.\r\n *\r\n * @param data - The input object to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash of the serialized object in base64url format or an error.\r\n * */\r\nexport function tryHashObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n options: HashOptions = {},\r\n): Result<string> {\r\n return $hashObj(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input object by first serializing it to a JSON string using stable key ordering,\r\n * then hashing the string using SHA-256 and returning the hash in base64url format.\r\n *\r\n * @param data - The input object to hash.\r\n * @returns A string representing the SHA-256 hash of the serialized object in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport function hashObj<T extends object = Record<string, unknown>>(data: T, options: HashOptions = {}): string {\r\n const { result, error } = $hashObj(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns A Result containing an object with the hash and salt, or an error.\r\n */\r\nexport function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ hash: string; salt: string }> {\r\n return $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns An object with the hash and salt.\r\n * @throws {Error} If the input password is invalid or hashing fails.\r\n */\r\nexport function hashPassword(password: string, options: HashPasswordOptions = {}): { hash: string; salt: string } {\r\n const { hash, salt, error } = $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { hash, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a hashed password and salt.\r\n *\r\n * @param password - The password to verify.\r\n * @param hashedPassword - The hashed password to compare against (in base64url format).\r\n * @param salt - The salt used during hashing (in base64url format).\r\n * @returns A boolean indicating whether the password matches the hashed password.\r\n */\r\nexport function verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): boolean {\r\n return $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing a Node.js Buffer with the encoded data or an error.\r\n */\r\nexport function tryConvertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Result<{ result: Buffer }> {\r\n return $convertStrToBytes(data, inputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Node.js Buffer containing the encoded data.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Buffer {\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Converts a Buffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Buffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing the string representation of the Buffer or an error.\r\n */\r\nexport function tryConvertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n return $convertBytesToStr(data, outputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a Buffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Buffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A string representation of the Buffer in the specified format.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, outputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A Result containing the converted string or an error.\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A converted string.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertEncoding(data: string, from: Encoding, to: Encoding): string {\r\n const { result, error } = $convertEncoding(data, from, to);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport { ENCODINGS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { Encoding } from '~/helpers/types';\r\nimport { $isStr } from '~/helpers/validate';\r\n\r\nexport function $convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Result<{ result: Buffer }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - String to Bytes: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(inputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - String to Bytes: Unsupported encoding: ${inputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n return $ok({ result: Buffer.from(data, inputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - String to Bytes: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n if (!(data instanceof Buffer)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Bytes to String: Invalid data type',\r\n desc: 'Data must be a Buffer',\r\n });\r\n }\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Bytes to String: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n return $ok(Buffer.from(data).toString(outputEncoding));\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Bytes to String: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Convert Format: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n if (!ENCODINGS.includes(from) || !ENCODINGS.includes(to)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Convert Format: Unsupported encoding: from ${from} to ${to}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const bytes = $convertStrToBytes(data, from);\r\n if (bytes.error) return $err({ msg: bytes.error.message, desc: bytes.error.description });\r\n\r\n const str = $convertBytesToStr(bytes.result, to);\r\n if (str.error) return $err({ msg: str.error.message, desc: str.error.description });\r\n\r\n return $ok({ result: str.result });\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport nodeCrypto from 'node:crypto';\r\nimport { DIGEST_ALGORITHMS, ENCODINGS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $isSecretKey, $isStr, matchPattern } from '~/helpers/validate';\r\nimport { $convertBytesToStr, $convertStrToBytes } from './node-encode';\r\n\r\nexport function $generateUuid(): Result<string> {\r\n try {\r\n return $ok(nodeCrypto.randomUUID());\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - UUID Generation: Failed to generate UUID', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $createSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Result<{ result: SecretKey<'node'> }> {\r\n if (!$isStr(secret)) {\r\n return $err({ msg: 'Crypto NodeJS API - Key Generation: Empty Secret', desc: 'Secret must be a non-empty string' });\r\n }\r\n\r\n const algorithm = options.algorithm ?? 'aes256gcm';\r\n if (!(algorithm in ENCRYPTION_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,\r\n desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const salt = options.salt ?? 'cipher-kit-salt';\r\n if (!$isStr(salt, 8)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Weak salt',\r\n desc: 'Salt must be a non-empty string with at least 8 characters',\r\n });\r\n }\r\n\r\n const info = options.info ?? 'cipher-kit';\r\n if (!$isStr(info)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Invalid info',\r\n desc: 'Info must be a non-empty string',\r\n });\r\n }\r\n\r\n const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n try {\r\n const derivedKey = nodeCrypto.hkdfSync(\r\n digestAlgo.node,\r\n secret.normalize('NFKC'),\r\n salt.normalize('NFKC'),\r\n info.normalize('NFKC'),\r\n encryptAlgo.keyBytes,\r\n );\r\n const key = nodeCrypto.createSecretKey(Buffer.from(derivedKey));\r\n const secretKey = Object.freeze({\r\n platform: 'node',\r\n digest: digest,\r\n algorithm: algorithm,\r\n key: key,\r\n }) as SecretKey<'node'>;\r\n\r\n return $ok({ result: secretKey });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Key Generation: Failed to create secret key', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $encrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Empty data for encryption',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Encryption: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const injectedKey = $isSecretKey(secretKey, 'node');\r\n if (!injectedKey) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Invalid Secret Key',\r\n desc: 'Expected a Node SecretKey',\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const iv = nodeCrypto.randomBytes(injectedKey.injected.ivLength);\r\n const cipher = nodeCrypto.createCipheriv(injectedKey.injected.node, injectedKey.key, iv);\r\n const encrypted = Buffer.concat([cipher.update(result), cipher.final()]);\r\n const tag = cipher.getAuthTag();\r\n\r\n const ivStr = $convertBytesToStr(iv, outputEncoding);\r\n const cipherStr = $convertBytesToStr(encrypted, outputEncoding);\r\n const tagStr = $convertBytesToStr(tag, outputEncoding);\r\n\r\n if (ivStr.error || cipherStr.error || tagStr.error) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Failed to convert IV or encrypted data or tag',\r\n desc: `Conversion error: ${ivStr.error || cipherStr.error || tagStr.error}`,\r\n });\r\n }\r\n\r\n return $ok(`${ivStr.result}.${cipherStr.result}.${tagStr.result}.`);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Encryption: Failed to encrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<string> {\r\n if (matchPattern(encrypted, 'node') === false) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid encrypted data format',\r\n desc: 'Encrypted data must be in the format \"iv.cipher.tag.\"',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n const outputEncoding = options.outputEncoding ?? 'utf8';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Decryption: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const [iv, cipher, tag] = encrypted.split('.', 4);\r\n if (!$isStr(iv) || !$isStr(cipher) || !$isStr(tag)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid encrypted data',\r\n desc: 'Encrypted data must contain valid IV, encrypted data, and tag components',\r\n });\r\n }\r\n\r\n const injectedKey = $isSecretKey(secretKey, 'node');\r\n if (!injectedKey) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid Secret Key',\r\n desc: 'Expected a Node SecretKey',\r\n });\r\n }\r\n\r\n const ivBytes = $convertStrToBytes(iv, inputEncoding);\r\n const cipherBytes = $convertStrToBytes(cipher, inputEncoding);\r\n const tagBytes = $convertStrToBytes(tag, inputEncoding);\r\n\r\n if (ivBytes.error || cipherBytes.error || tagBytes.error) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Failed to convert IV or encrypted data or tag',\r\n desc: `Conversion error: ${ivBytes.error || cipherBytes.error || tagBytes.error}`,\r\n });\r\n }\r\n\r\n try {\r\n const decipher = nodeCrypto.createDecipheriv(injectedKey.injected.node, injectedKey.key, ivBytes.result);\r\n decipher.setAuthTag(tagBytes.result);\r\n const decrypted = Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);\r\n\r\n return $convertBytesToStr(decrypted, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Decryption: Failed to decrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\nexport function $encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): Result<string> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return $encrypt(result, secretKey, options);\r\n}\r\n\r\nexport function $decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<{ result: T }> {\r\n const { result, error } = $decrypt(encrypted, secretKey, options);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport function $hash(data: string, options: HashOptions = {}): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Hashing: Empty data for hashing',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Hashing: Unsupported encoding: ${inputEncoding} or ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const hashed = nodeCrypto.createHash(digestAlgo.node).update(result).digest();\r\n return $convertBytesToStr(hashed, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Hashing: Failed to hash data with Crypto NodeJS', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $hashObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n options: HashOptions = {},\r\n): Result<string> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return $hash(result, options);\r\n}\r\n\r\nexport function $hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ hash: string; salt: string }> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Empty password for hashing',\r\n desc: 'Password must be a non-empty string',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Password Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Password Hashing: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const saltLength = options.saltLength ?? 16;\r\n if (typeof saltLength !== 'number' || saltLength < 8) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Weak salt length',\r\n desc: 'Salt length must be a number and at least 8 bytes (recommended 16 or more)',\r\n });\r\n }\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Weak iterations count',\r\n desc: 'Iterations must be a number and at least 1000 (recommended 320,000 or more)',\r\n });\r\n }\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Invalid key length',\r\n desc: 'Key length must be a number and at least 16 bytes (recommended 64 or more)',\r\n });\r\n }\r\n\r\n try {\r\n const salt = nodeCrypto.randomBytes(saltLength);\r\n const hash = nodeCrypto.pbkdf2Sync(password.normalize('NFKC'), salt, iterations, keyLength, digestAlgo.node);\r\n\r\n return $ok({ salt: salt.toString(outputEncoding), hash: hash.toString(outputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Password Hashing: Failed to hash password', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): boolean {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) return false;\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) return false;\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding)) return false;\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) return false;\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) return false;\r\n\r\n const saltBytes = $convertStrToBytes(salt, inputEncoding);\r\n if (saltBytes.error) return false;\r\n\r\n const hashedPasswordBytes = $convertStrToBytes(hashedPassword, inputEncoding);\r\n if (hashedPasswordBytes.error) return false;\r\n\r\n try {\r\n return nodeCrypto.timingSafeEqual(\r\n nodeCrypto.pbkdf2Sync(password.normalize('NFKC'), saltBytes.result, iterations, keyLength, digestAlgo.node),\r\n hashedPasswordBytes.result,\r\n );\r\n } catch {\r\n return false;\r\n }\r\n}\r\n"]}
@@ -1,4 +1,4 @@
1
- import { __export, $fmtResultErr, $ok, $err, $fmtError, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, ENCODINGS, isSecretKey, matchPattern, $stringifyObj, $parseToObj } from './chunk-LOJKJJX5.js';
1
+ import { __export, $fmtResultErr, $ok, $err, $fmtError, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, ENCODINGS, $isSecretKey, matchPattern, $stringifyObj, $parseToObj } from './chunk-YPYDYYV2.js';
2
2
 
3
3
  // src/web/kit.ts
4
4
  var kit_exports = {};
@@ -13,6 +13,7 @@ __export(kit_exports, {
13
13
  encryptObj: () => encryptObj,
14
14
  generateUuid: () => generateUuid,
15
15
  hash: () => hash,
16
+ hashObj: () => hashObj,
16
17
  hashPassword: () => hashPassword,
17
18
  tryConvertBytesToStr: () => tryConvertBytesToStr,
18
19
  tryConvertEncoding: () => tryConvertEncoding,
@@ -24,6 +25,7 @@ __export(kit_exports, {
24
25
  tryEncryptObj: () => tryEncryptObj,
25
26
  tryGenerateUuid: () => tryGenerateUuid,
26
27
  tryHash: () => tryHash,
28
+ tryHashObj: () => tryHashObj,
27
29
  tryHashPassword: () => tryHashPassword,
28
30
  verifyPassword: () => verifyPassword
29
31
  });
@@ -213,7 +215,7 @@ async function $createSecretKey(secret, options = {}) {
213
215
  const secretKey = Object.freeze({
214
216
  platform: "web",
215
217
  digest,
216
- algo: encryptAlgo,
218
+ algorithm,
217
219
  key
218
220
  });
219
221
  return $ok({ result: secretKey });
@@ -239,7 +241,8 @@ async function $encrypt(data, secretKey, options = {}) {
239
241
  desc: "Use base64, base64url, hex, utf8, or latin1"
240
242
  });
241
243
  }
242
- if (!isSecretKey(secretKey, "web")) {
244
+ const injectedKey = $isSecretKey(secretKey, "web");
245
+ if (!injectedKey) {
243
246
  return $err({
244
247
  msg: "Crypto Web API - Encryption: Invalid Secret Key",
245
248
  desc: "Expected a Web SecretKey"
@@ -248,8 +251,12 @@ async function $encrypt(data, secretKey, options = {}) {
248
251
  const { result, error } = $convertStrToBytes(data, inputEncoding);
249
252
  if (error) return $err(error);
250
253
  try {
251
- const iv = crypto.getRandomValues(new Uint8Array(secretKey.algo.ivLength));
252
- const cipherWithTag = await crypto.subtle.encrypt({ name: secretKey.algo.web, iv }, secretKey.key, result);
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
+ );
253
260
  const ivStr = $convertBytesToStr(iv, outputEncoding);
254
261
  const cipherStr = $convertBytesToStr(cipherWithTag, outputEncoding);
255
262
  if (ivStr.error || cipherStr.error) {
@@ -285,7 +292,8 @@ async function $decrypt(encrypted, secretKey, options = {}) {
285
292
  desc: "Encrypted data must contain valid IV, encrypted and tag components"
286
293
  });
287
294
  }
288
- if (!isSecretKey(secretKey, "web")) {
295
+ const injectedKey = $isSecretKey(secretKey, "web");
296
+ if (!injectedKey) {
289
297
  return $err({
290
298
  msg: "Crypto Web API - Decryption: Invalid Secret Key",
291
299
  desc: "Expected a Web SecretKey"
@@ -301,8 +309,8 @@ async function $decrypt(encrypted, secretKey, options = {}) {
301
309
  }
302
310
  try {
303
311
  const decrypted = await crypto.subtle.decrypt(
304
- { name: secretKey.algo.web, iv: ivBytes.result },
305
- secretKey.key,
312
+ { name: injectedKey.injected.web, iv: ivBytes.result },
313
+ injectedKey.key,
306
314
  cipherWithTagBytes.result
307
315
  );
308
316
  return $convertBytesToStr(decrypted, "utf8");
@@ -349,6 +357,11 @@ async function $hash(data, options = {}) {
349
357
  return $err({ msg: "Crypto Web API - Hashing: Failed to hash data", desc: $fmtError(error2) });
350
358
  }
351
359
  }
360
+ async function $hashObj(data, options = {}) {
361
+ const { result, error } = $stringifyObj(data);
362
+ if (error) return $err(error);
363
+ return await $hash(result, options);
364
+ }
352
365
  async function $hashPassword(password, options = {}) {
353
366
  if (!$isStr(password)) {
354
367
  return $err({
@@ -482,7 +495,7 @@ async function tryEncrypt(data, secretKey, options = {}) {
482
495
  return await $encrypt(data, secretKey, options);
483
496
  }
484
497
  async function encrypt(data, secretKey, options = {}) {
485
- const { result, error } = await $encrypt(data, secretKey);
498
+ const { result, error } = await $encrypt(data, secretKey, options);
486
499
  if (error) throw new Error($fmtResultErr(error));
487
500
  return result;
488
501
  }
@@ -518,6 +531,14 @@ async function hash(data, options = {}) {
518
531
  if (error) throw new Error($fmtResultErr(error));
519
532
  return result;
520
533
  }
534
+ async function tryHashObj(data, options = {}) {
535
+ return await $hashObj(data, options);
536
+ }
537
+ async function hashObj(data, options = {}) {
538
+ const { result, error } = await $hashObj(data, options);
539
+ if (error) throw new Error($fmtResultErr(error));
540
+ return result;
541
+ }
521
542
  async function tryHashPassword(password, options = {}) {
522
543
  return await $hashPassword(password, options);
523
544
  }
@@ -554,6 +575,6 @@ function convertEncoding(data, from, to) {
554
575
  return result;
555
576
  }
556
577
 
557
- export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, kit_exports, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword };
558
- //# sourceMappingURL=chunk-SUGN4VDZ.js.map
559
- //# sourceMappingURL=chunk-SUGN4VDZ.js.map
578
+ export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashObj, hashPassword, kit_exports, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashObj, tryHashPassword, verifyPassword };
579
+ //# sourceMappingURL=chunk-BNQERV4S.js.map
580
+ //# sourceMappingURL=chunk-BNQERV4S.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/web/kit.ts","../src/web/web-encode.ts","../src/web/web-encrypt.ts"],"names":["error","hash"],"mappings":";;;AAAA,IAAA,WAAA,GAAA;AAAA,QAAA,CAAA,WAAA,EAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,IAAA,EAAA,MAAA,IAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,cAAA,EAAA,MAAA;AAAA,CAAA,CAAA;;;ACKO,IAA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AAS,iBAAA,CAAkB,IAAA,EAAgC,cAAA,GAA2B,MAAA,EAAgB;AAC3G,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,kBAAA,CAAmB,MAAM,cAAc,CAAA;AACjE,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAUO,SAAS,kBAAA,CAAmB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAA0C;AACzG,EAAA,OAAO,gBAAA,CAAiB,IAAA,EAAM,IAAA,EAAM,EAAE,CAAA;AACxC;AAWO,SAAS,eAAA,CAAgB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAAsB;AAClF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,KAAU,gBAAA,CAAiB,IAAA,EAAM,MAAM,EAAE,CAAA;AACzD,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT","file":"chunk-BNQERV4S.js","sourcesContent":["import { $fmtResultErr, type Result } from '~/helpers/error';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n Encoding,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $convertBytesToStr, $convertEncoding, $convertStrToBytes } from './web-encode';\r\nimport {\r\n $createSecretKey,\r\n $decrypt,\r\n $decryptObj,\r\n $encrypt,\r\n $encryptObj,\r\n $generateUuid,\r\n $hash,\r\n $hashObj,\r\n $hashPassword,\r\n $verifyPassword,\r\n} from './web-encrypt';\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A Result containing a string representing the generated UUID or an error.\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A string representing the generated UUID.\r\n * @throws {Error} If UUID generation fails.\r\n */\r\nexport function generateUuid(): string {\r\n const { result, error } = $generateUuid();\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A Result containing a SecretKey object representing the derived secret key or an error.\r\n */\r\nexport async function tryCreateSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Promise<Result<{ result: SecretKey<'web'> }>> {\r\n return await $createSecretKey(secret, options);\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A SecretKey object representing the derived secret key.\r\n * @throws {Error} If the input key is invalid or key generation fails.\r\n */\r\nexport async function createSecretKey(secret: string, options: CreateSecretKeyOptions = {}): Promise<SecretKey<'web'>> {\r\n const { result, error } = await $createSecretKey(secret, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted data in the specified format or an error.\r\n */\r\nexport async function tryEncrypt(\r\n data: string,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n return await $encrypt(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted data in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport async function encrypt(\r\n data: string,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $encrypt(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing a string representing the decrypted data or an error.\r\n */\r\nexport async function tryDecrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<string>> {\r\n return await $decrypt(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A string representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport async function decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $decrypt(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted object in the specified format or an error.\r\n */\r\nexport async function tryEncryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n return await $encryptObj(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted object in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport async function encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $encryptObj(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing an object representing the decrypted data or an error.\r\n */\r\nexport async function tryDecryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<{ result: T }>> {\r\n return await $decryptObj<T>(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns An object representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport async function decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<T> {\r\n const { result, error } = await $decryptObj<T>(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash in base64url format or an error.\r\n */\r\nexport async function tryHash(data: string, options: HashOptions = {}): Promise<Result<string>> {\r\n return await $hash(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A string representing the SHA-256 hash in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport async function hash(data: string, options: HashOptions = {}): Promise<string> {\r\n const { result, error } = await $hash(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input object by first serializing it to a JSON string using stable key ordering,\r\n * then hashing the string using SHA-256 and returning the hash in base64url format.\r\n *\r\n * @param data - The input object to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash of the serialized object in base64url format or an error.\r\n * */\r\nexport async function tryHashObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n options: HashOptions = {},\r\n): Promise<Result<string>> {\r\n return await $hashObj(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input object by first serializing it to a JSON string using stable key ordering,\r\n * then hashing the string using SHA-256 and returning the hash in base64url format.\r\n *\r\n * @param data - The input object to hash.\r\n * @returns A string representing the SHA-256 hash of the serialized object in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport async function hashObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n options: HashOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $hashObj(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns A Result containing an object with the hash and salt, or an error.\r\n */\r\nexport async function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Promise<Result<{ hash: string; salt: string }>> {\r\n return await $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns An object with the hash and salt.\r\n * @throws {Error} If the input password is invalid or hashing fails.\r\n */\r\nexport async function hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Promise<{ hash: string; salt: string }> {\r\n const { hash, salt, error } = await $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { hash, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a hashed password and salt.\r\n *\r\n * @param password - The password to verify.\r\n * @param hashedPassword - The hashed password to compare against (in base64url format).\r\n * @param salt - The salt used during hashing (in base64url format).\r\n * @returns A boolean indicating whether the password matches the hashed password.\r\n */\r\nexport async function verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): Promise<boolean> {\r\n return await $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing a Uint8Array with the encoded data or an error.\r\n */\r\nexport function tryConvertStrToBytes(\r\n data: string,\r\n inputEncoding: Encoding = 'utf8',\r\n): Result<{ result: Uint8Array<ArrayBuffer> }> {\r\n return $convertStrToBytes(data, inputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Uint8Array containing the encoded data.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Uint8Array<ArrayBuffer> {\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Converts a Uint8Array or ArrayBuffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Uint8Array or ArrayBuffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing the string representation of the Uint8Array or an error.\r\n */\r\nexport function tryConvertBytesToStr(\r\n data: Uint8Array | ArrayBuffer,\r\n outputEncoding: Encoding = 'utf8',\r\n): Result<string> {\r\n return $convertBytesToStr(data, outputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a Uint8Array or ArrayBuffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Uint8Array or ArrayBuffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A string representation of the Uint8Array in the specified format.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertBytesToStr(data: Uint8Array | ArrayBuffer, outputEncoding: Encoding = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, outputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A Result containing the converted string or an error.\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A converted string.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertEncoding(data: string, from: Encoding, to: Encoding): string {\r\n const { result, error } = $convertEncoding(data, from, to);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n","import { ENCODINGS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { Encoding } from '~/helpers/types';\r\nimport { $isStr } from '~/helpers/validate';\r\n\r\nexport const textEncoder = new TextEncoder();\r\nexport const textDecoder = new TextDecoder();\r\n\r\nexport function $convertStrToBytes(\r\n data: string,\r\n inputEncoding: Encoding = 'utf8',\r\n): Result<{ result: Uint8Array<ArrayBuffer> }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto Web API - String to Bytes: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(inputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - String to Bytes: Unsupported encoding: ${inputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n try {\r\n const bytes = strToBytes[inputEncoding](data);\r\n return $ok({ result: bytes });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - String to Bytes: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertBytesToStr(data: Uint8Array | ArrayBuffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n if (!(data instanceof ArrayBuffer || data instanceof Uint8Array)) {\r\n return $err({\r\n msg: 'Crypto Web API - Bytes to String: Invalid data type',\r\n desc: 'Data must be an ArrayBuffer or Uint8Array',\r\n });\r\n }\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Bytes to String: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);\r\n const str = bytesToStr[outputEncoding](bytes);\r\n return $ok(str);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Bytes to String: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto Web API - Convert Format: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(from) || !ENCODINGS.includes(to)) {\r\n return $err({\r\n msg: `Crypto Web API - Convert Format: Unsupported encoding: from ${from} to ${to}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const bytes = $convertStrToBytes(data, from);\r\n if (bytes.error) return $err({ msg: bytes.error.message, desc: bytes.error.description });\r\n\r\n const str = $convertBytesToStr(bytes.result, to);\r\n if (str.error) return $err({ msg: str.error.message, desc: str.error.description });\r\n\r\n return $ok({ result: str.result });\r\n}\r\n\r\nconst strToBytes = {\r\n base64: $fromBase64,\r\n base64url: $fromBase64Url,\r\n hex: $fromHex,\r\n latin1: $fromLatin1,\r\n utf8: (data: string) => textEncoder.encode(data),\r\n} as const satisfies Record<Encoding, (data: string) => Uint8Array<ArrayBuffer>>;\r\n\r\nconst bytesToStr = {\r\n base64: $toBase64,\r\n base64url: $toBase64Url,\r\n hex: $toHex,\r\n latin1: $toLatin1,\r\n utf8: (data: Uint8Array) => textDecoder.decode(data),\r\n} as const satisfies Record<Encoding, (data: Uint8Array) => string>;\r\n\r\nfunction $toLatin1(bytes: Uint8Array): string {\r\n let out = '';\r\n const chunk = 1 << 15; // 32KiB per chunk\r\n for (let i = 0; i < bytes.length; i += chunk) {\r\n out += String.fromCharCode(...bytes.subarray(i, i + chunk));\r\n }\r\n return out;\r\n}\r\n\r\nfunction $fromLatin1(data: string): Uint8Array<ArrayBuffer> {\r\n const out = new Uint8Array(data.length);\r\n for (let i = 0; i < data.length; i++) {\r\n const charCode = data.charCodeAt(i);\r\n if (charCode > 255) throw new Error('Invalid latin1 string');\r\n out[i] = charCode;\r\n }\r\n return out;\r\n}\r\n\r\nfunction $toBase64(bytes: Uint8Array): string {\r\n return btoa($toLatin1(bytes));\r\n}\r\n\r\nfunction $fromBase64(data: string): Uint8Array<ArrayBuffer> {\r\n return $fromLatin1(atob(data));\r\n}\r\n\r\nfunction $toBase64Url(bytes: Uint8Array): string {\r\n return $toBase64(bytes).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '');\r\n}\r\n\r\nfunction $fromBase64Url(data: string): Uint8Array<ArrayBuffer> {\r\n let base64 = data.replace(/-/g, '+').replace(/_/g, '/');\r\n const padLen = (4 - (base64.length % 4)) % 4;\r\n base64 += '='.repeat(padLen);\r\n return $fromBase64(base64);\r\n}\r\n\r\nfunction $toHex(bytes: Uint8Array): string {\r\n return Array.from(bytes)\r\n .map((b) => b.toString(16).padStart(2, '0'))\r\n .join('');\r\n}\r\n\r\nfunction $fromHex(data: string): Uint8Array<ArrayBuffer> {\r\n const clean = data.startsWith('0x') ? data.slice(2) : data;\r\n if (clean.length % 2 !== 0) throw new Error('Invalid hex string');\r\n const out = new Uint8Array(clean.length / 2);\r\n for (let i = 0; i < out.length; i++) {\r\n const byte = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);\r\n if (Number.isNaN(byte)) throw new Error('Invalid hex string');\r\n out[i] = byte;\r\n }\r\n return out;\r\n}\r\n","import { DIGEST_ALGORITHMS, ENCODINGS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $isSecretKey, $isStr, matchPattern } from '~/helpers/validate';\r\nimport { $convertBytesToStr, $convertStrToBytes, textEncoder } from './web-encode';\r\n\r\nexport function $generateUuid(): Result<string> {\r\n try {\r\n return $ok(crypto.randomUUID());\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - UUID Generation: Failed to generate UUID', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $createSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Promise<Result<{ result: SecretKey<'web'> }>> {\r\n if (!$isStr(secret)) {\r\n return $err({ msg: 'Crypto Web API - Key Generation: Empty Secret', desc: 'Secret must be a non-empty string' });\r\n }\r\n\r\n const algorithm = options.algorithm ?? 'aes256gcm';\r\n if (!(algorithm in ENCRYPTION_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,\r\n desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const salt = options.salt ?? 'cipher-kit-salt';\r\n if (!$isStr(salt, 8)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Weak salt',\r\n desc: 'Salt must be a non-empty string with at least 8 characters',\r\n });\r\n }\r\n\r\n const info = options.info ?? 'cipher-kit';\r\n if (!$isStr(info)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Invalid info',\r\n desc: 'Info must be a non-empty string',\r\n });\r\n }\r\n\r\n const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n try {\r\n const ikm = await crypto.subtle.importKey('raw', textEncoder.encode(secret.normalize('NFKC')), 'HKDF', false, [\r\n 'deriveKey',\r\n ]);\r\n const key = await crypto.subtle.deriveKey(\r\n {\r\n name: 'HKDF',\r\n hash: digestAlgo.web,\r\n salt: textEncoder.encode(salt.normalize('NFKC')),\r\n info: textEncoder.encode(info.normalize('NFKC')),\r\n },\r\n ikm,\r\n { name: encryptAlgo.web, length: encryptAlgo.keyBytes * 8 },\r\n true,\r\n ['encrypt', 'decrypt'],\r\n );\r\n const secretKey = Object.freeze({\r\n platform: 'web',\r\n digest: digest,\r\n algorithm: algorithm,\r\n key: key,\r\n }) as SecretKey<'web'>;\r\n\r\n return $ok({ result: secretKey });\r\n } catch (error) {\r\n return $err({\r\n msg: 'Crypto Web API - Key Generation: Failed to create secret key',\r\n desc: $fmtError(error),\r\n });\r\n }\r\n}\r\n\r\nexport async function $encrypt(\r\n data: string,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto Web API - Encryption: Empty data for encryption',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Encryption: Unsupported encoding: input ${inputEncoding} or output ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const injectedKey = $isSecretKey(secretKey, 'web');\r\n if (!injectedKey) {\r\n return $err({\r\n msg: 'Crypto Web API - Encryption: Invalid Secret Key',\r\n desc: 'Expected a Web SecretKey',\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const iv = crypto.getRandomValues(new Uint8Array(injectedKey.injected.ivLength));\r\n const cipherWithTag = await crypto.subtle.encrypt(\r\n { name: injectedKey.injected.web, iv: iv },\r\n injectedKey.key,\r\n result,\r\n );\r\n\r\n const ivStr = $convertBytesToStr(iv, outputEncoding);\r\n const cipherStr = $convertBytesToStr(cipherWithTag, outputEncoding);\r\n\r\n if (ivStr.error || cipherStr.error) {\r\n return $err({\r\n msg: 'Crypto Web API - Encryption: Failed to convert IV or encrypted data',\r\n desc: `Conversion error: ${ivStr.error || cipherStr.error}`,\r\n });\r\n }\r\n\r\n return $ok(`${ivStr.result}.${cipherStr.result}.`);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Encryption: Failed to encrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<string>> {\r\n if (matchPattern(encrypted, 'web') === false) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Invalid encrypted data format',\r\n desc: 'Encrypted data must be in the format \"iv.cipherWithTag.\"',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n const outputEncoding = options.outputEncoding ?? 'utf8';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Decryption: Unsupported encoding: input ${inputEncoding} or output ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const [iv, encryptedWithTag] = encrypted.split('.', 3);\r\n if (!$isStr(iv) || !$isStr(encryptedWithTag)) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Invalid encrypted data',\r\n desc: 'Encrypted data must contain valid IV, encrypted and tag components',\r\n });\r\n }\r\n\r\n const injectedKey = $isSecretKey(secretKey, 'web');\r\n if (!injectedKey) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Invalid Secret Key',\r\n desc: 'Expected a Web SecretKey',\r\n });\r\n }\r\n\r\n const ivBytes = $convertStrToBytes(iv, inputEncoding);\r\n const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, inputEncoding);\r\n\r\n if (ivBytes.error || cipherWithTagBytes.error) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Failed to convert IV or encrypted data',\r\n desc: `Conversion error: ${ivBytes.error || cipherWithTagBytes.error}`,\r\n });\r\n }\r\n\r\n try {\r\n const decrypted = await crypto.subtle.decrypt(\r\n { name: injectedKey.injected.web, iv: ivBytes.result },\r\n injectedKey.key,\r\n cipherWithTagBytes.result,\r\n );\r\n\r\n return $convertBytesToStr(decrypted, 'utf8');\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Decryption: Failed to decrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return await $encrypt(result, secretKey, options);\r\n}\r\n\r\nexport async function $decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<{ result: T }>> {\r\n const { result, error } = await $decrypt(encrypted, secretKey, options);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport async function $hash(data: string, options: HashOptions = {}): Promise<Result<string>> {\r\n if (!$isStr(data)) {\r\n return $err({ msg: 'Crypto Web API - Hashing: Empty data for hashing', desc: 'Data must be a non-empty string' });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Hashing: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto Web API - Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const hashed = await crypto.subtle.digest(digestAlgo.web, result);\r\n return $convertBytesToStr(hashed, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Hashing: Failed to hash data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $hashObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n options: HashOptions = {},\r\n): Promise<Result<string>> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return await $hash(result, options);\r\n}\r\n\r\nexport async function $hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Promise<Result<{ hash: string; salt: string }>> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Empty password',\r\n desc: 'Password must be a non-empty string',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto Web API - Password Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Password Hashing: Unsupported output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const saltLength = options.saltLength ?? 16;\r\n if (typeof saltLength !== 'number' || saltLength < 8) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Weak salt length',\r\n desc: 'Salt length must be a number and at least 8 bytes (recommended 16)',\r\n });\r\n }\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Weak iteration count',\r\n desc: 'Iterations must be a number and at least 1000 (recommended 320,000 or more)',\r\n });\r\n }\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Weak key length',\r\n desc: 'Key length must be a number and at least 16 bytes (recommended 64)',\r\n });\r\n }\r\n\r\n try {\r\n const salt = crypto.getRandomValues(new Uint8Array(saltLength));\r\n const baseKey = await crypto.subtle.importKey(\r\n 'raw',\r\n textEncoder.encode(password.normalize('NFKC')),\r\n 'PBKDF2',\r\n false,\r\n ['deriveBits'],\r\n );\r\n const bits = await crypto.subtle.deriveBits(\r\n { name: 'PBKDF2', salt, iterations: iterations, hash: digestAlgo.web },\r\n baseKey,\r\n keyLength * 8,\r\n );\r\n\r\n const saltStr = $convertBytesToStr(salt, outputEncoding);\r\n if (saltStr.error) return $err(saltStr.error);\r\n\r\n const hashedPasswordStr = $convertBytesToStr(bits, outputEncoding);\r\n if (hashedPasswordStr.error) return $err(hashedPasswordStr.error);\r\n\r\n return $ok({ hash: hashedPasswordStr.result, salt: saltStr.result });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Password Hashing: Failed to hash password', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): Promise<boolean> {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) return false;\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) return false;\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding)) return false;\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) return false;\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) return false;\r\n\r\n const saltBytes = $convertStrToBytes(salt, 'base64url');\r\n if (saltBytes.error) return false;\r\n\r\n const hashedPasswordBytes = $convertStrToBytes(hashedPassword, 'base64url');\r\n if (hashedPasswordBytes.error) return false;\r\n\r\n try {\r\n const baseKey = await crypto.subtle.importKey(\r\n 'raw',\r\n textEncoder.encode(password.normalize('NFKC')),\r\n 'PBKDF2',\r\n false,\r\n ['deriveBits'],\r\n );\r\n\r\n const bits = new Uint8Array(\r\n await crypto.subtle.deriveBits(\r\n {\r\n name: 'PBKDF2',\r\n salt: saltBytes.result,\r\n iterations: iterations,\r\n hash: digestAlgo.web,\r\n },\r\n baseKey,\r\n keyLength * 8,\r\n ),\r\n );\r\n\r\n if (bits.length !== hashedPasswordBytes.result.length) return false;\r\n\r\n let isMatch = true;\r\n for (let i = 0; i < bits.length; i++) {\r\n if (bits[i] !== hashedPasswordBytes.result[i]) isMatch = false;\r\n }\r\n\r\n return isMatch;\r\n } catch {\r\n return false;\r\n }\r\n}\r\n"]}