UNPKG

node-ciphers

Version:

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

41 lines (39 loc) 1.46 kB
import { BaseDesCipher } from "./index.js"; import { randomBytes } from "node:crypto"; //#region src/des/_internals/base/encrypt-and-decrypt.ts var BaseDesEncryptAndDecrypt = class extends BaseDesCipher { decrypt(encryptedData, iv, encodingOptions, decipherOptions) { try { const decipher = this.createDecipher(this.dataToBuffer(iv, encodingOptions?.iv || this.encodingOptions.iv), decipherOptions); return this.createOkResult(this.getDecipherResult(decipher, encryptedData, encodingOptions)); } catch (error) { return this.createErrorResult(error); } } decryptToJson(encryptedData, iv, encodingOptions, decipherOptions) { const result = this.decrypt(encryptedData, iv, encodingOptions, decipherOptions); if (!result.ok) return result; return this.parseJson(result.value); } encrypt(data, encodingOptions, cipherOptions) { const iv = randomBytes(8); try { return this.createOkResult({ data: this.getCipherResult(this.createCipher(iv, cipherOptions), data, encodingOptions), iv: iv.toString(encodingOptions?.iv || this.encodingOptions.iv) }); } catch (error) { return this.createErrorResult(error); } } encryptJson(data, encodingOptions, cipherOptions) { try { return this.encrypt(JSON.stringify(data), encodingOptions, cipherOptions); } catch (error) { return this.createErrorResult(error); } } }; //#endregion export { BaseDesEncryptAndDecrypt }; //# sourceMappingURL=encrypt-and-decrypt.js.map