cipher-kit 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -27
- package/dist/{chunk-3UX5MZ2P.cjs → chunk-BHG5RSUV.cjs} +22 -20
- package/dist/chunk-BHG5RSUV.cjs.map +1 -0
- package/dist/{chunk-4MFF6V3R.js → chunk-CRTOKS3Q.js} +76 -41
- package/dist/chunk-CRTOKS3Q.js.map +1 -0
- package/dist/{chunk-N2EW2FDZ.cjs → chunk-HMTHK2IY.cjs} +183 -148
- package/dist/chunk-HMTHK2IY.cjs.map +1 -0
- package/dist/{chunk-ACFPMIXO.js → chunk-RAEBT46G.js} +75 -40
- package/dist/chunk-RAEBT46G.js.map +1 -0
- package/dist/{chunk-FKSYSPJR.js → chunk-RUTGDMVR.js} +21 -20
- package/dist/chunk-RUTGDMVR.js.map +1 -0
- package/dist/{chunk-CVCDAHDW.cjs → chunk-UVEMRK5F.cjs} +189 -154
- package/dist/chunk-UVEMRK5F.cjs.map +1 -0
- package/dist/{export-BMvZq46v.d.ts → export-5hmOiU0J.d.cts} +194 -20
- package/dist/{export-CQNsJFh_.d.cts → export-BF9wW56f.d.ts} +194 -20
- package/dist/{export-llM6c7Do.d.ts → export-DVERZibl.d.cts} +194 -20
- package/dist/{export-55tHE0Bw.d.cts → export-w8sBcKXw.d.ts} +194 -20
- package/dist/index.cjs +12 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +33 -33
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-EHuJC5QQ.d.cts → validate-B3uHoP8n.d.cts} +26 -38
- package/dist/{validate-EHuJC5QQ.d.ts → validate-B3uHoP8n.d.ts} +26 -38
- package/dist/web-api.cjs +33 -33
- package/dist/web-api.d.cts +2 -2
- package/dist/web-api.d.ts +2 -2
- package/dist/web-api.js +2 -2
- package/package.json +6 -7
- package/dist/chunk-3UX5MZ2P.cjs.map +0 -1
- package/dist/chunk-4MFF6V3R.js.map +0 -1
- package/dist/chunk-ACFPMIXO.js.map +0 -1
- package/dist/chunk-CVCDAHDW.cjs.map +0 -1
- package/dist/chunk-FKSYSPJR.js.map +0 -1
- package/dist/chunk-N2EW2FDZ.cjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/node/kit.ts","../src/node/node-encode.ts","../src/node/node-encrypt.ts"],"names":["__export","$isStr","$err","ENCODING","$ok","Buffer","$fmtError","nodeCrypto","title","$isPlainObj","ENCRYPTION_ALGORITHMS","DIGEST_ALGORITHMS","CIPHER_ENCODING","$isSecretKey","$fmtResultErr","error","matchEncryptedPattern","$stringifyObj","$parseToObj","hash"],"mappings":";;;;;;;;;;;AAAA,IAAA,WAAA,GAAA;AAAAA,0BAAA,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,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;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,U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AAO,MAAA;AACT","file":"chunk-HMTHK2IY.cjs","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 { $isSecretKey } from \"~/helpers/validate\";\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 $hashPassword,\r\n $verifyPassword,\r\n} from \"./node-encrypt\";\r\n\r\n/**\r\n * Type guard to check if the provided value is a SecretKey object for Node.js environment.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Checking if the key in your hand is the right kind of key for the lock.\r\n *\r\n * @param x - The value to check.\r\n * @returns True if the value is a SecretKey object for Node.js, false otherwise.\r\n *\r\n * @example\r\n * ```ts\r\n * isNodeSecretKey(nodeKey); // true\r\n * isNodeSecretKey(webKey); // false\r\n * isNodeSecretKey({}); // false\r\n * ```\r\n */\r\nexport function isNodeSecretKey(x: unknown): x is SecretKey<\"node\"> {\r\n return $isSecretKey(x, \"node\") !== null;\r\n}\r\n\r\n/**\r\n * Safely generates a UUID (v4) (non-throwing).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It's like giving your pet a name tag with a super random name made of numbers and letters.\r\n * The chance of two pets getting the same name tag is practically zero, and it's very hard to guess!\r\n *\r\n * @returns A `Result` containing the UUID string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryGenerateUuid();\r\n *\r\n * if (success) console.log(result); // \"...\" (a UUID string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4) (throwing).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It's like giving your pet a name tag with a super random name made of numbers and letters.\r\n * The chance of two pets getting the same name tag is practically zero, and it's very hard to guess!\r\n *\r\n * @returns A UUID string.\r\n * @throws {Error} If UUID generation fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const uuid = generateUuid(); // \"...\" (a UUID string)\r\n * ```\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 * Safely derives a `SecretKey` from the provided string for encryption/decryption (non-throwing).\r\n *\r\n * Uses HKDF to derive a key from the input string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you want to create a special key for future use to lock your treasure box (data).\r\n * So, you stir in some secret ingredients (like salt and info) to make sure your key is one-of-a-kind.\r\n *\r\n * @param secret - The input string to derive the `SecretKey` from, must be at least 8 characters.\r\n * @param options.algorithm - The encryption algorithm to use (default: `'aes256gcm'`).\r\n * @param options.digest - The hash algorithm for HKDF (default: `'sha256'`).\r\n * @param options.salt - A salt string (default: `'cipher-kit-salt'`, must be ≥ 8 chars).\r\n * @param options.info - An info string (default: `'cipher-kit'`).\r\n * @returns A `Result` containing the derived `SecretKey` or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryCreateSecretKey(\"my-secret\");\r\n *\r\n * if (success) console.log(result); // SecretKey object\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 `SecretKey` from the provided string for encryption/decryption (throwing).\r\n *\r\n * Uses HKDF to derive a key from the input string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you want to create a special key for future use to lock your treasure box (data).\r\n * So, you stir in some secret ingredients (like salt and info) to make sure your key is one-of-a-kind.\r\n *\r\n * @param secret - The input string to derive the `SecretKey` from, must be at least 8 characters.\r\n * @param options.algorithm - The encryption algorithm to use (default: `'aes256gcm'`).\r\n * @param options.digest - The hash algorithm for HKDF (default: `'sha256'`).\r\n * @param options.salt - A salt string (default: `'cipher-kit-salt'`, must be ≥ 8 chars).\r\n * @param options.info - An info string (default: `'cipher-kit'`).\r\n * @returns The derived `SecretKey`.\r\n * @throws {Error} If key derivation fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\"); // SecretKey object\r\n * ```\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 * Safely encrypts a UTF-8 string using the provided `SecretKey` (non-throwing).\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You scramble a secret message with your special key,\r\n * creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @param data - A UTF-8 string to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns A `Result` containing the encrypted string in the specified format or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const {result, error, success} = tryEncrypt(\"Hello, World!\", secretKey);\r\n *\r\n * if (success) console.log(result); // \"iv.cipher.tag.\" (Encrypted string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 a UTF-8 string using the provided `SecretKey` (throwing).\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You scramble a secret message with your special key,\r\n * creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @param data - A UTF-8 string to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns The encrypted string in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encrypt(\"Hello, World!\", secretKey); // \"iv.cipher.tag.\" (Encrypted string)\r\n * ```\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 * Safely decrypts the input string using the provided `SecretKey` (non-throwing).\r\n *\r\n * Expects input in the format \"iv.cipher.tag.\" and returns the decrypted UTF-8 string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You take a scrambled secret message and use your special key to unscramble it,\r\n * revealing the original message inside.\r\n *\r\n * @param encrypted - The input string to decrypt, in the format \"iv.cipher.tag.\".\r\n * @param secretKey - The `SecretKey` object used for decryption.\r\n * @param options.inputEncoding - The encoding format for the input ciphertext (default: `'base64url'`).\r\n * @returns A `Result` containing the decrypted UTF-8 string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encrypt(\"Hello, World!\", secretKey);\r\n * const {result, error, success} = tryDecrypt(encrypted, secretKey);\r\n *\r\n * if (success) console.log(result); // \"Hello, World!\" (Decrypted string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 `SecretKey` (throwing).\r\n *\r\n * Expects input in the format \"iv.cipher.tag.\" and returns the decrypted UTF-8 string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You take a scrambled secret message and use your special key to unscramble it,\r\n * revealing the original message inside.\r\n *\r\n * @param encrypted - The input string to decrypt, in the format \"iv.cipher.tag.\".\r\n * @param secretKey - The `SecretKey` object used for decryption.\r\n * @param options.inputEncoding - The encoding format for the input ciphertext (default: `'base64url'`).\r\n * @returns The decrypted UTF-8 string.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encrypt(\"Hello, World!\", secretKey);\r\n * const decrypted = decrypt(encrypted, secretKey); // \"Hello, World!\" (Decrypted string)\r\n * ```\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 * Safely encrypts a plain object using the provided `SecretKey` (non-throwing).\r\n *\r\n * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you have a toy box (an object) that you want to keep secret.\r\n * So, you take a picture of your toy box (convert it to JSON), and scramble that with\r\n * your special key, creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @template T - The type of the plain object to encrypt.\r\n * @param data - A plain object to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns A `Result` containing the encrypted string in the specified format or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const {result, error, success} = tryEncryptObj({ a: 1 }, secretKey);\r\n *\r\n * if (success) console.log(result); // \"iv.cipher.tag.\" (Encrypted string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 a plain object using the provided `SecretKey` (throwing).\r\n *\r\n * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you have a toy box (an object) that you want to keep secret.\r\n * So, you take a picture of your toy box (convert it to JSON), and scramble that with\r\n * your special key, creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @template T - The type of the plain object to encrypt.\r\n * @param data - A plain object to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns The encrypted string in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encryptObj({ a: 1 }, secretKey); // \"iv.cipher.tag.\" (Encrypted string)\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): 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 * Safely decrypts an encrypted JSON string into an object (non-throwing).\r\n *\r\n * Expects input in the format `\"iv.cipher.tag.\"` and returns a plain object.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You rebuild your toy box (an object) by unscrambling the jumbled code (encrypted text),\r\n * using your special key to open it.\r\n *\r\n * @template T - The expected shape of the decrypted object.\r\n * @param encrypted - The encrypted string (format: `\"iv.cipher.tag.\"`).\r\n * @param secretKey - The `SecretKey` used for decryption.\r\n * @param options.inputEncoding - Input ciphertext encoding (default: `'base64url'`).\r\n * @returns A `Result` with the decrypted object on success, or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encryptObj({ a: 1 }, secretKey);\r\n * const {result, error, success} = tryDecryptObj<{ a: number }>(encrypted, secretKey);\r\n *\r\n * if (success) console.log(result); // { a: 1 } (Decrypted object)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 an encrypted JSON string into an object (throwing).\r\n *\r\n * Expects input in the format `\"iv.cipher.tag.\"` and returns a plain object.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You rebuild your toy box (an object) by unscrambling the jumbled code (encrypted text),\r\n * using your special key to open it.\r\n *\r\n * @template T - The expected shape of the decrypted object.\r\n * @param encrypted - The encrypted string (format: `\"iv.cipher.tag.\"`).\r\n * @param secretKey - The `SecretKey` used for decryption.\r\n * @param options.inputEncoding - Input ciphertext encoding (default: `'base64url'`).\r\n * @returns The decrypted object.\r\n * @throws {Error} If decryption or JSON parsing fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encryptObj({ a: 1 }, secretKey);\r\n * const obj = decryptObj<{ a: number }>(encrypted, secretKey); // obj.a === 1\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): 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 * Safely hashes a UTF-8 string (non-throwing).\r\n *\r\n * Uses the selected digest (default: `'sha256'`) and returns the hash\r\n * in the chosen encoding (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Like putting something in a blender and getting a smoothie, you can’t get the original ingredients back,\r\n * but the smoothie is always the same if you use the same ingredients.\r\n *\r\n * @param data - The input string to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`, default: `'sha256'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`, default: `'base64url'`).\r\n * @returns A `Result` with the hash string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryHash(\"my data\");\r\n *\r\n * if (success) console.log(result); // \"...\" (Hashed string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 a UTF-8 string (throwing).\r\n *\r\n * Uses the selected digest (default: `'sha256'`) and returns the hash\r\n * in the chosen encoding (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Like putting something in a blender and getting a smoothie, you can’t get the original ingredients back,\r\n * but the smoothie is always the same if you use the same ingredients.\r\n *\r\n * @param data - The input string to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha256'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @returns The hashed string.\r\n * @throws {Error} If input is invalid or hashing fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const hash = hash(\"my data\"); // \"...\" (Hashed string)\r\n * ```\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 * Safely hashes a password using PBKDF2 (non-throwing).\r\n *\r\n * Uses strong defaults (`sha512`, 320k iterations, 64-byte key, 16-byte salt) and\r\n * returns `{ hash, salt }` encoded (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * We take your password, mix in some random salt, and stir many times.\r\n * The result is a super-secret soup that’s hard to copy.\r\n *\r\n * @param password - The password to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @param options.saltLength - Length of the random salt in bytes (default: `16` bytes, min: `8` bytes).\r\n * @param options.iterations - Number of iterations (default: `320000`, min: `1000`).\r\n * @param options.keyLength - Length of the derived key in bytes (default: `64` bytes, min: `16` bytes).\r\n * @returns A `Result` with `{ result, salt }` or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, salt, error, success} = tryHashPassword(\"my-password\");\r\n *\r\n * if (success) console.log(result, salt); // \"...\" (Hashed password string), \"...\" (salt)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\r\n */\r\nexport function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ result: string; salt: string }> {\r\n return $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 (throwing).\r\n *\r\n * Uses strong defaults (`sha512`, 320k iterations, 64-byte key, 16-byte salt) and\r\n * returns `{ hash, salt }` encoded (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * We take your password, mix in some random salt, and stir many times.\r\n * The result is a super-secret soup that’s hard to copy.\r\n *\r\n * @param password - The password to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @param options.saltLength - Length of the random salt in bytes (default: `16` bytes, min: `8` bytes).\r\n * @param options.iterations - Number of iterations (default: `320000`, min: `1000`).\r\n * @param options.keyLength - Length of the derived key in bytes (default: `64` bytes, min: `16` bytes).\r\n * @returns `{ result, salt }` for storage.\r\n * @throws {Error} If inputs are invalid or hashing fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const { result, salt } = hashPassword(\"my-password\"); // \"...\" (Hashed password string), \"...\" (salt)\r\n * ```\r\n */\r\nexport function hashPassword(password: string, options: HashPasswordOptions = {}): { result: string; salt: string } {\r\n const { result, salt, error } = $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { result, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a stored PBKDF2 hash (non-throwing).\r\n *\r\n * Re-derives the key using the same parameters and compares in constant time, to prevent timing attacks.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * We follow the same recipe as when we made the secret.\r\n * If the new soup tastes exactly the same, the password is correct.\r\n *\r\n * @param password - The plain password to verify.\r\n * @param hashedPassword - The stored hash (encoded).\r\n * @param salt - The stored salt (encoded).\r\n * @param options.digest - Hash algorithm used during hashing (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).\r\n * @param options.inputEncoding - Encoding of the stored hash and salt (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @param options.iterations - Number of iterations used during hashing (default: `320000`).\r\n * @param options.keyLength - Length of the derived key in bytes used during hashing (default: `64` bytes).\r\n * @returns `true` if the password matches, otherwise `false`.\r\n *\r\n * @example\r\n * ```ts\r\n * const { result, salt } = hashPassword(\"my-password\");\r\n *\r\n * verifyPassword(\"my-password\", result, salt); // true\r\n * verifyPassword(\"wrong-password\", result, salt); // false\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 return $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Safely converts a string to a Node.js `Buffer` using the specified encoding (non-throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns your words into tiny computer building blocks (bytes) so computers can work with them.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding of the input string (default: `'utf8'`).\r\n * @returns A `Result` with `{ result: Buffer }` or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryConvertStrToBytes(\"Hello\", \"utf8\");\r\n *\r\n * if (success) console.log(result); // <Buffer ...>\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 Node.js `Buffer` using the specified encoding (throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns your words into tiny computer building blocks (bytes) so computers can work with them.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding of the input string (default: `'utf8'`).\r\n * @returns A `Buffer` containing the bytes.\r\n * @throws {Error} If input is invalid or conversion fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const bytes = convertStrToBytes(\"Hello\", \"utf8\"); // <Buffer ...>\r\n * ```\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 * Safely converts a Node.js `Buffer` to a string using the specified encoding (non-throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns the tiny computer building blocks (bytes) back into a readable sentence.\r\n *\r\n * @param data - The `Buffer` to convert.\r\n * @param outputEncoding - The output encoding (default: `'utf8'`).\r\n * @returns A `Result` with the string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const bytes = convertStrToBytes(\"Hello\", \"utf8\"); // <Buffer ...>\r\n * const {result, error, success} = tryConvertBytesToStr(bytes, \"utf8\");\r\n *\r\n * if (success) console.log(result); // \"Hello\"\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 Node.js `Buffer` to a string using the specified encoding (throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns the tiny computer building blocks (bytes) back into a readable sentence.\r\n *\r\n * @param data - The `Buffer` to convert.\r\n * @param outputEncoding - The output encoding (default: `'utf8'`).\r\n * @returns The encoded string.\r\n * @throws {Error} If input is invalid or conversion fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const bytes = convertStrToBytes(\"Hello\", \"utf8\"); // <Buffer ...>\r\n * const str = convertBytesToStr(bytes, \"utf8\"); // \"Hello\"\r\n * ```\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 * Safely converts text between encodings (non-throwing).\r\n *\r\n * Example: convert `'utf8'` text to `'base64url'`, or `'hex'` to `'utf8'`.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It’s like translating your sentence from one alphabet to another.\r\n *\r\n * @param data - The input string to convert.\r\n * @param from - The current encoding of `data`.\r\n * @param to - The target encoding for `data`.\r\n * @returns A `Result` with a string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryConvertEncoding(\"Hello\", \"utf8\", \"base64url\");\r\n *\r\n * if (success) console.log(result); // \"...\" (Base64url encoded string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<string> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Converts text between encodings (throwing).\r\n *\r\n * Example: convert `'utf8'` text to `'base64url'`, or `'hex'` to `'utf8'`.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It’s like translating your sentence from one alphabet to another.\r\n *\r\n * @param data - The input string to convert.\r\n * @param from - The current encoding of `data`.\r\n * @param to - The target encoding for `data`.\r\n * @returns The converted string.\r\n * @throws {Error} If encodings are invalid or conversion fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const encoded = convertEncoding(\"Hello\", \"utf8\", \"base64url\"); // \"...\" (Base64url encoded string)\r\n * ```\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 { ENCODING } 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 (!ENCODING.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 (!ENCODING.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<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.includes(from) || !ENCODING.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 { CIPHER_ENCODING, DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from \"~/helpers/consts\";\r\nimport { $err, $fmtError, $fmtResultErr, $ok, type Result, title } 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 { $isPlainObj, $isSecretKey, $isStr, matchEncryptedPattern } 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: `${title(\"node\", \"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, 8)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Key Generation\")}: Empty Secret`,\r\n desc: \"Secret must be a non-empty string with at least 8 characters\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<CreateSecretKeyOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Key Generation\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"Encryption\")}: Empty data for encryption`,\r\n desc: \"Data must be a non-empty string\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<EncryptOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Encryption\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\r\n }\r\n\r\n const outputEncoding = options.outputEncoding ?? \"base64url\";\r\n if (!CIPHER_ENCODING.includes(outputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Encryption\")}: Unsupported output encoding: ${outputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\r\n });\r\n }\r\n\r\n const injectedKey = $isSecretKey(secretKey, \"node\");\r\n if (!injectedKey) {\r\n return $err({\r\n msg: `${title(\"node\", \"Encryption\")}: Invalid Secret Key`,\r\n desc: \"Expected a Node SecretKey\",\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, \"utf8\");\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: ${$fmtResultErr(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: `${title(\"node\", \"Encryption\")}: Failed to encrypt data`, desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $decrypt(encrypted: string, secretKey: SecretKey<\"node\">, options?: DecryptOptions): Result<string> {\r\n if (!matchEncryptedPattern(encrypted, \"node\")) {\r\n return $err({\r\n msg: `${title(\"node\", \"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 if (!$isPlainObj<DecryptOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Decryption\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? \"base64url\";\r\n if (!CIPHER_ENCODING.includes(inputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Decryption\")}: Unsupported input encoding: ${inputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\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: `${title(\"node\", \"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: `${title(\"node\", \"Decryption\")}: Failed to convert IV or encrypted data or tag`,\r\n desc: `Conversion error: ${$fmtResultErr(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, \"utf8\");\r\n } catch (error) {\r\n return $err({ msg: `${title(\"node\", \"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: `${title(\"node\", \"Hashing\")}: Empty data for hashing`,\r\n desc: \"Data must be a non-empty string\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<HashOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Hashing\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\r\n }\r\n\r\n const outputEncoding = options.outputEncoding ?? \"base64url\";\r\n if (!CIPHER_ENCODING.includes(outputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Hashing\")}: Unsupported output encoding: ${outputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\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: `${title(\"node\", \"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, \"utf8\");\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: `${title(\"node\", \"Hashing\")}: Failed to hash data with Crypto NodeJS`, desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $hashPassword(\r\n password: string,\r\n options?: HashPasswordOptions,\r\n): Result<{ result: string; salt: string }> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Password Hashing\")}: Empty password for hashing`,\r\n desc: \"Password must be a non-empty string\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<HashPasswordOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Password Hashing\")}: Invalid options`,\r\n desc: \"Options must be an object\",\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: `${title(\"node\", \"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 (!CIPHER_ENCODING.includes(outputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Password Hashing\")}: Unsupported encoding: ${outputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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({ result: hash.toString(outputEncoding), salt: salt.toString(outputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: `${title(\"node\", \"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) || !$isPlainObj<VerifyPasswordOptions>(options)) {\r\n return false;\r\n }\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 (!CIPHER_ENCODING.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, $isSecretKey, $fmtResultErr, $ok, $err, $fmtError, title, $isStr, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, CIPHER_ENCODING,
|
|
1
|
+
import { __export, $isSecretKey, $fmtResultErr, $ok, $err, $fmtError, title, $isStr, $isPlainObj, ENCRYPTION_ALGORITHMS, DIGEST_ALGORITHMS, CIPHER_ENCODING, matchEncryptedPattern, $stringifyObj, $parseToObj, ENCODING } from './chunk-RUTGDMVR.js';
|
|
2
2
|
import { Buffer } from 'buffer';
|
|
3
3
|
import nodeCrypto from 'crypto';
|
|
4
4
|
|
|
@@ -94,9 +94,18 @@ function $generateUuid() {
|
|
|
94
94
|
return $err({ msg: `${title("node", "UUID Generation")}: Failed to generate UUID`, desc: $fmtError(error) });
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
function $createSecretKey(secret, options
|
|
98
|
-
if (!$isStr(secret)) {
|
|
99
|
-
return $err({
|
|
97
|
+
function $createSecretKey(secret, options) {
|
|
98
|
+
if (!$isStr(secret, 8)) {
|
|
99
|
+
return $err({
|
|
100
|
+
msg: `${title("node", "Key Generation")}: Empty Secret`,
|
|
101
|
+
desc: "Secret must be a non-empty string with at least 8 characters"
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (!$isPlainObj(options)) {
|
|
105
|
+
return $err({
|
|
106
|
+
msg: `${title("node", "Key Generation")}: Invalid options`,
|
|
107
|
+
desc: "Options must be an object"
|
|
108
|
+
});
|
|
100
109
|
}
|
|
101
110
|
const algorithm = options.algorithm ?? "aes256gcm";
|
|
102
111
|
if (!(algorithm in ENCRYPTION_ALGORITHMS)) {
|
|
@@ -148,17 +157,23 @@ function $createSecretKey(secret, options = {}) {
|
|
|
148
157
|
return $err({ msg: `${title("node", "Key Generation")}: Failed to create secret key`, desc: $fmtError(error) });
|
|
149
158
|
}
|
|
150
159
|
}
|
|
151
|
-
function $encrypt(data, secretKey, options
|
|
160
|
+
function $encrypt(data, secretKey, options) {
|
|
152
161
|
if (!$isStr(data)) {
|
|
153
162
|
return $err({
|
|
154
163
|
msg: `${title("node", "Encryption")}: Empty data for encryption`,
|
|
155
164
|
desc: "Data must be a non-empty string"
|
|
156
165
|
});
|
|
157
166
|
}
|
|
158
|
-
|
|
159
|
-
if (!CIPHER_ENCODING.includes(encoding)) {
|
|
167
|
+
if (!$isPlainObj(options)) {
|
|
160
168
|
return $err({
|
|
161
|
-
msg: `${title("node", "Encryption")}:
|
|
169
|
+
msg: `${title("node", "Encryption")}: Invalid options`,
|
|
170
|
+
desc: "Options must be an object"
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
174
|
+
if (!CIPHER_ENCODING.includes(outputEncoding)) {
|
|
175
|
+
return $err({
|
|
176
|
+
msg: `${title("node", "Encryption")}: Unsupported output encoding: ${outputEncoding}`,
|
|
162
177
|
desc: "Use base64, base64url, or hex"
|
|
163
178
|
});
|
|
164
179
|
}
|
|
@@ -176,9 +191,9 @@ function $encrypt(data, secretKey, options = {}) {
|
|
|
176
191
|
const cipher = nodeCrypto.createCipheriv(injectedKey.injected.node, injectedKey.key, iv);
|
|
177
192
|
const encrypted = Buffer.concat([cipher.update(result), cipher.final()]);
|
|
178
193
|
const tag = cipher.getAuthTag();
|
|
179
|
-
const ivStr = $convertBytesToStr(iv,
|
|
180
|
-
const cipherStr = $convertBytesToStr(encrypted,
|
|
181
|
-
const tagStr = $convertBytesToStr(tag,
|
|
194
|
+
const ivStr = $convertBytesToStr(iv, outputEncoding);
|
|
195
|
+
const cipherStr = $convertBytesToStr(encrypted, outputEncoding);
|
|
196
|
+
const tagStr = $convertBytesToStr(tag, outputEncoding);
|
|
182
197
|
if (ivStr.error || cipherStr.error || tagStr.error) {
|
|
183
198
|
return $err({
|
|
184
199
|
msg: "Crypto NodeJS API - Encryption: Failed to convert IV or encrypted data or tag",
|
|
@@ -190,17 +205,23 @@ function $encrypt(data, secretKey, options = {}) {
|
|
|
190
205
|
return $err({ msg: `${title("node", "Encryption")}: Failed to encrypt data`, desc: $fmtError(error2) });
|
|
191
206
|
}
|
|
192
207
|
}
|
|
193
|
-
function $decrypt(encrypted, secretKey, options
|
|
194
|
-
if (
|
|
208
|
+
function $decrypt(encrypted, secretKey, options) {
|
|
209
|
+
if (!matchEncryptedPattern(encrypted, "node")) {
|
|
195
210
|
return $err({
|
|
196
211
|
msg: `${title("node", "Decryption")}: Invalid encrypted data format`,
|
|
197
212
|
desc: 'Encrypted data must be in the format "iv.cipher.tag."'
|
|
198
213
|
});
|
|
199
214
|
}
|
|
200
|
-
|
|
201
|
-
if (!CIPHER_ENCODING.includes(encoding)) {
|
|
215
|
+
if (!$isPlainObj(options)) {
|
|
202
216
|
return $err({
|
|
203
|
-
msg: `${title("node", "Decryption")}:
|
|
217
|
+
msg: `${title("node", "Decryption")}: Invalid options`,
|
|
218
|
+
desc: "Options must be an object"
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
222
|
+
if (!CIPHER_ENCODING.includes(inputEncoding)) {
|
|
223
|
+
return $err({
|
|
224
|
+
msg: `${title("node", "Decryption")}: Unsupported input encoding: ${inputEncoding}`,
|
|
204
225
|
desc: "Use base64, base64url, or hex"
|
|
205
226
|
});
|
|
206
227
|
}
|
|
@@ -218,9 +239,9 @@ function $decrypt(encrypted, secretKey, options = {}) {
|
|
|
218
239
|
desc: "Expected a Node SecretKey"
|
|
219
240
|
});
|
|
220
241
|
}
|
|
221
|
-
const ivBytes = $convertStrToBytes(iv,
|
|
222
|
-
const cipherBytes = $convertStrToBytes(cipher,
|
|
223
|
-
const tagBytes = $convertStrToBytes(tag,
|
|
242
|
+
const ivBytes = $convertStrToBytes(iv, inputEncoding);
|
|
243
|
+
const cipherBytes = $convertStrToBytes(cipher, inputEncoding);
|
|
244
|
+
const tagBytes = $convertStrToBytes(tag, inputEncoding);
|
|
224
245
|
if (ivBytes.error || cipherBytes.error || tagBytes.error) {
|
|
225
246
|
return $err({
|
|
226
247
|
msg: `${title("node", "Decryption")}: Failed to convert IV or encrypted data or tag`,
|
|
@@ -236,12 +257,12 @@ function $decrypt(encrypted, secretKey, options = {}) {
|
|
|
236
257
|
return $err({ msg: `${title("node", "Decryption")}: Failed to decrypt data`, desc: $fmtError(error) });
|
|
237
258
|
}
|
|
238
259
|
}
|
|
239
|
-
function $encryptObj(data, secretKey, options
|
|
260
|
+
function $encryptObj(data, secretKey, options) {
|
|
240
261
|
const { result, error } = $stringifyObj(data);
|
|
241
262
|
if (error) return $err(error);
|
|
242
263
|
return $encrypt(result, secretKey, options);
|
|
243
264
|
}
|
|
244
|
-
function $decryptObj(encrypted, secretKey, options
|
|
265
|
+
function $decryptObj(encrypted, secretKey, options) {
|
|
245
266
|
const { result, error } = $decrypt(encrypted, secretKey, options);
|
|
246
267
|
if (error) return $err(error);
|
|
247
268
|
return $parseToObj(result);
|
|
@@ -253,10 +274,16 @@ function $hash(data, options = {}) {
|
|
|
253
274
|
desc: "Data must be a non-empty string"
|
|
254
275
|
});
|
|
255
276
|
}
|
|
256
|
-
|
|
257
|
-
if (!CIPHER_ENCODING.includes(encoding)) {
|
|
277
|
+
if (!$isPlainObj(options)) {
|
|
258
278
|
return $err({
|
|
259
|
-
msg: `${title("node", "Hashing")}:
|
|
279
|
+
msg: `${title("node", "Hashing")}: Invalid options`,
|
|
280
|
+
desc: "Options must be an object"
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
284
|
+
if (!CIPHER_ENCODING.includes(outputEncoding)) {
|
|
285
|
+
return $err({
|
|
286
|
+
msg: `${title("node", "Hashing")}: Unsupported output encoding: ${outputEncoding}`,
|
|
260
287
|
desc: "Use base64, base64url, or hex"
|
|
261
288
|
});
|
|
262
289
|
}
|
|
@@ -272,18 +299,24 @@ function $hash(data, options = {}) {
|
|
|
272
299
|
if (error) return $err(error);
|
|
273
300
|
try {
|
|
274
301
|
const hashed = nodeCrypto.createHash(digestAlgo.node).update(result).digest();
|
|
275
|
-
return $convertBytesToStr(hashed,
|
|
302
|
+
return $convertBytesToStr(hashed, outputEncoding);
|
|
276
303
|
} catch (error2) {
|
|
277
304
|
return $err({ msg: `${title("node", "Hashing")}: Failed to hash data with Crypto NodeJS`, desc: $fmtError(error2) });
|
|
278
305
|
}
|
|
279
306
|
}
|
|
280
|
-
function $hashPassword(password, options
|
|
307
|
+
function $hashPassword(password, options) {
|
|
281
308
|
if (!$isStr(password)) {
|
|
282
309
|
return $err({
|
|
283
310
|
msg: `${title("node", "Password Hashing")}: Empty password for hashing`,
|
|
284
311
|
desc: "Password must be a non-empty string"
|
|
285
312
|
});
|
|
286
313
|
}
|
|
314
|
+
if (!$isPlainObj(options)) {
|
|
315
|
+
return $err({
|
|
316
|
+
msg: `${title("node", "Password Hashing")}: Invalid options`,
|
|
317
|
+
desc: "Options must be an object"
|
|
318
|
+
});
|
|
319
|
+
}
|
|
287
320
|
const digest = options.digest ?? "sha512";
|
|
288
321
|
if (!(digest in DIGEST_ALGORITHMS)) {
|
|
289
322
|
return $err({
|
|
@@ -292,10 +325,10 @@ function $hashPassword(password, options = {}) {
|
|
|
292
325
|
});
|
|
293
326
|
}
|
|
294
327
|
const digestAlgo = DIGEST_ALGORITHMS[digest];
|
|
295
|
-
const
|
|
296
|
-
if (!CIPHER_ENCODING.includes(
|
|
328
|
+
const outputEncoding = options.outputEncoding ?? "base64url";
|
|
329
|
+
if (!CIPHER_ENCODING.includes(outputEncoding)) {
|
|
297
330
|
return $err({
|
|
298
|
-
msg: `${title("node", "Password Hashing")}: Unsupported encoding: ${
|
|
331
|
+
msg: `${title("node", "Password Hashing")}: Unsupported encoding: ${outputEncoding}`,
|
|
299
332
|
desc: "Use base64, base64url, or hex"
|
|
300
333
|
});
|
|
301
334
|
}
|
|
@@ -323,25 +356,27 @@ function $hashPassword(password, options = {}) {
|
|
|
323
356
|
try {
|
|
324
357
|
const salt = nodeCrypto.randomBytes(saltLength);
|
|
325
358
|
const hash2 = nodeCrypto.pbkdf2Sync(password.normalize("NFKC"), salt, iterations, keyLength, digestAlgo.node);
|
|
326
|
-
return $ok({
|
|
359
|
+
return $ok({ result: hash2.toString(outputEncoding), salt: salt.toString(outputEncoding) });
|
|
327
360
|
} catch (error) {
|
|
328
361
|
return $err({ msg: `${title("node", "Password Hashing")}: Failed to hash password`, desc: $fmtError(error) });
|
|
329
362
|
}
|
|
330
363
|
}
|
|
331
|
-
function $verifyPassword(password, hashedPassword, salt, options
|
|
332
|
-
if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt))
|
|
364
|
+
function $verifyPassword(password, hashedPassword, salt, options) {
|
|
365
|
+
if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt) || !$isPlainObj(options)) {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
333
368
|
const digest = options.digest ?? "sha512";
|
|
334
369
|
if (!(digest in DIGEST_ALGORITHMS)) return false;
|
|
335
370
|
const digestAlgo = DIGEST_ALGORITHMS[digest];
|
|
336
|
-
const
|
|
337
|
-
if (!CIPHER_ENCODING.includes(
|
|
371
|
+
const inputEncoding = options.inputEncoding ?? "base64url";
|
|
372
|
+
if (!CIPHER_ENCODING.includes(inputEncoding)) return false;
|
|
338
373
|
const iterations = options.iterations ?? 32e4;
|
|
339
374
|
if (typeof iterations !== "number" || iterations < 1e3) return false;
|
|
340
375
|
const keyLength = options.keyLength ?? 64;
|
|
341
376
|
if (typeof keyLength !== "number" || keyLength < 16) return false;
|
|
342
|
-
const saltBytes = $convertStrToBytes(salt,
|
|
377
|
+
const saltBytes = $convertStrToBytes(salt, inputEncoding);
|
|
343
378
|
if (saltBytes.error) return false;
|
|
344
|
-
const hashedPasswordBytes = $convertStrToBytes(hashedPassword,
|
|
379
|
+
const hashedPasswordBytes = $convertStrToBytes(hashedPassword, inputEncoding);
|
|
345
380
|
if (hashedPasswordBytes.error) return false;
|
|
346
381
|
try {
|
|
347
382
|
return nodeCrypto.timingSafeEqual(
|
|
@@ -417,9 +452,9 @@ function tryHashPassword(password, options = {}) {
|
|
|
417
452
|
return $hashPassword(password, options);
|
|
418
453
|
}
|
|
419
454
|
function hashPassword(password, options = {}) {
|
|
420
|
-
const {
|
|
455
|
+
const { result, salt, error } = $hashPassword(password, options);
|
|
421
456
|
if (error) throw new Error($fmtResultErr(error));
|
|
422
|
-
return {
|
|
457
|
+
return { result, salt };
|
|
423
458
|
}
|
|
424
459
|
function verifyPassword(password, hashedPassword, salt, options = {}) {
|
|
425
460
|
return $verifyPassword(password, hashedPassword, salt, options);
|
|
@@ -450,5 +485,5 @@ function convertEncoding(data, from, to) {
|
|
|
450
485
|
}
|
|
451
486
|
|
|
452
487
|
export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, isNodeSecretKey, kit_exports, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword };
|
|
453
|
-
//# sourceMappingURL=chunk-
|
|
454
|
-
//# sourceMappingURL=chunk-
|
|
488
|
+
//# sourceMappingURL=chunk-RAEBT46G.js.map
|
|
489
|
+
//# sourceMappingURL=chunk-RAEBT46G.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,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ACT","file":"chunk-RAEBT46G.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 { $isSecretKey } from \"~/helpers/validate\";\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 $hashPassword,\r\n $verifyPassword,\r\n} from \"./node-encrypt\";\r\n\r\n/**\r\n * Type guard to check if the provided value is a SecretKey object for Node.js environment.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Checking if the key in your hand is the right kind of key for the lock.\r\n *\r\n * @param x - The value to check.\r\n * @returns True if the value is a SecretKey object for Node.js, false otherwise.\r\n *\r\n * @example\r\n * ```ts\r\n * isNodeSecretKey(nodeKey); // true\r\n * isNodeSecretKey(webKey); // false\r\n * isNodeSecretKey({}); // false\r\n * ```\r\n */\r\nexport function isNodeSecretKey(x: unknown): x is SecretKey<\"node\"> {\r\n return $isSecretKey(x, \"node\") !== null;\r\n}\r\n\r\n/**\r\n * Safely generates a UUID (v4) (non-throwing).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It's like giving your pet a name tag with a super random name made of numbers and letters.\r\n * The chance of two pets getting the same name tag is practically zero, and it's very hard to guess!\r\n *\r\n * @returns A `Result` containing the UUID string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryGenerateUuid();\r\n *\r\n * if (success) console.log(result); // \"...\" (a UUID string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4) (throwing).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It's like giving your pet a name tag with a super random name made of numbers and letters.\r\n * The chance of two pets getting the same name tag is practically zero, and it's very hard to guess!\r\n *\r\n * @returns A UUID string.\r\n * @throws {Error} If UUID generation fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const uuid = generateUuid(); // \"...\" (a UUID string)\r\n * ```\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 * Safely derives a `SecretKey` from the provided string for encryption/decryption (non-throwing).\r\n *\r\n * Uses HKDF to derive a key from the input string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you want to create a special key for future use to lock your treasure box (data).\r\n * So, you stir in some secret ingredients (like salt and info) to make sure your key is one-of-a-kind.\r\n *\r\n * @param secret - The input string to derive the `SecretKey` from, must be at least 8 characters.\r\n * @param options.algorithm - The encryption algorithm to use (default: `'aes256gcm'`).\r\n * @param options.digest - The hash algorithm for HKDF (default: `'sha256'`).\r\n * @param options.salt - A salt string (default: `'cipher-kit-salt'`, must be ≥ 8 chars).\r\n * @param options.info - An info string (default: `'cipher-kit'`).\r\n * @returns A `Result` containing the derived `SecretKey` or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryCreateSecretKey(\"my-secret\");\r\n *\r\n * if (success) console.log(result); // SecretKey object\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 `SecretKey` from the provided string for encryption/decryption (throwing).\r\n *\r\n * Uses HKDF to derive a key from the input string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you want to create a special key for future use to lock your treasure box (data).\r\n * So, you stir in some secret ingredients (like salt and info) to make sure your key is one-of-a-kind.\r\n *\r\n * @param secret - The input string to derive the `SecretKey` from, must be at least 8 characters.\r\n * @param options.algorithm - The encryption algorithm to use (default: `'aes256gcm'`).\r\n * @param options.digest - The hash algorithm for HKDF (default: `'sha256'`).\r\n * @param options.salt - A salt string (default: `'cipher-kit-salt'`, must be ≥ 8 chars).\r\n * @param options.info - An info string (default: `'cipher-kit'`).\r\n * @returns The derived `SecretKey`.\r\n * @throws {Error} If key derivation fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\"); // SecretKey object\r\n * ```\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 * Safely encrypts a UTF-8 string using the provided `SecretKey` (non-throwing).\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You scramble a secret message with your special key,\r\n * creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @param data - A UTF-8 string to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns A `Result` containing the encrypted string in the specified format or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const {result, error, success} = tryEncrypt(\"Hello, World!\", secretKey);\r\n *\r\n * if (success) console.log(result); // \"iv.cipher.tag.\" (Encrypted string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 a UTF-8 string using the provided `SecretKey` (throwing).\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You scramble a secret message with your special key,\r\n * creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @param data - A UTF-8 string to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns The encrypted string in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encrypt(\"Hello, World!\", secretKey); // \"iv.cipher.tag.\" (Encrypted string)\r\n * ```\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 * Safely decrypts the input string using the provided `SecretKey` (non-throwing).\r\n *\r\n * Expects input in the format \"iv.cipher.tag.\" and returns the decrypted UTF-8 string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You take a scrambled secret message and use your special key to unscramble it,\r\n * revealing the original message inside.\r\n *\r\n * @param encrypted - The input string to decrypt, in the format \"iv.cipher.tag.\".\r\n * @param secretKey - The `SecretKey` object used for decryption.\r\n * @param options.inputEncoding - The encoding format for the input ciphertext (default: `'base64url'`).\r\n * @returns A `Result` containing the decrypted UTF-8 string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encrypt(\"Hello, World!\", secretKey);\r\n * const {result, error, success} = tryDecrypt(encrypted, secretKey);\r\n *\r\n * if (success) console.log(result); // \"Hello, World!\" (Decrypted string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 `SecretKey` (throwing).\r\n *\r\n * Expects input in the format \"iv.cipher.tag.\" and returns the decrypted UTF-8 string.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You take a scrambled secret message and use your special key to unscramble it,\r\n * revealing the original message inside.\r\n *\r\n * @param encrypted - The input string to decrypt, in the format \"iv.cipher.tag.\".\r\n * @param secretKey - The `SecretKey` object used for decryption.\r\n * @param options.inputEncoding - The encoding format for the input ciphertext (default: `'base64url'`).\r\n * @returns The decrypted UTF-8 string.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encrypt(\"Hello, World!\", secretKey);\r\n * const decrypted = decrypt(encrypted, secretKey); // \"Hello, World!\" (Decrypted string)\r\n * ```\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 * Safely encrypts a plain object using the provided `SecretKey` (non-throwing).\r\n *\r\n * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you have a toy box (an object) that you want to keep secret.\r\n * So, you take a picture of your toy box (convert it to JSON), and scramble that with\r\n * your special key, creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @template T - The type of the plain object to encrypt.\r\n * @param data - A plain object to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns A `Result` containing the encrypted string in the specified format or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const {result, error, success} = tryEncryptObj({ a: 1 }, secretKey);\r\n *\r\n * if (success) console.log(result); // \"iv.cipher.tag.\" (Encrypted string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 a plain object using the provided `SecretKey` (throwing).\r\n *\r\n * Only plain objects (POJOs) are accepted. Class instances, Maps, Sets, etc. are rejected.\r\n *\r\n * Output format: \"iv.cipher.tag.\"\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Imagine you have a toy box (an object) that you want to keep secret.\r\n * So, you take a picture of your toy box (convert it to JSON), and scramble that with\r\n * your special key, creating a jumbled code that only someone with the right key can read.\r\n *\r\n * @template T - The type of the plain object to encrypt.\r\n * @param data - A plain object to encrypt.\r\n * @param secretKey - The `SecretKey` object used for encryption.\r\n * @param options.outputEncoding - The encoding format for the output ciphertext (default: `'base64url'`).\r\n * @returns The encrypted string in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encryptObj({ a: 1 }, secretKey); // \"iv.cipher.tag.\" (Encrypted string)\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): 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 * Safely decrypts an encrypted JSON string into an object (non-throwing).\r\n *\r\n * Expects input in the format `\"iv.cipher.tag.\"` and returns a plain object.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You rebuild your toy box (an object) by unscrambling the jumbled code (encrypted text),\r\n * using your special key to open it.\r\n *\r\n * @template T - The expected shape of the decrypted object.\r\n * @param encrypted - The encrypted string (format: `\"iv.cipher.tag.\"`).\r\n * @param secretKey - The `SecretKey` used for decryption.\r\n * @param options.inputEncoding - Input ciphertext encoding (default: `'base64url'`).\r\n * @returns A `Result` with the decrypted object on success, or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encryptObj({ a: 1 }, secretKey);\r\n * const {result, error, success} = tryDecryptObj<{ a: number }>(encrypted, secretKey);\r\n *\r\n * if (success) console.log(result); // { a: 1 } (Decrypted object)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 an encrypted JSON string into an object (throwing).\r\n *\r\n * Expects input in the format `\"iv.cipher.tag.\"` and returns a plain object.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * You rebuild your toy box (an object) by unscrambling the jumbled code (encrypted text),\r\n * using your special key to open it.\r\n *\r\n * @template T - The expected shape of the decrypted object.\r\n * @param encrypted - The encrypted string (format: `\"iv.cipher.tag.\"`).\r\n * @param secretKey - The `SecretKey` used for decryption.\r\n * @param options.inputEncoding - Input ciphertext encoding (default: `'base64url'`).\r\n * @returns The decrypted object.\r\n * @throws {Error} If decryption or JSON parsing fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const secretKey = createSecretKey(\"my-secret\");\r\n * const encrypted = encryptObj({ a: 1 }, secretKey);\r\n * const obj = decryptObj<{ a: number }>(encrypted, secretKey); // obj.a === 1\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): 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 * Safely hashes a UTF-8 string (non-throwing).\r\n *\r\n * Uses the selected digest (default: `'sha256'`) and returns the hash\r\n * in the chosen encoding (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Like putting something in a blender and getting a smoothie, you can’t get the original ingredients back,\r\n * but the smoothie is always the same if you use the same ingredients.\r\n *\r\n * @param data - The input string to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`, default: `'sha256'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`, default: `'base64url'`).\r\n * @returns A `Result` with the hash string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryHash(\"my data\");\r\n *\r\n * if (success) console.log(result); // \"...\" (Hashed string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 a UTF-8 string (throwing).\r\n *\r\n * Uses the selected digest (default: `'sha256'`) and returns the hash\r\n * in the chosen encoding (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * Like putting something in a blender and getting a smoothie, you can’t get the original ingredients back,\r\n * but the smoothie is always the same if you use the same ingredients.\r\n *\r\n * @param data - The input string to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha256'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @returns The hashed string.\r\n * @throws {Error} If input is invalid or hashing fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const hash = hash(\"my data\"); // \"...\" (Hashed string)\r\n * ```\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 * Safely hashes a password using PBKDF2 (non-throwing).\r\n *\r\n * Uses strong defaults (`sha512`, 320k iterations, 64-byte key, 16-byte salt) and\r\n * returns `{ hash, salt }` encoded (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * We take your password, mix in some random salt, and stir many times.\r\n * The result is a super-secret soup that’s hard to copy.\r\n *\r\n * @param password - The password to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @param options.saltLength - Length of the random salt in bytes (default: `16` bytes, min: `8` bytes).\r\n * @param options.iterations - Number of iterations (default: `320000`, min: `1000`).\r\n * @param options.keyLength - Length of the derived key in bytes (default: `64` bytes, min: `16` bytes).\r\n * @returns A `Result` with `{ result, salt }` or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, salt, error, success} = tryHashPassword(\"my-password\");\r\n *\r\n * if (success) console.log(result, salt); // \"...\" (Hashed password string), \"...\" (salt)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\r\n */\r\nexport function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ result: string; salt: string }> {\r\n return $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 (throwing).\r\n *\r\n * Uses strong defaults (`sha512`, 320k iterations, 64-byte key, 16-byte salt) and\r\n * returns `{ hash, salt }` encoded (default: `'base64url'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * We take your password, mix in some random salt, and stir many times.\r\n * The result is a super-secret soup that’s hard to copy.\r\n *\r\n * @param password - The password to hash.\r\n * @param options.digest - Hash algorithm (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).\r\n * @param options.outputEncoding - Output encoding (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @param options.saltLength - Length of the random salt in bytes (default: `16` bytes, min: `8` bytes).\r\n * @param options.iterations - Number of iterations (default: `320000`, min: `1000`).\r\n * @param options.keyLength - Length of the derived key in bytes (default: `64` bytes, min: `16` bytes).\r\n * @returns `{ result, salt }` for storage.\r\n * @throws {Error} If inputs are invalid or hashing fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const { result, salt } = hashPassword(\"my-password\"); // \"...\" (Hashed password string), \"...\" (salt)\r\n * ```\r\n */\r\nexport function hashPassword(password: string, options: HashPasswordOptions = {}): { result: string; salt: string } {\r\n const { result, salt, error } = $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { result, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a stored PBKDF2 hash (non-throwing).\r\n *\r\n * Re-derives the key using the same parameters and compares in constant time, to prevent timing attacks.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * We follow the same recipe as when we made the secret.\r\n * If the new soup tastes exactly the same, the password is correct.\r\n *\r\n * @param password - The plain password to verify.\r\n * @param hashedPassword - The stored hash (encoded).\r\n * @param salt - The stored salt (encoded).\r\n * @param options.digest - Hash algorithm used during hashing (`'sha256' | 'sha384' | 'sha512'`; default: `'sha512'`).\r\n * @param options.inputEncoding - Encoding of the stored hash and salt (`'base64' | 'base64url' | 'hex'`; default: `'base64url'`).\r\n * @param options.iterations - Number of iterations used during hashing (default: `320000`).\r\n * @param options.keyLength - Length of the derived key in bytes used during hashing (default: `64` bytes).\r\n * @returns `true` if the password matches, otherwise `false`.\r\n *\r\n * @example\r\n * ```ts\r\n * const { result, salt } = hashPassword(\"my-password\");\r\n *\r\n * verifyPassword(\"my-password\", result, salt); // true\r\n * verifyPassword(\"wrong-password\", result, salt); // false\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 return $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Safely converts a string to a Node.js `Buffer` using the specified encoding (non-throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns your words into tiny computer building blocks (bytes) so computers can work with them.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding of the input string (default: `'utf8'`).\r\n * @returns A `Result` with `{ result: Buffer }` or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryConvertStrToBytes(\"Hello\", \"utf8\");\r\n *\r\n * if (success) console.log(result); // <Buffer ...>\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 Node.js `Buffer` using the specified encoding (throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns your words into tiny computer building blocks (bytes) so computers can work with them.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding of the input string (default: `'utf8'`).\r\n * @returns A `Buffer` containing the bytes.\r\n * @throws {Error} If input is invalid or conversion fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const bytes = convertStrToBytes(\"Hello\", \"utf8\"); // <Buffer ...>\r\n * ```\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 * Safely converts a Node.js `Buffer` to a string using the specified encoding (non-throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns the tiny computer building blocks (bytes) back into a readable sentence.\r\n *\r\n * @param data - The `Buffer` to convert.\r\n * @param outputEncoding - The output encoding (default: `'utf8'`).\r\n * @returns A `Result` with the string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const bytes = convertStrToBytes(\"Hello\", \"utf8\"); // <Buffer ...>\r\n * const {result, error, success} = tryConvertBytesToStr(bytes, \"utf8\");\r\n *\r\n * if (success) console.log(result); // \"Hello\"\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\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 Node.js `Buffer` to a string using the specified encoding (throwing).\r\n *\r\n * Supported encodings: `'base64' | 'base64url' | 'hex' | 'utf8' | 'latin1'` (default: `'utf8'`).\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * This turns the tiny computer building blocks (bytes) back into a readable sentence.\r\n *\r\n * @param data - The `Buffer` to convert.\r\n * @param outputEncoding - The output encoding (default: `'utf8'`).\r\n * @returns The encoded string.\r\n * @throws {Error} If input is invalid or conversion fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const bytes = convertStrToBytes(\"Hello\", \"utf8\"); // <Buffer ...>\r\n * const str = convertBytesToStr(bytes, \"utf8\"); // \"Hello\"\r\n * ```\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 * Safely converts text between encodings (non-throwing).\r\n *\r\n * Example: convert `'utf8'` text to `'base64url'`, or `'hex'` to `'utf8'`.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It’s like translating your sentence from one alphabet to another.\r\n *\r\n * @param data - The input string to convert.\r\n * @param from - The current encoding of `data`.\r\n * @param to - The target encoding for `data`.\r\n * @returns A `Result` with a string or an error.\r\n *\r\n * @example\r\n * ```ts\r\n * const {result, error, success} = tryConvertEncoding(\"Hello\", \"utf8\", \"base64url\");\r\n *\r\n * if (success) console.log(result); // \"...\" (Base64url encoded string)\r\n * else console.error(error); // { message: \"...\", description: \"...\" }\r\n * ```\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<string> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Converts text between encodings (throwing).\r\n *\r\n * Example: convert `'utf8'` text to `'base64url'`, or `'hex'` to `'utf8'`.\r\n *\r\n * ### 🍼 Explain Like I'm Five\r\n * It’s like translating your sentence from one alphabet to another.\r\n *\r\n * @param data - The input string to convert.\r\n * @param from - The current encoding of `data`.\r\n * @param to - The target encoding for `data`.\r\n * @returns The converted string.\r\n * @throws {Error} If encodings are invalid or conversion fails.\r\n *\r\n * @example\r\n * ```ts\r\n * const encoded = convertEncoding(\"Hello\", \"utf8\", \"base64url\"); // \"...\" (Base64url encoded string)\r\n * ```\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 { ENCODING } 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 (!ENCODING.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 (!ENCODING.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<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.includes(from) || !ENCODING.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 { CIPHER_ENCODING, DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from \"~/helpers/consts\";\r\nimport { $err, $fmtError, $fmtResultErr, $ok, type Result, title } 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 { $isPlainObj, $isSecretKey, $isStr, matchEncryptedPattern } 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: `${title(\"node\", \"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, 8)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Key Generation\")}: Empty Secret`,\r\n desc: \"Secret must be a non-empty string with at least 8 characters\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<CreateSecretKeyOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Key Generation\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"Encryption\")}: Empty data for encryption`,\r\n desc: \"Data must be a non-empty string\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<EncryptOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Encryption\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\r\n }\r\n\r\n const outputEncoding = options.outputEncoding ?? \"base64url\";\r\n if (!CIPHER_ENCODING.includes(outputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Encryption\")}: Unsupported output encoding: ${outputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\r\n });\r\n }\r\n\r\n const injectedKey = $isSecretKey(secretKey, \"node\");\r\n if (!injectedKey) {\r\n return $err({\r\n msg: `${title(\"node\", \"Encryption\")}: Invalid Secret Key`,\r\n desc: \"Expected a Node SecretKey\",\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, \"utf8\");\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: ${$fmtResultErr(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: `${title(\"node\", \"Encryption\")}: Failed to encrypt data`, desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $decrypt(encrypted: string, secretKey: SecretKey<\"node\">, options?: DecryptOptions): Result<string> {\r\n if (!matchEncryptedPattern(encrypted, \"node\")) {\r\n return $err({\r\n msg: `${title(\"node\", \"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 if (!$isPlainObj<DecryptOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Decryption\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? \"base64url\";\r\n if (!CIPHER_ENCODING.includes(inputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Decryption\")}: Unsupported input encoding: ${inputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\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: `${title(\"node\", \"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: `${title(\"node\", \"Decryption\")}: Failed to convert IV or encrypted data or tag`,\r\n desc: `Conversion error: ${$fmtResultErr(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, \"utf8\");\r\n } catch (error) {\r\n return $err({ msg: `${title(\"node\", \"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: `${title(\"node\", \"Hashing\")}: Empty data for hashing`,\r\n desc: \"Data must be a non-empty string\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<HashOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Hashing\")}: Invalid options`,\r\n desc: \"Options must be an object\",\r\n });\r\n }\r\n\r\n const outputEncoding = options.outputEncoding ?? \"base64url\";\r\n if (!CIPHER_ENCODING.includes(outputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Hashing\")}: Unsupported output encoding: ${outputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\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: `${title(\"node\", \"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, \"utf8\");\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: `${title(\"node\", \"Hashing\")}: Failed to hash data with Crypto NodeJS`, desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $hashPassword(\r\n password: string,\r\n options?: HashPasswordOptions,\r\n): Result<{ result: string; salt: string }> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Password Hashing\")}: Empty password for hashing`,\r\n desc: \"Password must be a non-empty string\",\r\n });\r\n }\r\n\r\n if (!$isPlainObj<HashPasswordOptions>(options)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Password Hashing\")}: Invalid options`,\r\n desc: \"Options must be an object\",\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: `${title(\"node\", \"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 (!CIPHER_ENCODING.includes(outputEncoding)) {\r\n return $err({\r\n msg: `${title(\"node\", \"Password Hashing\")}: Unsupported encoding: ${outputEncoding}`,\r\n desc: \"Use base64, base64url, or hex\",\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: `${title(\"node\", \"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: `${title(\"node\", \"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: `${title(\"node\", \"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({ result: hash.toString(outputEncoding), salt: salt.toString(outputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: `${title(\"node\", \"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) || !$isPlainObj<VerifyPasswordOptions>(options)) {\r\n return false;\r\n }\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 (!CIPHER_ENCODING.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"]}
|