cipher-kit 2.0.0-beta.0 → 2.0.0-beta.1

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-J5YHHXDC.js → chunk-6HNTSQAP.js} +60 -29
  2. package/dist/chunk-6HNTSQAP.js.map +1 -0
  3. package/dist/{chunk-IYMK2O3K.cjs → chunk-DZLEO2JD.cjs} +136 -98
  4. package/dist/chunk-DZLEO2JD.cjs.map +1 -0
  5. package/dist/{chunk-M7V4HSRH.js → chunk-I25C3WUZ.js} +61 -26
  6. package/dist/chunk-I25C3WUZ.js.map +1 -0
  7. package/dist/{chunk-N2ZI2CIT.js → chunk-SRGSVNQ2.js} +66 -28
  8. package/dist/chunk-SRGSVNQ2.js.map +1 -0
  9. package/dist/{chunk-4IOWANDJ.cjs → chunk-UR5HPMNF.cjs} +128 -93
  10. package/dist/chunk-UR5HPMNF.cjs.map +1 -0
  11. package/dist/{chunk-D656BYPA.cjs → chunk-ZIVTPFKN.cjs} +60 -30
  12. package/dist/chunk-ZIVTPFKN.cjs.map +1 -0
  13. package/dist/{export-Bh49U7Yf.d.ts → export-C2vQXrZa.d.cts} +35 -26
  14. package/dist/{export-VUQLKGvM.d.ts → export-DEWwhS-S.d.ts} +35 -26
  15. package/dist/{export-CrT_Y2Cy.d.cts → export-DzNR1ht3.d.ts} +35 -26
  16. package/dist/{export-VtQvdOuA.d.cts → export-oVEmcDLI.d.cts} +35 -26
  17. package/dist/index.cjs +36 -40
  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 +33 -33
  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-CPWPEEAt.d.cts → validate-TRx4DDtg.d.cts} +46 -9
  26. package/dist/{validate-CPWPEEAt.d.ts → validate-TRx4DDtg.d.ts} +46 -9
  27. package/dist/web-api.cjs +33 -33
  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-4IOWANDJ.cjs.map +0 -1
  33. package/dist/chunk-D656BYPA.cjs.map +0 -1
  34. package/dist/chunk-IYMK2O3K.cjs.map +0 -1
  35. package/dist/chunk-J5YHHXDC.js.map +0 -1
  36. package/dist/chunk-M7V4HSRH.js.map +0 -1
  37. package/dist/chunk-N2ZI2CIT.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/web/kit.ts","../src/web/web-encode.ts","../src/web/web-encrypt.ts"],"names":["__export","$isStr","$err","ENCODING_FORMATS","$ok","$fmtError","ENCRYPTION_ALGORITHMS","DIGEST_ALGORITHMS","isSecretKey","matchPattern","$stringifyObj","$parseToObj","PASSWORD_HASHING","$fmtResultErr","hash"],"mappings":";;;;;AAAA,IAAA,WAAA,GAAA;AAAAA,0BAAA,CAAA,WAAA,EAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;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,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,gBAAA,EAAA,MAAA,gBAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;A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,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,KAAU,cAAA,CAAe,IAAA,EAAM,MAAM,EAAE,CAAA;AACvD,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAMA,+BAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT","file":"chunk-DZLEO2JD.cjs","sourcesContent":["import type { DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $fmtResultErr, type Result } from '~/helpers/error';\r\nimport type { EncodingFormat, SecretKey } from '~/helpers/types';\r\nimport { $convertBytesToStr, $convertFormat, $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 $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: {\r\n algorithm?: keyof typeof ENCRYPTION_ALGORITHMS;\r\n digest?: keyof typeof DIGEST_ALGORITHMS;\r\n salt?: string;\r\n info?: string;\r\n } = {},\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(\r\n secret: string,\r\n options: {\r\n algorithm?: keyof typeof ENCRYPTION_ALGORITHMS;\r\n digest?: keyof typeof DIGEST_ALGORITHMS;\r\n salt?: string;\r\n info?: string;\r\n } = {},\r\n): 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(data: string, secretKey: SecretKey<'web'>): Promise<Result<string>> {\r\n return await $encrypt(data, secretKey);\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(data: string, secretKey: SecretKey<'web'>): Promise<string> {\r\n const { result, error } = await $encrypt(data, secretKey);\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(encrypted: string, secretKey: SecretKey<'web'>): Promise<Result<string>> {\r\n return await $decrypt(encrypted, secretKey);\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(encrypted: string, secretKey: SecretKey<'web'>): Promise<string> {\r\n const { result, error } = await $decrypt(encrypted, secretKey);\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): Promise<Result<string>> {\r\n return await $encryptObj(data, secretKey);\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): Promise<string> {\r\n const { result, error } = await $encryptObj(data, secretKey);\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): Promise<Result<{ result: T }>> {\r\n return await $decryptObj<T>(encrypted, secretKey);\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): Promise<{ result: T }> {\r\n const { result, error } = await $decryptObj<T>(encrypted, secretKey);\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): Promise<Result<string>> {\r\n return await $hash(data);\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): Promise<string> {\r\n const { result, error } = await $hash(data);\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(password: string): Promise<Result<{ hash: string; salt: string }>> {\r\n return await $hashPassword(password);\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(password: string): Promise<{ hash: string; salt: string }> {\r\n const { hash, salt, error } = await $hashPassword(password);\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(password: string, hashedPassword: string, salt: string): Promise<boolean> {\r\n return await $verifyPassword(password, hashedPassword, salt);\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 format - 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 format: EncodingFormat = 'utf8',\r\n): Result<{ result: Uint8Array<ArrayBuffer> }> {\r\n return $convertStrToBytes(data, format);\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 format - 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, format: EncodingFormat = 'utf8'): Uint8Array<ArrayBuffer> {\r\n const { result, error } = $convertStrToBytes(data, format);\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 format - 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(data: Uint8Array | ArrayBuffer, format: EncodingFormat = 'utf8'): Result<string> {\r\n return $convertBytesToStr(data, format);\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 format - 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, format: EncodingFormat = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, format);\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 tryConvertFormat(data: string, from: EncodingFormat, to: EncodingFormat): Result<{ result: string }> {\r\n return $convertFormat(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 convertFormat(data: string, from: EncodingFormat, to: EncodingFormat): string {\r\n const { result, error } = $convertFormat(data, from, to);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n","import { ENCODING_FORMATS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { EncodingFormat } 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 format: EncodingFormat = '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 (!ENCODING_FORMATS.includes(format)) {\r\n return $err({\r\n msg: `Crypto Web API - String to Bytes: Unsupported encode format: ${format}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n try {\r\n const bytes = strToBytes[format](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, format: EncodingFormat = '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 (!ENCODING_FORMATS.includes(format)) {\r\n return $err({\r\n msg: `Crypto Web API - Bytes to String: Unsupported format: ${format}`,\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[format](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 $convertFormat(data: string, from: EncodingFormat, to: EncodingFormat): 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 (!ENCODING_FORMATS.includes(from) || !ENCODING_FORMATS.includes(to)) {\r\n return $err({\r\n msg: `Crypto Web API - Convert Format: Unsupported format: 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<EncodingFormat, (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<EncodingFormat, (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, ENCRYPTION_ALGORITHMS, PASSWORD_HASHING } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type { SecretKey } from '~/helpers/types';\r\nimport { $isStr, isSecretKey, 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: {\r\n algorithm?: keyof typeof ENCRYPTION_ALGORITHMS;\r\n digest?: keyof typeof DIGEST_ALGORITHMS;\r\n salt?: string;\r\n info?: string;\r\n } = {},\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 algo: encryptAlgo,\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(data: string, secretKey: SecretKey<'web'>): 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 if (!isSecretKey<'web'>(secretKey, 'web')) {\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 bytes = $convertStrToBytes(data, 'utf8');\r\n if (bytes.error) return $err(bytes.error);\r\n\r\n try {\r\n const iv = crypto.getRandomValues(new Uint8Array(secretKey.algo.ivLength));\r\n const cipherWithTag = await crypto.subtle.encrypt(\r\n { name: secretKey.algo.web, iv: iv },\r\n secretKey.key,\r\n bytes.result,\r\n );\r\n\r\n const ivStr = $convertBytesToStr(iv, 'base64url');\r\n const cipherStr = $convertBytesToStr(cipherWithTag, 'base64url');\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(encrypted: string, secretKey: SecretKey<'web'>): 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 [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 if (!isSecretKey<'web'>(secretKey, 'web')) {\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, 'base64url');\r\n const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, 'base64url');\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: secretKey.algo.web, iv: ivBytes.result },\r\n secretKey.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): Promise<Result<string>> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return await $encrypt(result, secretKey);\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): Promise<Result<{ result: T }>> {\r\n const { result, error } = await $decrypt(encrypted, secretKey);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport async function $hash(data: string): Promise<Result<string>> {\r\n if (!$isStr(data, 0)) {\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 bytes = $convertStrToBytes(data, 'utf8');\r\n if (bytes.error) return $err(bytes.error);\r\n\r\n try {\r\n const hashed = await crypto.subtle.digest(DIGEST_ALGORITHMS.sha256.web, bytes.result);\r\n return $convertBytesToStr(hashed, 'base64url');\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 $hashPassword(password: string): 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 try {\r\n const salt = crypto.getRandomValues(new Uint8Array(16));\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: PASSWORD_HASHING.pbkdf2.iterations, hash: DIGEST_ALGORITHMS.sha512.web },\r\n baseKey,\r\n PASSWORD_HASHING.pbkdf2.keyLength * 8,\r\n );\r\n\r\n const saltStr = $convertBytesToStr(salt, 'base64url');\r\n if (saltStr.error) return $err(saltStr.error);\r\n\r\n const hashedPasswordStr = $convertBytesToStr(bits, 'base64url');\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(password: string, hashedPassword: string, salt: string): Promise<boolean> {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) return false;\r\n\r\n const saltBytes = $convertStrToBytes(salt, 'base64url');\r\n if (saltBytes.error) return false;\r\n\r\n const { result: hashedPasswordBytes, error: hashedPasswordError } = $convertStrToBytes(hashedPassword, 'base64url');\r\n if (hashedPasswordError) 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: PASSWORD_HASHING.pbkdf2.iterations,\r\n hash: DIGEST_ALGORITHMS.sha512.web,\r\n },\r\n baseKey,\r\n PASSWORD_HASHING.pbkdf2.keyLength * 8,\r\n ),\r\n );\r\n\r\n if (bits.length !== hashedPasswordBytes.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[i]) isMatch = false;\r\n }\r\n\r\n return isMatch;\r\n } catch {\r\n return false;\r\n }\r\n}\r\n"]}
@@ -1,4 +1,4 @@
1
- import { __export, isNodeSecretKey, $fmtResultErr, $ok, $err, $fmtError, $isStr, DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS, matchPattern, $stringifyObj, $parseToObj, PASSWORD_HASHING, ENCODING_FORMATS } from './chunk-J5YHHXDC.js';
1
+ import { __export, $fmtResultErr, $ok, $err, $fmtError, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, isSecretKey, matchPattern, $stringifyObj, $parseToObj, PASSWORD_HASHING, ENCODING_FORMATS } from './chunk-6HNTSQAP.js';
2
2
  import { Buffer } from 'buffer';
3
3
  import nodeCrypto from 'crypto';
4
4
 
@@ -16,7 +16,6 @@ __export(kit_exports, {
16
16
  generateUuid: () => generateUuid,
17
17
  hash: () => hash,
18
18
  hashPassword: () => hashPassword,
19
- isNodeSecretKey: () => isNodeSecretKey,
20
19
  tryConvertBytesToStr: () => tryConvertBytesToStr,
21
20
  tryConvertFormat: () => tryConvertFormat,
22
21
  tryConvertStrToBytes: () => tryConvertStrToBytes,
@@ -94,19 +93,55 @@ function $generateUuid() {
94
93
  return $err({ msg: "Crypto NodeJS API - UUID Generation: Failed to generate UUID", desc: $fmtError(error) });
95
94
  }
96
95
  }
97
- function $createSecretKey(key) {
98
- if (!$isStr(key)) {
99
- return $err({ msg: "Crypto NodeJS API - Key Generation: Empty key", desc: "Key must be a non-empty string" });
96
+ function $createSecretKey(secret, options = {}) {
97
+ if (!$isStr(secret)) {
98
+ return $err({ msg: "Crypto NodeJS API - Key Generation: Empty Secret", desc: "Secret must be a non-empty string" });
100
99
  }
100
+ const algorithm = options.algorithm ?? "aes256gcm";
101
+ if (!(algorithm in ENCRYPTION_ALGORITHMS)) {
102
+ return $err({
103
+ msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,
104
+ desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(", ")}`
105
+ });
106
+ }
107
+ const digest = options.digest ?? "sha256";
108
+ if (!(digest in DIGEST_ALGORITHMS)) {
109
+ return $err({
110
+ msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,
111
+ desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(", ")}`
112
+ });
113
+ }
114
+ const salt = options.salt ?? "cipher-kit-salt";
115
+ if (!$isStr(salt, 8)) {
116
+ return $err({
117
+ msg: "Crypto NodeJS API - Key Generation: Weak salt",
118
+ desc: "Salt must be a non-empty string with at least 8 characters"
119
+ });
120
+ }
121
+ const info = options.info ?? "cipher-kit";
122
+ if (!$isStr(info)) {
123
+ return $err({
124
+ msg: "Crypto NodeJS API - Key Generation: Invalid info",
125
+ desc: "Info must be a non-empty string"
126
+ });
127
+ }
128
+ const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];
129
+ const digestAlgo = DIGEST_ALGORITHMS[digest];
101
130
  try {
102
131
  const derivedKey = nodeCrypto.hkdfSync(
103
- DIGEST_ALGORITHMS.sha256.node,
104
- key.normalize("NFKC"),
105
- "cipher-kit-salt",
106
- "cipher-kit",
107
- ENCRYPTION_ALGORITHMS.aes256gcm.keyBytes
132
+ digestAlgo.node,
133
+ secret.normalize("NFKC"),
134
+ salt.normalize("NFKC"),
135
+ info.normalize("NFKC"),
136
+ encryptAlgo.keyBytes
108
137
  );
109
- const secretKey = nodeCrypto.createSecretKey(Buffer.from(derivedKey));
138
+ const key = nodeCrypto.createSecretKey(Buffer.from(derivedKey));
139
+ const secretKey = Object.freeze({
140
+ platform: "node",
141
+ digest,
142
+ algo: encryptAlgo,
143
+ key
144
+ });
110
145
  return $ok({ result: secretKey });
111
146
  } catch (error) {
112
147
  return $err({ msg: "Crypto NodeJS API - Key Generation: Failed to create secret key", desc: $fmtError(error) });
@@ -119,15 +154,15 @@ function $encrypt(data, secretKey) {
119
154
  desc: "Data must be a non-empty string"
120
155
  });
121
156
  }
122
- if (!isNodeSecretKey(secretKey)) {
157
+ if (!isSecretKey(secretKey, "node")) {
123
158
  return $err({
124
- msg: "Crypto NodeJS API - Encryption: Invalid encryption key",
125
- desc: "Expected a NodeKey (crypto.KeyObject)"
159
+ msg: "Crypto NodeJS API - Encryption: Invalid Secret Key",
160
+ desc: "Expected a Node SecretKey"
126
161
  });
127
162
  }
128
163
  try {
129
- const iv = nodeCrypto.randomBytes(ENCRYPTION_ALGORITHMS.aes256gcm.ivLength);
130
- const cipher = nodeCrypto.createCipheriv(ENCRYPTION_ALGORITHMS.aes256gcm.node, secretKey, iv);
164
+ const iv = nodeCrypto.randomBytes(secretKey.algo.ivLength);
165
+ const cipher = nodeCrypto.createCipheriv(secretKey.algo.node, secretKey.key, iv);
131
166
  const encrypted = Buffer.concat([cipher.update(data, "utf8"), cipher.final()]);
132
167
  const tag = cipher.getAuthTag();
133
168
  const ivStr = $convertBytesToStr(iv, "base64url");
@@ -158,10 +193,10 @@ function $decrypt(encrypted, secretKey) {
158
193
  desc: "Encrypted data must contain valid IV, encrypted data, and tag components"
159
194
  });
160
195
  }
161
- if (!isNodeSecretKey(secretKey)) {
196
+ if (!isSecretKey(secretKey, "node")) {
162
197
  return $err({
163
- msg: "Crypto NodeJS API - Decryption: Invalid decryption key",
164
- desc: "Expected a NodeKey (crypto.KeyObject)"
198
+ msg: "Crypto NodeJS API - Decryption: Invalid Secret Key",
199
+ desc: "Expected a Node SecretKey"
165
200
  });
166
201
  }
167
202
  const ivBytes = $convertStrToBytes(iv, "base64url");
@@ -174,7 +209,7 @@ function $decrypt(encrypted, secretKey) {
174
209
  });
175
210
  }
176
211
  try {
177
- const decipher = nodeCrypto.createDecipheriv(ENCRYPTION_ALGORITHMS.aes256gcm.node, secretKey, ivBytes.result);
212
+ const decipher = nodeCrypto.createDecipheriv(secretKey.algo.node, secretKey.key, ivBytes.result);
178
213
  decipher.setAuthTag(tagBytes.result);
179
214
  const decrypted = Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);
180
215
  return $convertBytesToStr(decrypted, "utf8");
@@ -258,11 +293,11 @@ function generateUuid() {
258
293
  if (error) throw new Error($fmtResultErr(error));
259
294
  return result;
260
295
  }
261
- function tryCreateSecretKey(key) {
262
- return $createSecretKey(key);
296
+ function tryCreateSecretKey(secret, options = {}) {
297
+ return $createSecretKey(secret, options);
263
298
  }
264
- function createSecretKey(key) {
265
- const { result, error } = $createSecretKey(key);
299
+ function createSecretKey(secret, options = {}) {
300
+ const { result, error } = $createSecretKey(secret, options);
266
301
  if (error) throw new Error($fmtResultErr(error));
267
302
  return result;
268
303
  }
@@ -343,5 +378,5 @@ function convertFormat(data, from, to) {
343
378
  }
344
379
 
345
380
  export { convertBytesToStr, convertFormat, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, kit_exports, tryConvertBytesToStr, tryConvertFormat, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword };
346
- //# sourceMappingURL=chunk-M7V4HSRH.js.map
347
- //# sourceMappingURL=chunk-M7V4HSRH.js.map
381
+ //# sourceMappingURL=chunk-I25C3WUZ.js.map
382
+ //# sourceMappingURL=chunk-I25C3WUZ.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","hash"],"mappings":";;;;;AAAA,IAAA,WAAA,GAAA;AAAA,QAAA,CAAA,WAAA,EAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;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,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,gBAAA,EAAA,MAAA,gBAAA;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,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,cAAA,EAAA,MAAA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file":"chunk-I25C3WUZ.js","sourcesContent":["import type { Buffer } from 'node:buffer';\r\nimport type { DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $fmtResultErr, type Result } from '~/helpers/error';\r\nimport type { EncodingFormat, SecretKey } from '~/helpers/types';\r\nimport { $convertBytesToStr, $convertFormat, $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 $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: {\r\n algorithm?: keyof typeof ENCRYPTION_ALGORITHMS;\r\n digest?: keyof typeof DIGEST_ALGORITHMS;\r\n salt?: string;\r\n info?: string;\r\n } = {},\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(\r\n secret: string,\r\n options: {\r\n algorithm?: keyof typeof ENCRYPTION_ALGORITHMS;\r\n digest?: keyof typeof DIGEST_ALGORITHMS;\r\n salt?: string;\r\n info?: string;\r\n } = {},\r\n): 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'>): Result<string> {\r\n return $encrypt(data, secretKey);\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'>): string {\r\n const { result, error } = $encrypt(data, secretKey);\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(encrypted: string, secretKey: SecretKey<'node'>): Result<string> {\r\n return $decrypt(encrypted, secretKey);\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'>): string {\r\n const { result, error } = $decrypt(encrypted, secretKey);\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): Result<string> {\r\n return $encryptObj(data, secretKey);\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>>(data: T, secretKey: SecretKey<'node'>): string {\r\n const { result, error } = $encryptObj(data, secretKey);\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): Result<{ result: T }> {\r\n return $decryptObj<T>(encrypted, secretKey);\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): { result: T } {\r\n const { result, error } = $decryptObj<T>(encrypted, secretKey);\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): Result<string> {\r\n return $hash(data);\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): string {\r\n const { result, error } = $hash(data);\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(password: string): Result<{ hash: string; salt: string }> {\r\n return $hashPassword(password);\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): { hash: string; salt: string } {\r\n const { hash, salt, error } = $hashPassword(password);\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(password: string, hashedPassword: string, salt: string): boolean {\r\n return $verifyPassword(password, hashedPassword, salt);\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 format - 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, format: EncodingFormat = 'utf8'): Result<{ result: Buffer }> {\r\n return $convertStrToBytes(data, format);\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 format - 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, format: EncodingFormat = 'utf8'): Buffer {\r\n const { result, error } = $convertStrToBytes(data, format);\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 format - 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, format: EncodingFormat = 'utf8'): Result<string> {\r\n return $convertBytesToStr(data, format);\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 format - 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, format: EncodingFormat = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, format);\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 tryConvertFormat(data: string, from: EncodingFormat, to: EncodingFormat): Result<{ result: string }> {\r\n return $convertFormat(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 convertFormat(data: string, from: EncodingFormat, to: EncodingFormat): string {\r\n const { result, error } = $convertFormat(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 { ENCODING_FORMATS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { EncodingFormat } from '~/helpers/types';\r\nimport { $isStr } from '~/helpers/validate';\r\n\r\nexport function $convertStrToBytes(data: string, format: EncodingFormat = '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 (!ENCODING_FORMATS.includes(format)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - String to Bytes: Unsupported encode format: ${format}`,\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, format) });\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, format: EncodingFormat = '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 (!ENCODING_FORMATS.includes(format)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Bytes to String: Unsupported format: ${format}`,\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(format));\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 $convertFormat(data: string, from: EncodingFormat, to: EncodingFormat): 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 (!ENCODING_FORMATS.includes(from) || !ENCODING_FORMATS.includes(to)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Convert Format: Unsupported format: 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, ENCRYPTION_ALGORITHMS, PASSWORD_HASHING } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type { SecretKey } from '~/helpers/types';\r\nimport { $isStr, isSecretKey, 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: {\r\n algorithm?: keyof typeof ENCRYPTION_ALGORITHMS;\r\n digest?: keyof typeof DIGEST_ALGORITHMS;\r\n salt?: string;\r\n info?: string;\r\n } = {},\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 algo: encryptAlgo,\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'>): 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 if (!isSecretKey(secretKey, 'node')) {\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 try {\r\n const iv = nodeCrypto.randomBytes(secretKey.algo.ivLength);\r\n const cipher = nodeCrypto.createCipheriv(secretKey.algo.node, secretKey.key, iv);\r\n const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]);\r\n const tag = cipher.getAuthTag();\r\n\r\n const ivStr = $convertBytesToStr(iv, 'base64url');\r\n const cipherStr = $convertBytesToStr(encrypted, 'base64url');\r\n const tagStr = $convertBytesToStr(tag, 'base64url');\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(encrypted: string, secretKey: SecretKey<'node'>): 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 [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 if (!isSecretKey(secretKey, 'node')) {\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, 'base64url');\r\n const cipherBytes = $convertStrToBytes(cipher, 'base64url');\r\n const tagBytes = $convertStrToBytes(tag, 'base64url');\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(secretKey.algo.node, secretKey.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, 'utf8');\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): Result<string> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return $encrypt(result, secretKey);\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): Result<{ result: T }> {\r\n const { result, error } = $decrypt(encrypted, secretKey);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport function $hash(data: string): Result<string> {\r\n if (!$isStr(data, 0)) {\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 try {\r\n const hashed = nodeCrypto.createHash(DIGEST_ALGORITHMS.sha256.node).update(data).digest();\r\n return $convertBytesToStr(hashed, 'base64url');\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 $hashPassword(password: string): 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 try {\r\n const salt = nodeCrypto.randomBytes(PASSWORD_HASHING.pbkdf2.saltLength);\r\n const hash = nodeCrypto.pbkdf2Sync(\r\n password.normalize('NFKC'),\r\n salt,\r\n PASSWORD_HASHING.pbkdf2.iterations,\r\n PASSWORD_HASHING.pbkdf2.keyLength,\r\n DIGEST_ALGORITHMS.sha512.node,\r\n );\r\n\r\n return $ok({ salt: salt.toString('base64url'), hash: hash.toString('base64url') });\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(password: string, hashedPassword: string, salt: string): boolean {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) 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 return nodeCrypto.timingSafeEqual(\r\n nodeCrypto.pbkdf2Sync(\r\n password.normalize('NFKC'),\r\n saltBytes.result,\r\n PASSWORD_HASHING.pbkdf2.iterations,\r\n PASSWORD_HASHING.pbkdf2.keyLength,\r\n DIGEST_ALGORITHMS.sha512.node,\r\n ),\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, isWebSecretKey, $fmtResultErr, $ok, $err, $fmtError, $isStr, DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS, matchPattern, $stringifyObj, $parseToObj, PASSWORD_HASHING, ENCODING_FORMATS } from './chunk-J5YHHXDC.js';
1
+ import { __export, $fmtResultErr, $ok, $err, $fmtError, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, isSecretKey, matchPattern, $stringifyObj, $parseToObj, PASSWORD_HASHING, ENCODING_FORMATS } from './chunk-6HNTSQAP.js';
2
2
 
3
3
  // src/web/kit.ts
4
4
  var kit_exports = {};
@@ -14,7 +14,6 @@ __export(kit_exports, {
14
14
  generateUuid: () => generateUuid,
15
15
  hash: () => hash,
16
16
  hashPassword: () => hashPassword,
17
- isWebSecretKey: () => isWebSecretKey,
18
17
  tryConvertBytesToStr: () => tryConvertBytesToStr,
19
18
  tryConvertFormat: () => tryConvertFormat,
20
19
  tryConvertStrToBytes: () => tryConvertStrToBytes,
@@ -161,23 +160,62 @@ function $generateUuid() {
161
160
  return $err({ msg: "Crypto Web API - UUID Generation: Failed to generate UUID", desc: $fmtError(error) });
162
161
  }
163
162
  }
164
- async function $createSecretKey(key) {
165
- if (!$isStr(key)) {
166
- return $err({ msg: "Crypto Web API - Key Generation: Empty key", desc: "Key must be a non-empty string" });
163
+ async function $createSecretKey(secret, options = {}) {
164
+ if (!$isStr(secret)) {
165
+ return $err({ msg: "Crypto Web API - Key Generation: Empty Secret", desc: "Secret must be a non-empty string" });
167
166
  }
167
+ const algorithm = options.algorithm ?? "aes256gcm";
168
+ if (!(algorithm in ENCRYPTION_ALGORITHMS)) {
169
+ return $err({
170
+ msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,
171
+ desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(", ")}`
172
+ });
173
+ }
174
+ const digest = options.digest ?? "sha256";
175
+ if (!(digest in DIGEST_ALGORITHMS)) {
176
+ return $err({
177
+ msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,
178
+ desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(", ")}`
179
+ });
180
+ }
181
+ const salt = options.salt ?? "cipher-kit-salt";
182
+ if (!$isStr(salt, 8)) {
183
+ return $err({
184
+ msg: "Crypto NodeJS API - Key Generation: Weak salt",
185
+ desc: "Salt must be a non-empty string with at least 8 characters"
186
+ });
187
+ }
188
+ const info = options.info ?? "cipher-kit";
189
+ if (!$isStr(info)) {
190
+ return $err({
191
+ msg: "Crypto NodeJS API - Key Generation: Invalid info",
192
+ desc: "Info must be a non-empty string"
193
+ });
194
+ }
195
+ const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];
196
+ const digestAlgo = DIGEST_ALGORITHMS[digest];
168
197
  try {
169
- const secretKey = await crypto.subtle.deriveKey(
198
+ const ikm = await crypto.subtle.importKey("raw", textEncoder.encode(secret.normalize("NFKC")), "HKDF", false, [
199
+ "deriveKey"
200
+ ]);
201
+ const key = await crypto.subtle.deriveKey(
170
202
  {
171
203
  name: "HKDF",
172
- hash: DIGEST_ALGORITHMS.sha256.web,
173
- salt: textEncoder.encode("cipher-kit-salt"),
174
- info: textEncoder.encode("cipher-kit")
204
+ hash: digestAlgo.web,
205
+ salt: textEncoder.encode(salt.normalize("NFKC")),
206
+ info: textEncoder.encode(info.normalize("NFKC"))
175
207
  },
176
- await crypto.subtle.importKey("raw", textEncoder.encode(key.normalize("NFKC")), "HKDF", false, ["deriveKey"]),
177
- { name: ENCRYPTION_ALGORITHMS.aes256gcm.web, length: ENCRYPTION_ALGORITHMS.aes256gcm.keyBytes * 8 },
208
+ ikm,
209
+ { name: encryptAlgo.web, length: encryptAlgo.keyBytes * 8 },
178
210
  true,
179
211
  ["encrypt", "decrypt"]
180
212
  );
213
+ const secretKey = Object.freeze({
214
+ platform: "web",
215
+ digest,
216
+ algo: encryptAlgo,
217
+ key
218
+ });
181
219
  return $ok({ result: secretKey });
182
220
  } catch (error) {
183
221
  return $err({
@@ -193,19 +231,19 @@ async function $encrypt(data, secretKey) {
193
231
  desc: "Data must be a non-empty string"
194
232
  });
195
233
  }
196
- if (!isWebSecretKey(secretKey)) {
234
+ if (!isSecretKey(secretKey, "web")) {
197
235
  return $err({
198
- msg: "Crypto Web API - Encryption: Invalid encryption key",
199
- desc: "Expected a WebApiKey (webcrypto.CryptoKey)"
236
+ msg: "Crypto Web API - Encryption: Invalid Secret Key",
237
+ desc: "Expected a Web SecretKey"
200
238
  });
201
239
  }
202
240
  const bytes = $convertStrToBytes(data, "utf8");
203
241
  if (bytes.error) return $err(bytes.error);
204
242
  try {
205
- const iv = crypto.getRandomValues(new Uint8Array(ENCRYPTION_ALGORITHMS.aes256gcm.ivLength));
243
+ const iv = crypto.getRandomValues(new Uint8Array(secretKey.algo.ivLength));
206
244
  const cipherWithTag = await crypto.subtle.encrypt(
207
- { name: ENCRYPTION_ALGORITHMS.aes256gcm.web, iv },
208
- secretKey,
245
+ { name: secretKey.algo.web, iv },
246
+ secretKey.key,
209
247
  bytes.result
210
248
  );
211
249
  const ivStr = $convertBytesToStr(iv, "base64url");
@@ -235,10 +273,10 @@ async function $decrypt(encrypted, secretKey) {
235
273
  desc: "Encrypted data must contain valid IV, encrypted and tag components"
236
274
  });
237
275
  }
238
- if (!isWebSecretKey(secretKey)) {
276
+ if (!isSecretKey(secretKey, "web")) {
239
277
  return $err({
240
- msg: "Crypto Web API - Decryption: Invalid decryption key",
241
- desc: "Expected a WebApiKey (webcrypto.CryptoKey)"
278
+ msg: "Crypto Web API - Decryption: Invalid Secret Key",
279
+ desc: "Expected a Web SecretKey"
242
280
  });
243
281
  }
244
282
  const ivBytes = $convertStrToBytes(iv, "base64url");
@@ -251,8 +289,8 @@ async function $decrypt(encrypted, secretKey) {
251
289
  }
252
290
  try {
253
291
  const decrypted = await crypto.subtle.decrypt(
254
- { name: ENCRYPTION_ALGORITHMS.aes256gcm.web, iv: ivBytes.result },
255
- secretKey,
292
+ { name: secretKey.algo.web, iv: ivBytes.result },
293
+ secretKey.key,
256
294
  cipherWithTagBytes.result
257
295
  );
258
296
  return $convertBytesToStr(decrypted, "utf8");
@@ -359,11 +397,11 @@ function generateUuid() {
359
397
  if (error) throw new Error($fmtResultErr(error));
360
398
  return result;
361
399
  }
362
- async function tryCreateSecretKey(key) {
363
- return await $createSecretKey(key);
400
+ async function tryCreateSecretKey(secret, options = {}) {
401
+ return await $createSecretKey(secret, options);
364
402
  }
365
- async function createSecretKey(key) {
366
- const { result, error } = await $createSecretKey(key);
403
+ async function createSecretKey(secret, options = {}) {
404
+ const { result, error } = await $createSecretKey(secret, options);
367
405
  if (error) throw new Error($fmtResultErr(error));
368
406
  return result;
369
407
  }
@@ -444,5 +482,5 @@ function convertFormat(data, from, to) {
444
482
  }
445
483
 
446
484
  export { convertBytesToStr, convertFormat, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, kit_exports, tryConvertBytesToStr, tryConvertFormat, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword };
447
- //# sourceMappingURL=chunk-N2ZI2CIT.js.map
448
- //# sourceMappingURL=chunk-N2ZI2CIT.js.map
485
+ //# sourceMappingURL=chunk-SRGSVNQ2.js.map
486
+ //# sourceMappingURL=chunk-SRGSVNQ2.js.map