node-ciphers
Version:
Lightweight AES and DES encryption library for Node.js, featuring flexible encoding, multiple cipher modes, and TypeScript support.
1 lines • 4.1 kB
Source Map (JSON)
{"version":3,"file":"base.mjs","sources":["../src/base.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\nimport type {\n BinaryLike,\n Cipher,\n Decipher,\n} from 'node:crypto';\n\nimport type { RequiredDeep } from 'type-fest';\n\nimport { defaultEncodingOptions } from './constants';\nimport type {\n BaseCipherEncodingOptions,\n HasAuthTagAesCipherEncodingOptions,\n Result,\n} from './types';\n\nexport class BaseCipher<EncodingOptions extends HasAuthTagAesCipherEncodingOptions = BaseCipherEncodingOptions> {\n readonly #encodingOptions: Readonly<RequiredDeep<EncodingOptions>>;\n\n constructor(encodingOptions?: EncodingOptions) {\n this.#encodingOptions = <Readonly<RequiredDeep<EncodingOptions>>>{\n ...defaultEncodingOptions,\n ...encodingOptions,\n };\n }\n\n get encodingOptions(): Readonly<RequiredDeep<EncodingOptions>> {\n return this.#encodingOptions;\n }\n\n protected createErrorResult(error: unknown) {\n return {\n error: error as unknown,\n ok: false as const,\n };\n }\n\n protected createOkResult<T>(value: T) {\n return {\n ok: true as const,\n value: value as T,\n };\n }\n\n protected dataToBuffer(\n data: BinaryLike,\n encoding: BufferEncoding,\n ): Buffer<ArrayBuffer> | NodeJS.ArrayBufferView<ArrayBufferLike> {\n return typeof data === 'string' ? Buffer.from(data, encoding) : data;\n }\n\n protected getCipherResult(cipher: Cipher, data: BinaryLike, encodingOptions?: EncodingOptions): string {\n const outputEncoding = encodingOptions?.encryptOutput || this.#encodingOptions.encryptOutput;\n return cipher.update(\n this.dataToBuffer(data, encodingOptions?.encryptInput || this.#encodingOptions.encryptInput),\n undefined,\n outputEncoding,\n ) + cipher.final(outputEncoding);\n }\n\n protected getDecipherResult(\n decipher: Decipher,\n encryptedData: BinaryLike,\n encodingOptions?: EncodingOptions,\n ): string {\n const outputEncoding = encodingOptions?.decryptOutput || this.#encodingOptions.decryptOutput;\n return decipher.update(\n this.dataToBuffer(encryptedData, encodingOptions?.decryptInput || this.#encodingOptions.decryptInput),\n undefined,\n outputEncoding,\n ) + decipher.final(outputEncoding);\n }\n\n protected parseJson<T>(data: string): Result<T> {\n try {\n return this.createOkResult(JSON.parse(data) as T);\n } catch (error) {\n return this.createErrorResult(error);\n }\n }\n}\n"],"names":[],"mappings":";;;MAgBa,UAAU,CAAA;AACV,IAAA,gBAAgB;AAEzB,IAAA,WAAA,CAAY,eAAiC,EAAA;QACzC,IAAI,CAAC,gBAAgB,GAA4C;AAC7D,YAAA,GAAG,sBAAsB;AACzB,YAAA,GAAG,eAAe;SACrB;;AAGL,IAAA,IAAI,eAAe,GAAA;QACf,OAAO,IAAI,CAAC,gBAAgB;;AAGtB,IAAA,iBAAiB,CAAC,KAAc,EAAA;QACtC,OAAO;AACH,YAAA,KAAK,EAAE,KAAgB;AACvB,YAAA,EAAE,EAAE,KAAc;SACrB;;AAGK,IAAA,cAAc,CAAI,KAAQ,EAAA;QAChC,OAAO;AACH,YAAA,EAAE,EAAE,IAAa;AACjB,YAAA,KAAK,EAAE,KAAU;SACpB;;IAGK,YAAY,CAClB,IAAgB,EAChB,QAAwB,EAAA;AAExB,QAAA,OAAO,OAAO,IAAI,KAAK,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,IAAI;;AAG9D,IAAA,eAAe,CAAC,MAAc,EAAE,IAAgB,EAAE,eAAiC,EAAA;QACzF,MAAM,cAAc,GAAG,eAAe,EAAE,aAAa,IAAI,IAAI,CAAC,gBAAgB,CAAC,aAAa;AAC5F,QAAA,OAAO,MAAM,CAAC,MAAM,CAChB,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,eAAe,EAAE,YAAY,IAAI,IAAI,CAAC,gBAAgB,CAAC,YAAY,CAAC,EAC5F,SAAS,EACT,cAAc,CACjB,GAAG,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC;;AAG1B,IAAA,iBAAiB,C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