UNPKG

node-ciphers

Version:

Lightweight AES and DES encryption library for Node.js, featuring flexible encoding, multiple cipher modes, and TypeScript support.

1 lines 4 kB
{"version":3,"file":"base.cjs","sources":["../src/base.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\nimport type {\n BinaryLike,\n Cipheriv,\n Decipheriv,\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() {\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(data: BinaryLike, encoding: BufferEncoding) {\n return typeof data === 'string' ? Buffer.from(data, encoding) : data;\n }\n\n protected getCipherResult(cipher: Cipheriv, data: BinaryLike, encodingOptions?: EncodingOptions) {\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(decipher: Decipheriv, encryptedData: BinaryLike, encodingOptions?: EncodingOptions) {\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":["defaultEncodingOptions","Buffer"],"mappings":";;;;;MAgBa,UAAU,CAAA;AACV,IAAA,gBAAgB;AAEzB,IAAA,WAAA,CAAY,eAAiC,EAAA;QACzC,IAAI,CAAC,gBAAgB,GAA4C;AAC7D,YAAA,GAAGA,gCAAsB;AACzB,YAAA,GAAG,eAAe;SACrB;IACL;AAEA,IAAA,IAAI,eAAe,GAAA;QACf,OAAO,IAAI,CAAC,gBAAgB;IAChC;AAEU,IAAA,iBAAiB,CAAC,KAAc,EAAA;QACtC,OAAO;AACH,YAAA,KAAK,EAAE,KAAgB;AACvB,YAAA,EAAE,EAAE,KAAc;SACrB;IACL;AAEU,IAAA,cAAc,CAAI,KAAQ,EAAA;QAChC,OAAO;AACH,YAAA,EAAE,EAAE,IAAa;AACjB,YAAA,KAAK,EAAE,KAAU;SACpB;IACL;IAEU,YAAY,CAAC,IAAgB,EAAE,QAAwB,EAAA;AAC7D,QAAA,OAAO,OAAO,IAAI,KAAK,QAAQ,GAAGC,kBAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,IAAI;IACxE;AAEU,IAAA,eAAe,CAAC,MAAgB,EAAE,IAAgB,EAAE,eAAiC,EAAA;QAC3F,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;IACpC;AAEU,IAAA,iBAAiB,CAAC,QAAoB,EAAE,aAAyB,EAAE,eAAiC,EAAA;QAC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