cipher-kit 2.0.0-beta.2 → 2.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-AEQNI5EZ.js → chunk-6GBH7YTP.js} +27 -10
- package/dist/chunk-6GBH7YTP.js.map +1 -0
- package/dist/{chunk-SUGN4VDZ.js → chunk-BNQERV4S.js} +33 -12
- package/dist/chunk-BNQERV4S.js.map +1 -0
- package/dist/{chunk-WIZT7AYM.cjs → chunk-NKLNWTQA.cjs} +134 -111
- package/dist/chunk-NKLNWTQA.cjs.map +1 -0
- package/dist/{chunk-3XGARINH.cjs → chunk-UHP3PPXP.cjs} +125 -106
- package/dist/chunk-UHP3PPXP.cjs.map +1 -0
- package/dist/{chunk-LOJKJJX5.js → chunk-YPYDYYV2.js} +27 -32
- package/dist/chunk-YPYDYYV2.js.map +1 -0
- package/dist/{chunk-RXXVBCWL.cjs → chunk-ZJ32WGAA.cjs} +27 -31
- package/dist/chunk-ZJ32WGAA.cjs.map +1 -0
- package/dist/{export-Bq9tslUU.d.ts → export--ndIQ3j3.d.cts} +22 -3
- package/dist/{export-C0WDJZUy.d.ts → export-C2M5UPLX.d.cts} +22 -3
- package/dist/{export-RD2Af4CJ.d.cts → export-CPUbAFZA.d.ts} +22 -3
- package/dist/{export-LPOfeH2z.d.cts → export-v9ULdDL0.d.ts} +22 -3
- package/dist/index.cjs +12 -12
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/node.cjs +40 -32
- package/dist/node.d.cts +2 -2
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/{validate-CS4PFmY1.d.cts → validate-CULVlPck.d.cts} +9 -11
- package/dist/{validate-CS4PFmY1.d.ts → validate-CULVlPck.d.ts} +9 -11
- package/dist/web-api.cjs +40 -32
- package/dist/web-api.d.cts +2 -2
- package/dist/web-api.d.ts +2 -2
- package/dist/web-api.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-3XGARINH.cjs.map +0 -1
- package/dist/chunk-AEQNI5EZ.js.map +0 -1
- package/dist/chunk-LOJKJJX5.js.map +0 -1
- package/dist/chunk-RXXVBCWL.cjs.map +0 -1
- package/dist/chunk-SUGN4VDZ.js.map +0 -1
- package/dist/chunk-WIZT7AYM.cjs.map +0 -1
package/dist/web-api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashPassword, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashPassword, verifyPassword } from './chunk-
|
|
2
|
-
export { ENCRYPTED_REGEX, isSecretKey, matchPattern, parseToObj, stringifyObj, tryParseToObj, tryStringifyObj } from './chunk-
|
|
1
|
+
export { convertBytesToStr, convertEncoding, convertStrToBytes, createSecretKey, decrypt, decryptObj, encrypt, encryptObj, generateUuid, hash, hashObj, hashPassword, tryConvertBytesToStr, tryConvertEncoding, tryConvertStrToBytes, tryCreateSecretKey, tryDecrypt, tryDecryptObj, tryEncrypt, tryEncryptObj, tryGenerateUuid, tryHash, tryHashObj, tryHashPassword, verifyPassword } from './chunk-BNQERV4S.js';
|
|
2
|
+
export { ENCRYPTED_REGEX, isSecretKey, matchPattern, parseToObj, stringifyObj, tryParseToObj, tryStringifyObj } from './chunk-YPYDYYV2.js';
|
|
3
3
|
//# sourceMappingURL=web-api.js.map
|
|
4
4
|
//# sourceMappingURL=web-api.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cipher-kit",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.3",
|
|
4
4
|
"description": "🔐 Secure, Lightweight, and Cross-Platform Encryption, Decryption, and Hashing for Web, Node.js, Deno, Bun, and Cloudflare Workers",
|
|
5
5
|
"homepage": "https://github.com/WolfieLeader/npm/tree/main/packages/cipher-kit#readme",
|
|
6
6
|
"repository": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/node/kit.ts","../src/node/node-encode.ts","../src/node/node-encrypt.ts"],"names":["__export","$isStr","$err","ENCODINGS","$ok","Buffer","$fmtError","nodeCrypto","ENCRYPTION_ALGORITHMS","DIGEST_ALGORITHMS","isSecretKey","error","matchPattern","$stringifyObj","$parseToObj","hash","$fmtResultErr"],"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,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,e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AAI,OAAO,MAAM,IAAI,KAAA,CAAMC,+BAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAUO,SAAS,oBAAA,CAAqB,IAAA,EAAc,cAAA,GAA2B,MAAA,EAAwB;AACpG,EAAA,OAAO,kBAAA,CAAmB,MAAM,cAAc,CAAA;AAChD;AAWO,SAAS,iBAAA,CAAkB,IAAA,EAAc,cAAA,GAA2B,MAAA,EAAgB;AACzF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,kBAAA,CAAmB,MAAM,cAAc,CAAA;AACjE,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAMA,+BAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAUO,SAAS,kBAAA,CAAmB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAA0C;AACzG,EAAA,OAAO,gBAAA,CAAiB,IAAA,EAAM,IAAA,EAAM,EAAE,CAAA;AACxC;AAWO,SAAS,eAAA,CAAgB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAAsB;AAClF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,KAAU,gBAAA,CAAiB,IAAA,EAAM,MAAM,EAAE,CAAA;AACzD,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAMA,+BAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT","file":"chunk-3XGARINH.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 { $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 * Generates a UUID (v4).\r\n *\r\n * @returns A Result containing a string representing the generated UUID or an error.\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A string representing the generated UUID.\r\n * @throws {Error} If UUID generation fails.\r\n */\r\nexport function generateUuid(): string {\r\n const { result, error } = $generateUuid();\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A Result containing a SecretKey object representing the derived secret key or an error.\r\n */\r\nexport function tryCreateSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Result<{ result: SecretKey<'node'> }> {\r\n return $createSecretKey(secret, options);\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A SecretKey object representing the derived secret key.\r\n * @throws {Error} If the input key is invalid or key generation fails.\r\n */\r\nexport function createSecretKey(secret: string, options: CreateSecretKeyOptions = {}): SecretKey<'node'> {\r\n const { result, error } = $createSecretKey(secret, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted data in the specified format or an error.\r\n */\r\nexport function tryEncrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): Result<string> {\r\n return $encrypt(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted data in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport function encrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): string {\r\n const { result, error } = $encrypt(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing a string representing the decrypted data or an error.\r\n */\r\nexport function tryDecrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<string> {\r\n return $decrypt(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A string representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport function decrypt(encrypted: string, secretKey: SecretKey<'node'>, options: DecryptOptions = {}): string {\r\n const { result, error } = $decrypt(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted object in the specified format or an error.\r\n */\r\nexport function tryEncryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): Result<string> {\r\n return $encryptObj(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted object in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport function encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): string {\r\n const { result, error } = $encryptObj(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing an object representing the decrypted data or an error.\r\n */\r\nexport function tryDecryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<{ result: T }> {\r\n return $decryptObj<T>(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns An object representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport function decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): T {\r\n const { result, error } = $decryptObj<T>(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash in base64url format or an error.\r\n */\r\nexport function tryHash(data: string, options: HashOptions = {}): Result<string> {\r\n return $hash(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A string representing the SHA-256 hash in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport function hash(data: string, options: HashOptions = {}): string {\r\n const { result, error } = $hash(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns A Result containing an object with the hash and salt, or an error.\r\n */\r\nexport function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ hash: string; salt: string }> {\r\n return $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns An object with the hash and salt.\r\n * @throws {Error} If the input password is invalid or hashing fails.\r\n */\r\nexport function hashPassword(password: string, options: HashPasswordOptions = {}): { hash: string; salt: string } {\r\n const { hash, salt, error } = $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { hash, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a hashed password and salt.\r\n *\r\n * @param password - The password to verify.\r\n * @param hashedPassword - The hashed password to compare against (in base64url format).\r\n * @param salt - The salt used during hashing (in base64url format).\r\n * @returns A boolean indicating whether the password matches the hashed password.\r\n */\r\nexport function verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): boolean {\r\n return $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing a Node.js Buffer with the encoded data or an error.\r\n */\r\nexport function tryConvertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Result<{ result: Buffer }> {\r\n return $convertStrToBytes(data, inputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Node.js Buffer containing the encoded data.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Buffer {\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Converts a Buffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Buffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing the string representation of the Buffer or an error.\r\n */\r\nexport function tryConvertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n return $convertBytesToStr(data, outputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a Buffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Buffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A string representation of the Buffer in the specified format.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, outputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A Result containing the converted string or an error.\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A converted string.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertEncoding(data: string, from: Encoding, to: Encoding): string {\r\n const { result, error } = $convertEncoding(data, from, to);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport { ENCODINGS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { Encoding } from '~/helpers/types';\r\nimport { $isStr } from '~/helpers/validate';\r\n\r\nexport function $convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Result<{ result: Buffer }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - String to Bytes: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(inputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - String to Bytes: Unsupported encoding: ${inputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n return $ok({ result: Buffer.from(data, inputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - String to Bytes: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n if (!(data instanceof Buffer)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Bytes to String: Invalid data type',\r\n desc: 'Data must be a Buffer',\r\n });\r\n }\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Bytes to String: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n return $ok(Buffer.from(data).toString(outputEncoding));\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Bytes to String: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Convert Format: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n if (!ENCODINGS.includes(from) || !ENCODINGS.includes(to)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Convert Format: Unsupported encoding: from ${from} to ${to}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const bytes = $convertStrToBytes(data, from);\r\n if (bytes.error) return $err({ msg: bytes.error.message, desc: bytes.error.description });\r\n\r\n const str = $convertBytesToStr(bytes.result, to);\r\n if (str.error) return $err({ msg: str.error.message, desc: str.error.description });\r\n\r\n return $ok({ result: str.result });\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport nodeCrypto from 'node:crypto';\r\nimport { DIGEST_ALGORITHMS, ENCODINGS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $isStr, isSecretKey, matchPattern } from '~/helpers/validate';\r\nimport { $convertBytesToStr, $convertStrToBytes } from './node-encode';\r\n\r\nexport function $generateUuid(): Result<string> {\r\n try {\r\n return $ok(nodeCrypto.randomUUID());\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - UUID Generation: Failed to generate UUID', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $createSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Result<{ result: SecretKey<'node'> }> {\r\n if (!$isStr(secret)) {\r\n return $err({ msg: 'Crypto NodeJS API - Key Generation: Empty Secret', desc: 'Secret must be a non-empty string' });\r\n }\r\n\r\n const algorithm = options.algorithm ?? 'aes256gcm';\r\n if (!(algorithm in ENCRYPTION_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,\r\n desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const salt = options.salt ?? 'cipher-kit-salt';\r\n if (!$isStr(salt, 8)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Weak salt',\r\n desc: 'Salt must be a non-empty string with at least 8 characters',\r\n });\r\n }\r\n\r\n const info = options.info ?? 'cipher-kit';\r\n if (!$isStr(info)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Invalid info',\r\n desc: 'Info must be a non-empty string',\r\n });\r\n }\r\n\r\n const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n try {\r\n const derivedKey = nodeCrypto.hkdfSync(\r\n digestAlgo.node,\r\n secret.normalize('NFKC'),\r\n salt.normalize('NFKC'),\r\n info.normalize('NFKC'),\r\n encryptAlgo.keyBytes,\r\n );\r\n const key = nodeCrypto.createSecretKey(Buffer.from(derivedKey));\r\n const secretKey = Object.freeze({\r\n platform: 'node',\r\n digest: digest,\r\n algo: encryptAlgo,\r\n key: key,\r\n }) as SecretKey<'node'>;\r\n\r\n return $ok({ result: secretKey });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Key Generation: Failed to create secret key', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $encrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Empty data for encryption',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Encryption: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n if (!isSecretKey(secretKey, 'node')) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Invalid Secret Key',\r\n desc: 'Expected a Node SecretKey',\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const iv = nodeCrypto.randomBytes(secretKey.algo.ivLength);\r\n const cipher = nodeCrypto.createCipheriv(secretKey.algo.node, secretKey.key, iv);\r\n const encrypted = Buffer.concat([cipher.update(result), cipher.final()]);\r\n const tag = cipher.getAuthTag();\r\n\r\n const ivStr = $convertBytesToStr(iv, outputEncoding);\r\n const cipherStr = $convertBytesToStr(encrypted, outputEncoding);\r\n const tagStr = $convertBytesToStr(tag, outputEncoding);\r\n\r\n if (ivStr.error || cipherStr.error || tagStr.error) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Failed to convert IV or encrypted data or tag',\r\n desc: `Conversion error: ${ivStr.error || cipherStr.error || tagStr.error}`,\r\n });\r\n }\r\n\r\n return $ok(`${ivStr.result}.${cipherStr.result}.${tagStr.result}.`);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Encryption: Failed to encrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<string> {\r\n if (matchPattern(encrypted, 'node') === false) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid encrypted data format',\r\n desc: 'Encrypted data must be in the format \"iv.cipher.tag.\"',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n const outputEncoding = options.outputEncoding ?? 'utf8';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Decryption: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const [iv, cipher, tag] = encrypted.split('.', 4);\r\n if (!$isStr(iv) || !$isStr(cipher) || !$isStr(tag)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid encrypted data',\r\n desc: 'Encrypted data must contain valid IV, encrypted data, and tag components',\r\n });\r\n }\r\n\r\n if (!isSecretKey(secretKey, 'node')) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid Secret Key',\r\n desc: 'Expected a Node SecretKey',\r\n });\r\n }\r\n\r\n const ivBytes = $convertStrToBytes(iv, inputEncoding);\r\n const cipherBytes = $convertStrToBytes(cipher, inputEncoding);\r\n const tagBytes = $convertStrToBytes(tag, inputEncoding);\r\n\r\n if (ivBytes.error || cipherBytes.error || tagBytes.error) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Failed to convert IV or encrypted data or tag',\r\n desc: `Conversion error: ${ivBytes.error || cipherBytes.error || tagBytes.error}`,\r\n });\r\n }\r\n\r\n try {\r\n const decipher = nodeCrypto.createDecipheriv(secretKey.algo.node, secretKey.key, ivBytes.result);\r\n decipher.setAuthTag(tagBytes.result);\r\n const decrypted = Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);\r\n\r\n return $convertBytesToStr(decrypted, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Decryption: Failed to decrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\nexport function $encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): Result<string> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return $encrypt(result, secretKey, options);\r\n}\r\n\r\nexport function $decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<{ result: T }> {\r\n const { result, error } = $decrypt(encrypted, secretKey, options);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport function $hash(data: string, options: HashOptions = {}): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Hashing: Empty data for hashing',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Hashing: Unsupported encoding: ${inputEncoding} or ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const hashed = nodeCrypto.createHash(digestAlgo.node).update(result).digest();\r\n return $convertBytesToStr(hashed, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Hashing: Failed to hash data with Crypto NodeJS', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ hash: string; salt: string }> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Empty password for hashing',\r\n desc: 'Password must be a non-empty string',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Password Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Password Hashing: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const saltLength = options.saltLength ?? 16;\r\n if (typeof saltLength !== 'number' || saltLength < 8) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Weak salt length',\r\n desc: 'Salt length must be a number and at least 8 bytes (recommended 16 or more)',\r\n });\r\n }\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Weak iterations count',\r\n desc: 'Iterations must be a number and at least 1000 (recommended 320,000 or more)',\r\n });\r\n }\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Invalid key length',\r\n desc: 'Key length must be a number and at least 16 bytes (recommended 64 or more)',\r\n });\r\n }\r\n\r\n try {\r\n const salt = nodeCrypto.randomBytes(saltLength);\r\n const hash = nodeCrypto.pbkdf2Sync(password.normalize('NFKC'), salt, iterations, keyLength, digestAlgo.node);\r\n\r\n return $ok({ salt: salt.toString(outputEncoding), hash: hash.toString(outputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Password Hashing: Failed to hash password', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): boolean {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) return false;\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) return false;\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding)) return false;\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) return false;\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) return false;\r\n\r\n const saltBytes = $convertStrToBytes(salt, inputEncoding);\r\n if (saltBytes.error) return false;\r\n\r\n const hashedPasswordBytes = $convertStrToBytes(hashedPassword, inputEncoding);\r\n if (hashedPasswordBytes.error) return false;\r\n\r\n try {\r\n return nodeCrypto.timingSafeEqual(\r\n nodeCrypto.pbkdf2Sync(password.normalize('NFKC'), saltBytes.result, iterations, keyLength, digestAlgo.node),\r\n hashedPasswordBytes.result,\r\n );\r\n } catch {\r\n return false;\r\n }\r\n}\r\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/node/kit.ts","../src/node/node-encode.ts","../src/node/node-encrypt.ts"],"names":["Buffer","error","hash"],"mappings":";;;;;AAAA,IAAA,WAAA,GAAA;AAAA,QAAA,CAAA,WAAA,EAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,iBAAA,EAAA,MAAA,iBAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,IAAA,EAAA,MAAA,IAAA;AAAA,EAAA,YAAA,EAAA,MAAA,YAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,oBAAA,EAAA,MAAA,oBAAA;AAAA,EAAA,kBAAA,EAAA,MAAA,kBAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,UAAA,EAAA,MAAA,UAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,eAAA,EAAA,MAAA,eAAA;AAAA,EAAA,OAAA,EAAA,MAAA,OAAA;AAAA,EAAA,eAAA,EAA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cAAA,GAA2B,MAAA,EAAgB;AACzF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,kBAAA,CAAmB,MAAM,cAAc,CAAA;AACjE,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAUO,SAAS,kBAAA,CAAmB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAA0C;AACzG,EAAA,OAAO,gBAAA,CAAiB,IAAA,EAAM,IAAA,EAAM,EAAE,CAAA;AACxC;AAWO,SAAS,eAAA,CAAgB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAAsB;AAClF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,KAAU,gBAAA,CAAiB,IAAA,EAAM,MAAM,EAAE,CAAA;AACzD,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT","file":"chunk-AEQNI5EZ.js","sourcesContent":["import type { Buffer } from 'node:buffer';\r\nimport { $fmtResultErr, type Result } from '~/helpers/error';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n Encoding,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $convertBytesToStr, $convertEncoding, $convertStrToBytes } from './node-encode';\r\nimport {\r\n $createSecretKey,\r\n $decrypt,\r\n $decryptObj,\r\n $encrypt,\r\n $encryptObj,\r\n $generateUuid,\r\n $hash,\r\n $hashPassword,\r\n $verifyPassword,\r\n} from './node-encrypt';\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A Result containing a string representing the generated UUID or an error.\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A string representing the generated UUID.\r\n * @throws {Error} If UUID generation fails.\r\n */\r\nexport function generateUuid(): string {\r\n const { result, error } = $generateUuid();\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A Result containing a SecretKey object representing the derived secret key or an error.\r\n */\r\nexport function tryCreateSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Result<{ result: SecretKey<'node'> }> {\r\n return $createSecretKey(secret, options);\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A SecretKey object representing the derived secret key.\r\n * @throws {Error} If the input key is invalid or key generation fails.\r\n */\r\nexport function createSecretKey(secret: string, options: CreateSecretKeyOptions = {}): SecretKey<'node'> {\r\n const { result, error } = $createSecretKey(secret, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted data in the specified format or an error.\r\n */\r\nexport function tryEncrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): Result<string> {\r\n return $encrypt(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted data in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport function encrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): string {\r\n const { result, error } = $encrypt(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing a string representing the decrypted data or an error.\r\n */\r\nexport function tryDecrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<string> {\r\n return $decrypt(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A string representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport function decrypt(encrypted: string, secretKey: SecretKey<'node'>, options: DecryptOptions = {}): string {\r\n const { result, error } = $decrypt(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted object in the specified format or an error.\r\n */\r\nexport function tryEncryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): Result<string> {\r\n return $encryptObj(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted object in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport function encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): string {\r\n const { result, error } = $encryptObj(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing an object representing the decrypted data or an error.\r\n */\r\nexport function tryDecryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<{ result: T }> {\r\n return $decryptObj<T>(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipher.tag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns An object representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport function decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): T {\r\n const { result, error } = $decryptObj<T>(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash in base64url format or an error.\r\n */\r\nexport function tryHash(data: string, options: HashOptions = {}): Result<string> {\r\n return $hash(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A string representing the SHA-256 hash in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport function hash(data: string, options: HashOptions = {}): string {\r\n const { result, error } = $hash(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns A Result containing an object with the hash and salt, or an error.\r\n */\r\nexport function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ hash: string; salt: string }> {\r\n return $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns An object with the hash and salt.\r\n * @throws {Error} If the input password is invalid or hashing fails.\r\n */\r\nexport function hashPassword(password: string, options: HashPasswordOptions = {}): { hash: string; salt: string } {\r\n const { hash, salt, error } = $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { hash, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a hashed password and salt.\r\n *\r\n * @param password - The password to verify.\r\n * @param hashedPassword - The hashed password to compare against (in base64url format).\r\n * @param salt - The salt used during hashing (in base64url format).\r\n * @returns A boolean indicating whether the password matches the hashed password.\r\n */\r\nexport function verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): boolean {\r\n return $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing a Node.js Buffer with the encoded data or an error.\r\n */\r\nexport function tryConvertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Result<{ result: Buffer }> {\r\n return $convertStrToBytes(data, inputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Node.js Buffer containing the encoded data.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Buffer {\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Converts a Buffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Buffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing the string representation of the Buffer or an error.\r\n */\r\nexport function tryConvertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n return $convertBytesToStr(data, outputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a Buffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Buffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A string representation of the Buffer in the specified format.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, outputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A Result containing the converted string or an error.\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A converted string.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertEncoding(data: string, from: Encoding, to: Encoding): string {\r\n const { result, error } = $convertEncoding(data, from, to);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport { ENCODINGS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { Encoding } from '~/helpers/types';\r\nimport { $isStr } from '~/helpers/validate';\r\n\r\nexport function $convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Result<{ result: Buffer }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - String to Bytes: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(inputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - String to Bytes: Unsupported encoding: ${inputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n return $ok({ result: Buffer.from(data, inputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - String to Bytes: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertBytesToStr(data: Buffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n if (!(data instanceof Buffer)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Bytes to String: Invalid data type',\r\n desc: 'Data must be a Buffer',\r\n });\r\n }\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Bytes to String: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n return $ok(Buffer.from(data).toString(outputEncoding));\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Bytes to String: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Convert Format: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n if (!ENCODINGS.includes(from) || !ENCODINGS.includes(to)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Convert Format: Unsupported encoding: from ${from} to ${to}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const bytes = $convertStrToBytes(data, from);\r\n if (bytes.error) return $err({ msg: bytes.error.message, desc: bytes.error.description });\r\n\r\n const str = $convertBytesToStr(bytes.result, to);\r\n if (str.error) return $err({ msg: str.error.message, desc: str.error.description });\r\n\r\n return $ok({ result: str.result });\r\n}\r\n","import { Buffer } from 'node:buffer';\r\nimport nodeCrypto from 'node:crypto';\r\nimport { DIGEST_ALGORITHMS, ENCODINGS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $isStr, isSecretKey, matchPattern } from '~/helpers/validate';\r\nimport { $convertBytesToStr, $convertStrToBytes } from './node-encode';\r\n\r\nexport function $generateUuid(): Result<string> {\r\n try {\r\n return $ok(nodeCrypto.randomUUID());\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - UUID Generation: Failed to generate UUID', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $createSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Result<{ result: SecretKey<'node'> }> {\r\n if (!$isStr(secret)) {\r\n return $err({ msg: 'Crypto NodeJS API - Key Generation: Empty Secret', desc: 'Secret must be a non-empty string' });\r\n }\r\n\r\n const algorithm = options.algorithm ?? 'aes256gcm';\r\n if (!(algorithm in ENCRYPTION_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,\r\n desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const salt = options.salt ?? 'cipher-kit-salt';\r\n if (!$isStr(salt, 8)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Weak salt',\r\n desc: 'Salt must be a non-empty string with at least 8 characters',\r\n });\r\n }\r\n\r\n const info = options.info ?? 'cipher-kit';\r\n if (!$isStr(info)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Invalid info',\r\n desc: 'Info must be a non-empty string',\r\n });\r\n }\r\n\r\n const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n try {\r\n const derivedKey = nodeCrypto.hkdfSync(\r\n digestAlgo.node,\r\n secret.normalize('NFKC'),\r\n salt.normalize('NFKC'),\r\n info.normalize('NFKC'),\r\n encryptAlgo.keyBytes,\r\n );\r\n const key = nodeCrypto.createSecretKey(Buffer.from(derivedKey));\r\n const secretKey = Object.freeze({\r\n platform: 'node',\r\n digest: digest,\r\n algo: encryptAlgo,\r\n key: key,\r\n }) as SecretKey<'node'>;\r\n\r\n return $ok({ result: secretKey });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Key Generation: Failed to create secret key', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $encrypt(data: string, secretKey: SecretKey<'node'>, options: EncryptOptions = {}): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Empty data for encryption',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Encryption: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n if (!isSecretKey(secretKey, 'node')) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Invalid Secret Key',\r\n desc: 'Expected a Node SecretKey',\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const iv = nodeCrypto.randomBytes(secretKey.algo.ivLength);\r\n const cipher = nodeCrypto.createCipheriv(secretKey.algo.node, secretKey.key, iv);\r\n const encrypted = Buffer.concat([cipher.update(result), cipher.final()]);\r\n const tag = cipher.getAuthTag();\r\n\r\n const ivStr = $convertBytesToStr(iv, outputEncoding);\r\n const cipherStr = $convertBytesToStr(encrypted, outputEncoding);\r\n const tagStr = $convertBytesToStr(tag, outputEncoding);\r\n\r\n if (ivStr.error || cipherStr.error || tagStr.error) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Encryption: Failed to convert IV or encrypted data or tag',\r\n desc: `Conversion error: ${ivStr.error || cipherStr.error || tagStr.error}`,\r\n });\r\n }\r\n\r\n return $ok(`${ivStr.result}.${cipherStr.result}.${tagStr.result}.`);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Encryption: Failed to encrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<string> {\r\n if (matchPattern(encrypted, 'node') === false) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid encrypted data format',\r\n desc: 'Encrypted data must be in the format \"iv.cipher.tag.\"',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n const outputEncoding = options.outputEncoding ?? 'utf8';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Decryption: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const [iv, cipher, tag] = encrypted.split('.', 4);\r\n if (!$isStr(iv) || !$isStr(cipher) || !$isStr(tag)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid encrypted data',\r\n desc: 'Encrypted data must contain valid IV, encrypted data, and tag components',\r\n });\r\n }\r\n\r\n if (!isSecretKey(secretKey, 'node')) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Invalid Secret Key',\r\n desc: 'Expected a Node SecretKey',\r\n });\r\n }\r\n\r\n const ivBytes = $convertStrToBytes(iv, inputEncoding);\r\n const cipherBytes = $convertStrToBytes(cipher, inputEncoding);\r\n const tagBytes = $convertStrToBytes(tag, inputEncoding);\r\n\r\n if (ivBytes.error || cipherBytes.error || tagBytes.error) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Decryption: Failed to convert IV or encrypted data or tag',\r\n desc: `Conversion error: ${ivBytes.error || cipherBytes.error || tagBytes.error}`,\r\n });\r\n }\r\n\r\n try {\r\n const decipher = nodeCrypto.createDecipheriv(secretKey.algo.node, secretKey.key, ivBytes.result);\r\n decipher.setAuthTag(tagBytes.result);\r\n const decrypted = Buffer.concat([decipher.update(cipherBytes.result), decipher.final()]);\r\n\r\n return $convertBytesToStr(decrypted, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Decryption: Failed to decrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\nexport function $encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'node'>,\r\n options: EncryptOptions = {},\r\n): Result<string> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return $encrypt(result, secretKey, options);\r\n}\r\n\r\nexport function $decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'node'>,\r\n options: DecryptOptions = {},\r\n): Result<{ result: T }> {\r\n const { result, error } = $decrypt(encrypted, secretKey, options);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport function $hash(data: string, options: HashOptions = {}): Result<string> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Hashing: Empty data for hashing',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Hashing: Unsupported encoding: ${inputEncoding} or ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const hashed = nodeCrypto.createHash(digestAlgo.node).update(result).digest();\r\n return $convertBytesToStr(hashed, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Hashing: Failed to hash data with Crypto NodeJS', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Result<{ hash: string; salt: string }> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Empty password for hashing',\r\n desc: 'Password must be a non-empty string',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Password Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Password Hashing: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const saltLength = options.saltLength ?? 16;\r\n if (typeof saltLength !== 'number' || saltLength < 8) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Weak salt length',\r\n desc: 'Salt length must be a number and at least 8 bytes (recommended 16 or more)',\r\n });\r\n }\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Weak iterations count',\r\n desc: 'Iterations must be a number and at least 1000 (recommended 320,000 or more)',\r\n });\r\n }\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Password Hashing: Invalid key length',\r\n desc: 'Key length must be a number and at least 16 bytes (recommended 64 or more)',\r\n });\r\n }\r\n\r\n try {\r\n const salt = nodeCrypto.randomBytes(saltLength);\r\n const hash = nodeCrypto.pbkdf2Sync(password.normalize('NFKC'), salt, iterations, keyLength, digestAlgo.node);\r\n\r\n return $ok({ salt: salt.toString(outputEncoding), hash: hash.toString(outputEncoding) });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto NodeJS API - Password Hashing: Failed to hash password', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): boolean {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) return false;\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) return false;\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding)) return false;\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) return false;\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) return false;\r\n\r\n const saltBytes = $convertStrToBytes(salt, inputEncoding);\r\n if (saltBytes.error) return false;\r\n\r\n const hashedPasswordBytes = $convertStrToBytes(hashedPassword, inputEncoding);\r\n if (hashedPasswordBytes.error) return false;\r\n\r\n try {\r\n return nodeCrypto.timingSafeEqual(\r\n nodeCrypto.pbkdf2Sync(password.normalize('NFKC'), saltBytes.result, iterations, keyLength, digestAlgo.node),\r\n hashedPasswordBytes.result,\r\n );\r\n } catch {\r\n return false;\r\n }\r\n}\r\n"]}
|
|
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 'latin1'] as const);\r\n\r\nexport const DIGEST_ALGORITHMS = Object.freeze({\r\n sha256: { node: 'sha256', web: 'SHA-256' },\r\n sha384: { node: 'sha384', web: 'SHA-384' },\r\n sha512: { node: 'sha512', web: 'SHA-512' },\r\n} as const);\r\n\r\nexport const ENCRYPTION_ALGORITHMS = Object.freeze({\r\n aes256gcm: { name: 'aes256gcm', keyBytes: 32, ivLength: 12, node: 'aes-256-gcm', web: 'AES-GCM' },\r\n aes192gcm: { name: 'aes192gcm', keyBytes: 24, ivLength: 12, node: 'aes-192-gcm', web: 'AES-GCM' },\r\n aes128gcm: { name: 'aes128gcm', keyBytes: 16, ivLength: 12, node: 'aes-128-gcm', web: 'AES-GCM' },\r\n} as const);\r\n","import nodeCrypto from 'node:crypto';\r\nimport type { EncryptionAlgorithm, SecretKey } from '~/helpers/types';\r\nimport { DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from './consts';\r\n\r\nexport function $isStr(x: unknown, min = 1): x is string {\r\n return x !== null && x !== undefined && typeof x === 'string' && x.trim().length >= min;\r\n}\r\n\r\nexport function $isObj(x: unknown): x is Record<string, unknown> {\r\n if (typeof x !== 'object' || x === null || x === undefined) return false;\r\n const proto = Object.getPrototypeOf(x);\r\n return proto === Object.prototype || proto === null;\r\n}\r\n\r\nexport function $isLooseObj(x: unknown): x is Record<string, unknown> {\r\n return typeof x === 'object' && x !== null && x !== undefined;\r\n}\r\n\r\nconst expectedKeys = new Set(['platform', 'digest', 'algo', 'key']);\r\nconst expectedAlgoKeys = new Set(['name', 'keyBytes', 'ivLength', 'node', 'web']);\r\n\r\nexport function isSecretKey<Platform extends 'node' | 'web'>(x: unknown, platform: Platform): x is SecretKey<Platform> {\r\n if (!$isLooseObj(x) || (platform !== 'node' && platform !== 'web') || x.platform !== platform) return false;\r\n\r\n const keys = Object.keys(x);\r\n if (keys.length !== expectedKeys.size) return false;\r\n for (const key of keys) if (!expectedKeys.has(key)) return false;\r\n for (const key of expectedKeys) if (!Object.hasOwn(x, key)) return false;\r\n\r\n if (typeof x.digest !== 'string' || !(x.digest in DIGEST_ALGORITHMS)) return false;\r\n if (!$isLooseObj(x.algo) || typeof x.algo.name !== 'string' || !(x.algo.name in ENCRYPTION_ALGORITHMS)) return false;\r\n\r\n const algoKeys = Object.keys(x.algo);\r\n if (algoKeys.length !== expectedAlgoKeys.size) return false;\r\n for (const key of algoKeys) if (!expectedAlgoKeys.has(key)) return false;\r\n for (const key of expectedAlgoKeys) if (!Object.hasOwn(x.algo, key)) return false;\r\n\r\n const algo = ENCRYPTION_ALGORITHMS[x.algo.name as EncryptionAlgorithm];\r\n if (\r\n x.algo.keyBytes !== algo.keyBytes ||\r\n x.algo.ivLength !== algo.ivLength ||\r\n x.algo.node !== algo.node ||\r\n x.algo.web !== algo.web\r\n ) {\r\n return false;\r\n }\r\n\r\n if (!$isLooseObj(x.key) || x.key.type !== 'secret') return false;\r\n\r\n if (platform === 'node') {\r\n if (\r\n !(x.key instanceof nodeCrypto.KeyObject) ||\r\n (typeof x.key.symmetricKeySize === 'number' && x.key.symmetricKeySize !== algo.keyBytes)\r\n ) {\r\n return false;\r\n }\r\n return true;\r\n }\r\n\r\n if (\r\n !$isLooseObj(x.key.algorithm) ||\r\n x.key.algorithm.name !== algo.web ||\r\n (typeof x.key.algorithm.length === 'number' && x.key.algorithm.length !== algo.keyBytes * 8) ||\r\n typeof x.key.extractable !== 'boolean' ||\r\n !Array.isArray(x.key.usages) ||\r\n x.key.usages.length !== 2 ||\r\n !(x.key.usages.includes('encrypt') && x.key.usages.includes('decrypt'))\r\n ) {\r\n return false;\r\n }\r\n return true;\r\n}\r\n\r\n/** Regular expressions for encrypted data patterns */\r\nexport const ENCRYPTED_REGEX = Object.freeze({\r\n general: /^(?:[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+(?:\\.[A-Za-z0-9_-]+)?\\.)$/,\r\n node: /^([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)\\.$/,\r\n web: /^([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)\\.$/,\r\n});\r\n\r\n/** Checks if the input string matches the specified encrypted data pattern. */\r\nexport function matchPattern(data: string, format: 'general' | 'node' | 'web'): boolean {\r\n if (typeof data !== 'string') return false;\r\n if (!(format in ENCRYPTED_REGEX)) throw new Error(`Unknown format: ${format}`);\r\n return ENCRYPTED_REGEX[format].test(data);\r\n}\r\n","import { $isObj } from './validate';\r\n\r\nexport interface ResultErr {\r\n readonly message: string;\r\n readonly description: string;\r\n}\r\n\r\nexport type Result<T, E = ResultErr> = T extends object\r\n ?\r\n | ({ readonly [K in keyof T]: T[K] } & { readonly success: true; readonly error?: undefined })\r\n | ({ readonly [K in keyof T]?: undefined } & { readonly success: false; readonly error: E })\r\n :\r\n | { readonly success: true; readonly result: T; readonly error?: undefined }\r\n | { readonly success: false; readonly error: E; readonly result?: undefined };\r\n\r\nexport function $ok<T>(result?: T): Result<T> {\r\n if ($isObj(result)) return { success: true, ...(result as T & object) } as Result<T>;\r\n return { success: true, result } as Result<T>;\r\n}\r\n\r\nexport function $err(err: { msg: string; desc: string }): Result<never, ResultErr>;\r\nexport function $err(err: ResultErr): Result<never, ResultErr>;\r\nexport function $err(err: { msg: string; desc: string } | ResultErr): Result<never, ResultErr> {\r\n return {\r\n success: false,\r\n error: {\r\n message: 'msg' in err ? err.msg : err.message,\r\n description: 'desc' in err ? err.desc : err.description,\r\n },\r\n } as Result<never, ResultErr>;\r\n}\r\n\r\nexport function $fmtError(error: unknown): string {\r\n if (error instanceof Error) return error.message;\r\n if (typeof error === 'string') return error;\r\n return String(error);\r\n}\r\n\r\nexport function $fmtResultErr(err: ResultErr): string {\r\n return `${err.message} - ${err.description}`;\r\n}\r\n","import { $err, $fmtError, $fmtResultErr, $ok, type Result } from './error';\r\nimport { $isObj, $isStr } from './validate';\r\n\r\nexport function $stringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string> {\r\n try {\r\n if (!$isObj(obj)) return $err({ msg: 'Invalid object', desc: 'Input is not a plain object' });\r\n return $ok(JSON.stringify(obj));\r\n } catch (error) {\r\n return $err({ msg: 'Utility: Stringify error', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\n/**\r\n * Stringify an object.\r\n *\r\n * @param obj - The object to stringify.\r\n * @returns An JSON string.\r\n * @throws {Error} If the object cannot be stringified.\r\n */\r\nexport function stringifyObj<T extends object = Record<string, unknown>>(obj: T): string {\r\n const { result, error } = $stringifyObj(obj);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Stringify an object.\r\n *\r\n * @param obj - The object to stringify.\r\n * @returns A Result containing the JSON string or an error.\r\n */\r\nexport function tryStringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string> {\r\n return $stringifyObj(obj);\r\n}\r\n\r\nexport function $parseToObj<T extends object = Record<string, unknown>>(str: string): Result<{ result: T }> {\r\n try {\r\n if (!$isStr(str)) return $err({ msg: 'Utility: Invalid input', desc: 'Input is not a valid string' });\r\n const obj = JSON.parse(str);\r\n\r\n if (!$isObj(obj)) return $err({ msg: 'Utility: Invalid object format', desc: 'Parsed data is not a plain object' });\r\n return $ok({ result: obj as T });\r\n } catch (error) {\r\n return $err({ msg: 'Utility: Invalid format', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\n/**\r\n * Parse a string to an object.\r\n *\r\n * @param str - The JSON string to parse.\r\n * @returns A Result containing the parsed object or an error.\r\n */\r\nexport function tryParseToObj<T extends object = Record<string, unknown>>(str: string): Result<{ result: T }> {\r\n return $parseToObj<T>(str);\r\n}\r\n\r\n/**\r\n * Parse a string to an object.\r\n *\r\n * @param str - The JSON string to parse.\r\n * @returns A parsed object.\r\n * @throws {Error} If the string cannot be parsed or is not a valid object.\r\n */\r\nexport function parseToObj<T extends object = Record<string, unknown>>(str: string): T {\r\n const { result, error } = $parseToObj<T>(str);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n"]}
|
|
@@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base64url', 'hex', 'utf8', 'latin1'] as const);\r\n\r\nexport const DIGEST_ALGORITHMS = Object.freeze({\r\n sha256: { node: 'sha256', web: 'SHA-256' },\r\n sha384: { node: 'sha384', web: 'SHA-384' },\r\n sha512: { node: 'sha512', web: 'SHA-512' },\r\n} as const);\r\n\r\nexport const ENCRYPTION_ALGORITHMS = Object.freeze({\r\n aes256gcm: { name: 'aes256gcm', keyBytes: 32, ivLength: 12, node: 'aes-256-gcm', web: 'AES-GCM' },\r\n aes192gcm: { name: 'aes192gcm', keyBytes: 24, ivLength: 12, node: 'aes-192-gcm', web: 'AES-GCM' },\r\n aes128gcm: { name: 'aes128gcm', keyBytes: 16, ivLength: 12, node: 'aes-128-gcm', web: 'AES-GCM' },\r\n} as const);\r\n","import nodeCrypto from 'node:crypto';\r\nimport type { EncryptionAlgorithm, SecretKey } from '~/helpers/types';\r\nimport { DIGEST_ALGORITHMS, ENCRYPTION_ALGORITHMS } from './consts';\r\n\r\nexport function $isStr(x: unknown, min = 1): x is string {\r\n return x !== null && x !== undefined && typeof x === 'string' && x.trim().length >= min;\r\n}\r\n\r\nexport function $isObj(x: unknown): x is Record<string, unknown> {\r\n if (typeof x !== 'object' || x === null || x === undefined) return false;\r\n const proto = Object.getPrototypeOf(x);\r\n return proto === Object.prototype || proto === null;\r\n}\r\n\r\nexport function $isLooseObj(x: unknown): x is Record<string, unknown> {\r\n return typeof x === 'object' && x !== null && x !== undefined;\r\n}\r\n\r\nconst expectedKeys = new Set(['platform', 'digest', 'algo', 'key']);\r\nconst expectedAlgoKeys = new Set(['name', 'keyBytes', 'ivLength', 'node', 'web']);\r\n\r\nexport function isSecretKey<Platform extends 'node' | 'web'>(x: unknown, platform: Platform): x is SecretKey<Platform> {\r\n if (!$isLooseObj(x) || (platform !== 'node' && platform !== 'web') || x.platform !== platform) return false;\r\n\r\n const keys = Object.keys(x);\r\n if (keys.length !== expectedKeys.size) return false;\r\n for (const key of keys) if (!expectedKeys.has(key)) return false;\r\n for (const key of expectedKeys) if (!Object.hasOwn(x, key)) return false;\r\n\r\n if (typeof x.digest !== 'string' || !(x.digest in DIGEST_ALGORITHMS)) return false;\r\n if (!$isLooseObj(x.algo) || typeof x.algo.name !== 'string' || !(x.algo.name in ENCRYPTION_ALGORITHMS)) return false;\r\n\r\n const algoKeys = Object.keys(x.algo);\r\n if (algoKeys.length !== expectedAlgoKeys.size) return false;\r\n for (const key of algoKeys) if (!expectedAlgoKeys.has(key)) return false;\r\n for (const key of expectedAlgoKeys) if (!Object.hasOwn(x.algo, key)) return false;\r\n\r\n const algo = ENCRYPTION_ALGORITHMS[x.algo.name as EncryptionAlgorithm];\r\n if (\r\n x.algo.keyBytes !== algo.keyBytes ||\r\n x.algo.ivLength !== algo.ivLength ||\r\n x.algo.node !== algo.node ||\r\n x.algo.web !== algo.web\r\n ) {\r\n return false;\r\n }\r\n\r\n if (!$isLooseObj(x.key) || x.key.type !== 'secret') return false;\r\n\r\n if (platform === 'node') {\r\n if (\r\n !(x.key instanceof nodeCrypto.KeyObject) ||\r\n (typeof x.key.symmetricKeySize === 'number' && x.key.symmetricKeySize !== algo.keyBytes)\r\n ) {\r\n return false;\r\n }\r\n return true;\r\n }\r\n\r\n if (\r\n !$isLooseObj(x.key.algorithm) ||\r\n x.key.algorithm.name !== algo.web ||\r\n (typeof x.key.algorithm.length === 'number' && x.key.algorithm.length !== algo.keyBytes * 8) ||\r\n typeof x.key.extractable !== 'boolean' ||\r\n !Array.isArray(x.key.usages) ||\r\n x.key.usages.length !== 2 ||\r\n !(x.key.usages.includes('encrypt') && x.key.usages.includes('decrypt'))\r\n ) {\r\n return false;\r\n }\r\n return true;\r\n}\r\n\r\n/** Regular expressions for encrypted data patterns */\r\nexport const ENCRYPTED_REGEX = Object.freeze({\r\n general: /^(?:[A-Za-z0-9_-]+\\.[A-Za-z0-9_-]+(?:\\.[A-Za-z0-9_-]+)?\\.)$/,\r\n node: /^([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)\\.$/,\r\n web: /^([A-Za-z0-9_-]+)\\.([A-Za-z0-9_-]+)\\.$/,\r\n});\r\n\r\n/** Checks if the input string matches the specified encrypted data pattern. */\r\nexport function matchPattern(data: string, format: 'general' | 'node' | 'web'): boolean {\r\n if (typeof data !== 'string') return false;\r\n if (!(format in ENCRYPTED_REGEX)) throw new Error(`Unknown format: ${format}`);\r\n return ENCRYPTED_REGEX[format].test(data);\r\n}\r\n","import { $isObj } from './validate';\r\n\r\nexport interface ResultErr {\r\n readonly message: string;\r\n readonly description: string;\r\n}\r\n\r\nexport type Result<T, E = ResultErr> = T extends object\r\n ?\r\n | ({ readonly [K in keyof T]: T[K] } & { readonly success: true; readonly error?: undefined })\r\n | ({ readonly [K in keyof T]?: undefined } & { readonly success: false; readonly error: E })\r\n :\r\n | { readonly success: true; readonly result: T; readonly error?: undefined }\r\n | { readonly success: false; readonly error: E; readonly result?: undefined };\r\n\r\nexport function $ok<T>(result?: T): Result<T> {\r\n if ($isObj(result)) return { success: true, ...(result as T & object) } as Result<T>;\r\n return { success: true, result } as Result<T>;\r\n}\r\n\r\nexport function $err(err: { msg: string; desc: string }): Result<never, ResultErr>;\r\nexport function $err(err: ResultErr): Result<never, ResultErr>;\r\nexport function $err(err: { msg: string; desc: string } | ResultErr): Result<never, ResultErr> {\r\n return {\r\n success: false,\r\n error: {\r\n message: 'msg' in err ? err.msg : err.message,\r\n description: 'desc' in err ? err.desc : err.description,\r\n },\r\n } as Result<never, ResultErr>;\r\n}\r\n\r\nexport function $fmtError(error: unknown): string {\r\n if (error instanceof Error) return error.message;\r\n if (typeof error === 'string') return error;\r\n return String(error);\r\n}\r\n\r\nexport function $fmtResultErr(err: ResultErr): string {\r\n return `${err.message} - ${err.description}`;\r\n}\r\n","import { $err, $fmtError, $fmtResultErr, $ok, type Result } from './error';\r\nimport { $isObj, $isStr } from './validate';\r\n\r\nexport function $stringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string> {\r\n try {\r\n if (!$isObj(obj)) return $err({ msg: 'Invalid object', desc: 'Input is not a plain object' });\r\n return $ok(JSON.stringify(obj));\r\n } catch (error) {\r\n return $err({ msg: 'Utility: Stringify error', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\n/**\r\n * Stringify an object.\r\n *\r\n * @param obj - The object to stringify.\r\n * @returns An JSON string.\r\n * @throws {Error} If the object cannot be stringified.\r\n */\r\nexport function stringifyObj<T extends object = Record<string, unknown>>(obj: T): string {\r\n const { result, error } = $stringifyObj(obj);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Stringify an object.\r\n *\r\n * @param obj - The object to stringify.\r\n * @returns A Result containing the JSON string or an error.\r\n */\r\nexport function tryStringifyObj<T extends object = Record<string, unknown>>(obj: T): Result<string> {\r\n return $stringifyObj(obj);\r\n}\r\n\r\nexport function $parseToObj<T extends object = Record<string, unknown>>(str: string): Result<{ result: T }> {\r\n try {\r\n if (!$isStr(str)) return $err({ msg: 'Utility: Invalid input', desc: 'Input is not a valid string' });\r\n const obj = JSON.parse(str);\r\n\r\n if (!$isObj(obj)) return $err({ msg: 'Utility: Invalid object format', desc: 'Parsed data is not a plain object' });\r\n return $ok({ result: obj as T });\r\n } catch (error) {\r\n return $err({ msg: 'Utility: Invalid format', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\n/**\r\n * Parse a string to an object.\r\n *\r\n * @param str - The JSON string to parse.\r\n * @returns A Result containing the parsed object or an error.\r\n */\r\nexport function tryParseToObj<T extends object = Record<string, unknown>>(str: string): Result<{ result: T }> {\r\n return $parseToObj<T>(str);\r\n}\r\n\r\n/**\r\n * Parse a string to an object.\r\n *\r\n * @param str - The JSON string to parse.\r\n * @returns A parsed object.\r\n * @throws {Error} If the string cannot be parsed or is not a valid object.\r\n */\r\nexport function parseToObj<T extends object = Record<string, unknown>>(str: string): T {\r\n const { result, error } = $parseToObj<T>(str);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}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G,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,kBAAA,CAAmB,MAAM,aAAa,CAAA;AAChE,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAUO,SAAS,oBAAA,CACd,IAAA,EACA,cAAA,GAA2B,MAAA,EACX;AAChB,EAAA,OAAO,kBAAA,CAAmB,MAAM,cAAc,CAAA;AAChD;AAWO,SAAS,iBAAA,CAAkB,IAAA,EAAgC,cAAA,GAA2B,MAAA,EAAgB;AAC3G,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,EAAM,GAAI,kBAAA,CAAmB,MAAM,cAAc,CAAA;AACjE,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT;AAUO,SAAS,kBAAA,CAAmB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAA0C;AACzG,EAAA,OAAO,gBAAA,CAAiB,IAAA,EAAM,IAAA,EAAM,EAAE,CAAA;AACxC;AAWO,SAAS,eAAA,CAAgB,IAAA,EAAc,IAAA,EAAgB,EAAA,EAAsB;AAClF,EAAA,MAAM,EAAE,MAAA,EAAQ,KAAA,KAAU,gBAAA,CAAiB,IAAA,EAAM,MAAM,EAAE,CAAA;AACzD,EAAA,IAAI,OAAO,MAAM,IAAI,KAAA,CAAM,aAAA,CAAc,KAAK,CAAC,CAAA;AAC/C,EAAA,OAAO,MAAA;AACT","file":"chunk-SUGN4VDZ.js","sourcesContent":["import { $fmtResultErr, type Result } from '~/helpers/error';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n Encoding,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $convertBytesToStr, $convertEncoding, $convertStrToBytes } from './web-encode';\r\nimport {\r\n $createSecretKey,\r\n $decrypt,\r\n $decryptObj,\r\n $encrypt,\r\n $encryptObj,\r\n $generateUuid,\r\n $hash,\r\n $hashPassword,\r\n $verifyPassword,\r\n} from './web-encrypt';\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A Result containing a string representing the generated UUID or an error.\r\n */\r\nexport function tryGenerateUuid(): Result<string> {\r\n return $generateUuid();\r\n}\r\n\r\n/**\r\n * Generates a UUID (v4).\r\n *\r\n * @returns A string representing the generated UUID.\r\n * @throws {Error} If UUID generation fails.\r\n */\r\nexport function generateUuid(): string {\r\n const { result, error } = $generateUuid();\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A Result containing a SecretKey object representing the derived secret key or an error.\r\n */\r\nexport async function tryCreateSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Promise<Result<{ result: SecretKey<'web'> }>> {\r\n return await $createSecretKey(secret, options);\r\n}\r\n\r\n/**\r\n * Derives a secret key from the provided string for encryption/decryption.\r\n * Internally, the key is hashed using SHA-256 to ensure it meets the required length.\r\n *\r\n * @param secret - The input string to derive the secret key from.\r\n * @returns A SecretKey object representing the derived secret key.\r\n * @throws {Error} If the input key is invalid or key generation fails.\r\n */\r\nexport async function createSecretKey(secret: string, options: CreateSecretKeyOptions = {}): Promise<SecretKey<'web'>> {\r\n const { result, error } = await $createSecretKey(secret, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted data in the specified format or an error.\r\n */\r\nexport async function tryEncrypt(\r\n data: string,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n return await $encrypt(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input string using the provided secret key.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input string to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted data in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport async function encrypt(\r\n data: string,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $encrypt(data, secretKey);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing a string representing the decrypted data or an error.\r\n */\r\nexport async function tryDecrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<string>> {\r\n return await $decrypt(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag\" where each component is base64url encoded.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A string representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport async function decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $decrypt(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A Result containing a string representing the encrypted object in the specified format or an error.\r\n */\r\nexport async function tryEncryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n return await $encryptObj(data, secretKey, options);\r\n}\r\n\r\n/**\r\n * Encrypts the input object using the provided secret key.\r\n * The object is first serialized to a JSON string before encryption.\r\n * The output is a string in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n *\r\n * @param data - The input object to encrypt.\r\n * @param secretKey - The SecretKey object used for encryption.\r\n * @returns A string representing the encrypted object in the specified format.\r\n * @throws {Error} If the input data or key is invalid, or if encryption fails.\r\n */\r\nexport async function encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<string> {\r\n const { result, error } = await $encryptObj(data, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns A Result containing an object representing the decrypted data or an error.\r\n */\r\nexport async function tryDecryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<{ result: T }>> {\r\n return await $decryptObj<T>(encrypted, secretKey, options);\r\n}\r\n\r\n/**\r\n * Decrypts the input string to an object using the provided secret key.\r\n * The input must be in the format \"iv.cipherWithTag.\" where each component is base64url encoded.\r\n * The decrypted string is parsed as JSON to reconstruct the original object.\r\n *\r\n * @param encrypted - The input string to decrypt.\r\n * @param secretKey - The SecretKey object used for decryption.\r\n * @returns An object representing the decrypted data.\r\n * @throws {Error} If the input data or key is invalid, or if decryption fails.\r\n */\r\nexport async function decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<T> {\r\n const { result, error } = await $decryptObj<T>(encrypted, secretKey, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A Result containing a string representing the SHA-256 hash in base64url format or an error.\r\n */\r\nexport async function tryHash(data: string, options: HashOptions = {}): Promise<Result<string>> {\r\n return await $hash(data, options);\r\n}\r\n\r\n/**\r\n * Hashes the input string using SHA-256 and returns the hash in base64url format.\r\n *\r\n * @param data - The input string to hash.\r\n * @returns A string representing the SHA-256 hash in base64url format.\r\n * @throws {Error} If the input data is invalid or hashing fails.\r\n */\r\nexport async function hash(data: string, options: HashOptions = {}): Promise<string> {\r\n const { result, error } = await $hash(data, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns A Result containing an object with the hash and salt, or an error.\r\n */\r\nexport async function tryHashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Promise<Result<{ hash: string; salt: string }>> {\r\n return await $hashPassword(password, options);\r\n}\r\n\r\n/**\r\n * Hashes a password using PBKDF2 with SHA-512.\r\n *\r\n * @param password - The password to hash.\r\n * @returns An object with the hash and salt.\r\n * @throws {Error} If the input password is invalid or hashing fails.\r\n */\r\nexport async function hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Promise<{ hash: string; salt: string }> {\r\n const { hash, salt, error } = await $hashPassword(password, options);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return { hash, salt };\r\n}\r\n\r\n/**\r\n * Verifies a password against a hashed password and salt.\r\n *\r\n * @param password - The password to verify.\r\n * @param hashedPassword - The hashed password to compare against (in base64url format).\r\n * @param salt - The salt used during hashing (in base64url format).\r\n * @returns A boolean indicating whether the password matches the hashed password.\r\n */\r\nexport async function verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): Promise<boolean> {\r\n return await $verifyPassword(password, hashedPassword, salt, options);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing a Uint8Array with the encoded data or an error.\r\n */\r\nexport function tryConvertStrToBytes(\r\n data: string,\r\n inputEncoding: Encoding = 'utf8',\r\n): Result<{ result: Uint8Array<ArrayBuffer> }> {\r\n return $convertStrToBytes(data, inputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a string to a Buffer (byte array) using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input string to convert.\r\n * @param inputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Uint8Array containing the encoded data.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertStrToBytes(data: string, inputEncoding: Encoding = 'utf8'): Uint8Array<ArrayBuffer> {\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Converts a Uint8Array or ArrayBuffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Uint8Array or ArrayBuffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A Result containing the string representation of the Uint8Array or an error.\r\n */\r\nexport function tryConvertBytesToStr(\r\n data: Uint8Array | ArrayBuffer,\r\n outputEncoding: Encoding = 'utf8',\r\n): Result<string> {\r\n return $convertBytesToStr(data, outputEncoding);\r\n}\r\n\r\n/**\r\n * Converts a Uint8Array or ArrayBuffer (byte array) to a string using the specified encoding format.\r\n * Supported formats: 'base64', 'base64url', 'hex', 'utf8', 'latin1'.\r\n *\r\n * @param data - The input Uint8Array or ArrayBuffer to convert.\r\n * @param outputEncoding - The encoding format to use (default is 'utf8').\r\n * @returns A string representation of the Uint8Array in the specified format.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertBytesToStr(data: Uint8Array | ArrayBuffer, outputEncoding: Encoding = 'utf8'): string {\r\n const { result, error } = $convertBytesToStr(data, outputEncoding);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A Result containing the converted string or an error.\r\n */\r\nexport function tryConvertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n return $convertEncoding(data, from, to);\r\n}\r\n\r\n/**\r\n * Convert data from one encoding format to another.\r\n *\r\n * @param data - The input data to convert.\r\n * @param from - The encoding format to convert from.\r\n * @param to - The encoding format to convert to.\r\n * @returns A converted string.\r\n * @throws {Error} If the input data is invalid or conversion fails.\r\n */\r\nexport function convertEncoding(data: string, from: Encoding, to: Encoding): string {\r\n const { result, error } = $convertEncoding(data, from, to);\r\n if (error) throw new Error($fmtResultErr(error));\r\n return result;\r\n}\r\n","import { ENCODINGS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport type { Encoding } from '~/helpers/types';\r\nimport { $isStr } from '~/helpers/validate';\r\n\r\nexport const textEncoder = new TextEncoder();\r\nexport const textDecoder = new TextDecoder();\r\n\r\nexport function $convertStrToBytes(\r\n data: string,\r\n inputEncoding: Encoding = 'utf8',\r\n): Result<{ result: Uint8Array<ArrayBuffer> }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto Web API - String to Bytes: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(inputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - String to Bytes: Unsupported encoding: ${inputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n try {\r\n const bytes = strToBytes[inputEncoding](data);\r\n return $ok({ result: bytes });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - String to Bytes: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertBytesToStr(data: Uint8Array | ArrayBuffer, outputEncoding: Encoding = 'utf8'): Result<string> {\r\n if (!(data instanceof ArrayBuffer || data instanceof Uint8Array)) {\r\n return $err({\r\n msg: 'Crypto Web API - Bytes to String: Invalid data type',\r\n desc: 'Data must be an ArrayBuffer or Uint8Array',\r\n });\r\n }\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Bytes to String: Unsupported encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n try {\r\n const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);\r\n const str = bytesToStr[outputEncoding](bytes);\r\n return $ok(str);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Bytes to String: Failed to convert data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport function $convertEncoding(data: string, from: Encoding, to: Encoding): Result<{ result: string }> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto Web API - Convert Format: Empty data',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n if (!ENCODINGS.includes(from) || !ENCODINGS.includes(to)) {\r\n return $err({\r\n msg: `Crypto Web API - Convert Format: Unsupported encoding: from ${from} to ${to}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const bytes = $convertStrToBytes(data, from);\r\n if (bytes.error) return $err({ msg: bytes.error.message, desc: bytes.error.description });\r\n\r\n const str = $convertBytesToStr(bytes.result, to);\r\n if (str.error) return $err({ msg: str.error.message, desc: str.error.description });\r\n\r\n return $ok({ result: str.result });\r\n}\r\n\r\nconst strToBytes = {\r\n base64: $fromBase64,\r\n base64url: $fromBase64Url,\r\n hex: $fromHex,\r\n latin1: $fromLatin1,\r\n utf8: (data: string) => textEncoder.encode(data),\r\n} as const satisfies Record<Encoding, (data: string) => Uint8Array<ArrayBuffer>>;\r\n\r\nconst bytesToStr = {\r\n base64: $toBase64,\r\n base64url: $toBase64Url,\r\n hex: $toHex,\r\n latin1: $toLatin1,\r\n utf8: (data: Uint8Array) => textDecoder.decode(data),\r\n} as const satisfies Record<Encoding, (data: Uint8Array) => string>;\r\n\r\nfunction $toLatin1(bytes: Uint8Array): string {\r\n let out = '';\r\n const chunk = 1 << 15; // 32KiB per chunk\r\n for (let i = 0; i < bytes.length; i += chunk) {\r\n out += String.fromCharCode(...bytes.subarray(i, i + chunk));\r\n }\r\n return out;\r\n}\r\n\r\nfunction $fromLatin1(data: string): Uint8Array<ArrayBuffer> {\r\n const out = new Uint8Array(data.length);\r\n for (let i = 0; i < data.length; i++) {\r\n const charCode = data.charCodeAt(i);\r\n if (charCode > 255) throw new Error('Invalid latin1 string');\r\n out[i] = charCode;\r\n }\r\n return out;\r\n}\r\n\r\nfunction $toBase64(bytes: Uint8Array): string {\r\n return btoa($toLatin1(bytes));\r\n}\r\n\r\nfunction $fromBase64(data: string): Uint8Array<ArrayBuffer> {\r\n return $fromLatin1(atob(data));\r\n}\r\n\r\nfunction $toBase64Url(bytes: Uint8Array): string {\r\n return $toBase64(bytes).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '');\r\n}\r\n\r\nfunction $fromBase64Url(data: string): Uint8Array<ArrayBuffer> {\r\n let base64 = data.replace(/-/g, '+').replace(/_/g, '/');\r\n const padLen = (4 - (base64.length % 4)) % 4;\r\n base64 += '='.repeat(padLen);\r\n return $fromBase64(base64);\r\n}\r\n\r\nfunction $toHex(bytes: Uint8Array): string {\r\n return Array.from(bytes)\r\n .map((b) => b.toString(16).padStart(2, '0'))\r\n .join('');\r\n}\r\n\r\nfunction $fromHex(data: string): Uint8Array<ArrayBuffer> {\r\n const clean = data.startsWith('0x') ? data.slice(2) : data;\r\n if (clean.length % 2 !== 0) throw new Error('Invalid hex string');\r\n const out = new Uint8Array(clean.length / 2);\r\n for (let i = 0; i < out.length; i++) {\r\n const byte = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);\r\n if (Number.isNaN(byte)) throw new Error('Invalid hex string');\r\n out[i] = byte;\r\n }\r\n return out;\r\n}\r\n","import { DIGEST_ALGORITHMS, ENCODINGS, ENCRYPTION_ALGORITHMS } from '~/helpers/consts';\r\nimport { $err, $fmtError, $ok, type Result } from '~/helpers/error';\r\nimport { $parseToObj, $stringifyObj } from '~/helpers/object';\r\nimport type {\r\n CreateSecretKeyOptions,\r\n DecryptOptions,\r\n EncryptOptions,\r\n HashOptions,\r\n HashPasswordOptions,\r\n SecretKey,\r\n VerifyPasswordOptions,\r\n} from '~/helpers/types';\r\nimport { $isStr, isSecretKey, matchPattern } from '~/helpers/validate';\r\nimport { $convertBytesToStr, $convertStrToBytes, textEncoder } from './web-encode';\r\n\r\nexport function $generateUuid(): Result<string> {\r\n try {\r\n return $ok(crypto.randomUUID());\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - UUID Generation: Failed to generate UUID', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $createSecretKey(\r\n secret: string,\r\n options: CreateSecretKeyOptions = {},\r\n): Promise<Result<{ result: SecretKey<'web'> }>> {\r\n if (!$isStr(secret)) {\r\n return $err({ msg: 'Crypto Web API - Key Generation: Empty Secret', desc: 'Secret must be a non-empty string' });\r\n }\r\n\r\n const algorithm = options.algorithm ?? 'aes256gcm';\r\n if (!(algorithm in ENCRYPTION_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported algorithm: ${algorithm}`,\r\n desc: `Supported algorithms are: ${Object.keys(ENCRYPTION_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto NodeJS API - Key Generation: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n\r\n const salt = options.salt ?? 'cipher-kit-salt';\r\n if (!$isStr(salt, 8)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Weak salt',\r\n desc: 'Salt must be a non-empty string with at least 8 characters',\r\n });\r\n }\r\n\r\n const info = options.info ?? 'cipher-kit';\r\n if (!$isStr(info)) {\r\n return $err({\r\n msg: 'Crypto NodeJS API - Key Generation: Invalid info',\r\n desc: 'Info must be a non-empty string',\r\n });\r\n }\r\n\r\n const encryptAlgo = ENCRYPTION_ALGORITHMS[algorithm];\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n try {\r\n const ikm = await crypto.subtle.importKey('raw', textEncoder.encode(secret.normalize('NFKC')), 'HKDF', false, [\r\n 'deriveKey',\r\n ]);\r\n const key = await crypto.subtle.deriveKey(\r\n {\r\n name: 'HKDF',\r\n hash: digestAlgo.web,\r\n salt: textEncoder.encode(salt.normalize('NFKC')),\r\n info: textEncoder.encode(info.normalize('NFKC')),\r\n },\r\n ikm,\r\n { name: encryptAlgo.web, length: encryptAlgo.keyBytes * 8 },\r\n true,\r\n ['encrypt', 'decrypt'],\r\n );\r\n const secretKey = Object.freeze({\r\n platform: 'web',\r\n digest: digest,\r\n algo: encryptAlgo,\r\n key: key,\r\n }) as SecretKey<'web'>;\r\n\r\n return $ok({ result: secretKey });\r\n } catch (error) {\r\n return $err({\r\n msg: 'Crypto Web API - Key Generation: Failed to create secret key',\r\n desc: $fmtError(error),\r\n });\r\n }\r\n}\r\n\r\nexport async function $encrypt(\r\n data: string,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n if (!$isStr(data)) {\r\n return $err({\r\n msg: 'Crypto Web API - Encryption: Empty data for encryption',\r\n desc: 'Data must be a non-empty string',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Encryption: Unsupported encoding: input ${inputEncoding} or output ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n if (!isSecretKey(secretKey, 'web')) {\r\n return $err({\r\n msg: 'Crypto Web API - Encryption: Invalid Secret Key',\r\n desc: 'Expected a Web SecretKey',\r\n });\r\n }\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const iv = crypto.getRandomValues(new Uint8Array(secretKey.algo.ivLength));\r\n const cipherWithTag = await crypto.subtle.encrypt({ name: secretKey.algo.web, iv: iv }, secretKey.key, result);\r\n\r\n const ivStr = $convertBytesToStr(iv, outputEncoding);\r\n const cipherStr = $convertBytesToStr(cipherWithTag, outputEncoding);\r\n\r\n if (ivStr.error || cipherStr.error) {\r\n return $err({\r\n msg: 'Crypto Web API - Encryption: Failed to convert IV or encrypted data',\r\n desc: `Conversion error: ${ivStr.error || cipherStr.error}`,\r\n });\r\n }\r\n\r\n return $ok(`${ivStr.result}.${cipherStr.result}.`);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Encryption: Failed to encrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $decrypt(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<string>> {\r\n if (matchPattern(encrypted, 'web') === false) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Invalid encrypted data format',\r\n desc: 'Encrypted data must be in the format \"iv.cipherWithTag.\"',\r\n });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n const outputEncoding = options.outputEncoding ?? 'utf8';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Decryption: Unsupported encoding: input ${inputEncoding} or output ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const [iv, encryptedWithTag] = encrypted.split('.', 3);\r\n if (!$isStr(iv) || !$isStr(encryptedWithTag)) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Invalid encrypted data',\r\n desc: 'Encrypted data must contain valid IV, encrypted and tag components',\r\n });\r\n }\r\n\r\n if (!isSecretKey(secretKey, 'web')) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Invalid Secret Key',\r\n desc: 'Expected a Web SecretKey',\r\n });\r\n }\r\n\r\n const ivBytes = $convertStrToBytes(iv, inputEncoding);\r\n const cipherWithTagBytes = $convertStrToBytes(encryptedWithTag, inputEncoding);\r\n\r\n if (ivBytes.error || cipherWithTagBytes.error) {\r\n return $err({\r\n msg: 'Crypto Web API - Decryption: Failed to convert IV or encrypted data',\r\n desc: `Conversion error: ${ivBytes.error || cipherWithTagBytes.error}`,\r\n });\r\n }\r\n\r\n try {\r\n const decrypted = await crypto.subtle.decrypt(\r\n { name: secretKey.algo.web, iv: ivBytes.result },\r\n secretKey.key,\r\n cipherWithTagBytes.result,\r\n );\r\n\r\n return $convertBytesToStr(decrypted, 'utf8');\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Decryption: Failed to decrypt data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $encryptObj<T extends object = Record<string, unknown>>(\r\n data: T,\r\n secretKey: SecretKey<'web'>,\r\n options: EncryptOptions = {},\r\n): Promise<Result<string>> {\r\n const { result, error } = $stringifyObj(data);\r\n if (error) return $err(error);\r\n return await $encrypt(result, secretKey, options);\r\n}\r\n\r\nexport async function $decryptObj<T extends object = Record<string, unknown>>(\r\n encrypted: string,\r\n secretKey: SecretKey<'web'>,\r\n options: DecryptOptions = {},\r\n): Promise<Result<{ result: T }>> {\r\n const { result, error } = await $decrypt(encrypted, secretKey, options);\r\n if (error) return $err(error);\r\n return $parseToObj<T>(result);\r\n}\r\n\r\nexport async function $hash(data: string, options: HashOptions = {}): Promise<Result<string>> {\r\n if (!$isStr(data)) {\r\n return $err({ msg: 'Crypto Web API - Hashing: Empty data for hashing', desc: 'Data must be a non-empty string' });\r\n }\r\n\r\n const inputEncoding = options.inputEncoding ?? 'utf8';\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding) || !ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Hashing: Unsupported input encoding: ${inputEncoding} or output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha256';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto Web API - Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const { result, error } = $convertStrToBytes(data, inputEncoding);\r\n if (error) return $err(error);\r\n\r\n try {\r\n const hashed = await crypto.subtle.digest(digestAlgo.web, result);\r\n return $convertBytesToStr(hashed, outputEncoding);\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Hashing: Failed to hash data', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $hashPassword(\r\n password: string,\r\n options: HashPasswordOptions = {},\r\n): Promise<Result<{ hash: string; salt: string }>> {\r\n if (!$isStr(password)) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Empty password',\r\n desc: 'Password must be a non-empty string',\r\n });\r\n }\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) {\r\n return $err({\r\n msg: `Crypto Web API - Password Hashing: Unsupported digest: ${digest}`,\r\n desc: `Supported digests are: ${Object.keys(DIGEST_ALGORITHMS).join(', ')}`,\r\n });\r\n }\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const outputEncoding = options.outputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(outputEncoding)) {\r\n return $err({\r\n msg: `Crypto Web API - Password Hashing: Unsupported output encoding: ${outputEncoding}`,\r\n desc: 'Use base64, base64url, hex, utf8, or latin1',\r\n });\r\n }\r\n\r\n const saltLength = options.saltLength ?? 16;\r\n if (typeof saltLength !== 'number' || saltLength < 8) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Weak salt length',\r\n desc: 'Salt length must be a number and at least 8 bytes (recommended 16)',\r\n });\r\n }\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Weak iteration count',\r\n desc: 'Iterations must be a number and at least 1000 (recommended 320,000 or more)',\r\n });\r\n }\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) {\r\n return $err({\r\n msg: 'Crypto Web API - Password Hashing: Weak key length',\r\n desc: 'Key length must be a number and at least 16 bytes (recommended 64)',\r\n });\r\n }\r\n\r\n try {\r\n const salt = crypto.getRandomValues(new Uint8Array(saltLength));\r\n const baseKey = await crypto.subtle.importKey(\r\n 'raw',\r\n textEncoder.encode(password.normalize('NFKC')),\r\n 'PBKDF2',\r\n false,\r\n ['deriveBits'],\r\n );\r\n const bits = await crypto.subtle.deriveBits(\r\n { name: 'PBKDF2', salt, iterations: iterations, hash: digestAlgo.web },\r\n baseKey,\r\n keyLength * 8,\r\n );\r\n\r\n const saltStr = $convertBytesToStr(salt, outputEncoding);\r\n if (saltStr.error) return $err(saltStr.error);\r\n\r\n const hashedPasswordStr = $convertBytesToStr(bits, outputEncoding);\r\n if (hashedPasswordStr.error) return $err(hashedPasswordStr.error);\r\n\r\n return $ok({ hash: hashedPasswordStr.result, salt: saltStr.result });\r\n } catch (error) {\r\n return $err({ msg: 'Crypto Web API - Password Hashing: Failed to hash password', desc: $fmtError(error) });\r\n }\r\n}\r\n\r\nexport async function $verifyPassword(\r\n password: string,\r\n hashedPassword: string,\r\n salt: string,\r\n options: VerifyPasswordOptions = {},\r\n): Promise<boolean> {\r\n if (!$isStr(password) || !$isStr(hashedPassword) || !$isStr(salt)) return false;\r\n\r\n const digest = options.digest ?? 'sha512';\r\n if (!(digest in DIGEST_ALGORITHMS)) return false;\r\n const digestAlgo = DIGEST_ALGORITHMS[digest];\r\n\r\n const inputEncoding = options.inputEncoding ?? 'base64url';\r\n if (!ENCODINGS.includes(inputEncoding)) return false;\r\n\r\n const iterations = options.iterations ?? 320_000;\r\n if (typeof iterations !== 'number' || iterations < 1000) return false;\r\n\r\n const keyLength = options.keyLength ?? 64;\r\n if (typeof keyLength !== 'number' || keyLength < 16) return false;\r\n\r\n const saltBytes = $convertStrToBytes(salt, 'base64url');\r\n if (saltBytes.error) return false;\r\n\r\n const hashedPasswordBytes = $convertStrToBytes(hashedPassword, 'base64url');\r\n if (hashedPasswordBytes.error) return false;\r\n\r\n try {\r\n const baseKey = await crypto.subtle.importKey(\r\n 'raw',\r\n textEncoder.encode(password.normalize('NFKC')),\r\n 'PBKDF2',\r\n false,\r\n ['deriveBits'],\r\n );\r\n\r\n const bits = new Uint8Array(\r\n await crypto.subtle.deriveBits(\r\n {\r\n name: 'PBKDF2',\r\n salt: saltBytes.result,\r\n iterations: iterations,\r\n hash: digestAlgo.web,\r\n },\r\n baseKey,\r\n keyLength * 8,\r\n ),\r\n );\r\n\r\n if (bits.length !== hashedPasswordBytes.result.length) return false;\r\n\r\n let isMatch = true;\r\n for (let i = 0; i < bits.length; i++) {\r\n if (bits[i] !== hashedPasswordBytes.result[i]) isMatch = false;\r\n }\r\n\r\n return isMatch;\r\n } catch {\r\n return false;\r\n }\r\n}\r\n"]}
|