UNPKG

node-ciphers

Version:

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

71 lines (63 loc) 2.15 kB
import type { BinaryLike } from 'node:crypto'; import type { TransformOptions } from 'node:stream'; import type { DesCipherEncodingOptions, EcbEncryptResult, Result, } from '@/types'; import { BaseDesCipher } from './_internals/base'; export class Ecb extends BaseDesCipher { constructor(key: BinaryLike, encodingOptions?: DesCipherEncodingOptions) { super(key, 'ecb', encodingOptions); } decrypt( encryptedData: BinaryLike, // @ts-expect-error Allow iv to be null. iv?: null, encodingOptions?: DesCipherEncodingOptions.Decrypt, decipherOptions?: TransformOptions, ): Result<string> { try { return this.createOkResult( this.getDecipherResult(this.createDecipher(null, decipherOptions), encryptedData, encodingOptions), ); } catch (error) { return this.createErrorResult(error); } } decryptToJson<T = any>( encryptedData: BinaryLike, iv?: null, encodingOptions?: DesCipherEncodingOptions.Decrypt, decipherOptions?: TransformOptions, ): Result<T> { const result = this.decrypt(encryptedData, iv, encodingOptions, decipherOptions); if (!result.ok) return result; return this.parseJson<T>(result.value); } encrypt( data: BinaryLike, encodingOptions?: DesCipherEncodingOptions.Encrypt, cipherOptions?: TransformOptions, ): EcbEncryptResult { try { return this.createOkResult({ data: this.getCipherResult(this.createCipher(null, cipherOptions), data, encodingOptions), iv: null, }); } catch (error) { return this.createErrorResult(error); } } encryptJson( data: any, encodingOptions?: DesCipherEncodingOptions.Encrypt, cipherOptions?: TransformOptions, ): EcbEncryptResult { try { return this.encrypt(JSON.stringify(data), encodingOptions, cipherOptions); } catch (error) { return this.createErrorResult(error); } } }