UNPKG

easy-cipher-mate

Version:

A CLI and programmatic tool for encryption/decryption supporting AES-GCM and ChaCha20-Poly1305 algorithms, with text encoding options and line-by-line text file processing.

42 lines (41 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EncryptionService = void 0; const fs_1 = require("fs"); class EncryptionService { constructor(algorithm, algorithmConfig) { this.algorithm = algorithm; this.algorithmConfig = algorithmConfig; } async encryptText(plaintext, encoding) { return this.algorithm.encryptText(plaintext, this.algorithmConfig, encoding); } async decryptText(encryptedData, encoding) { return this.algorithm.decryptText(encryptedData, this.algorithmConfig, encoding); } async encryptFile(fileBuffer, encoding) { return this.algorithm.encryptFile(fileBuffer, this.algorithmConfig, encoding); } async decryptFile(encryptedBuffer, encoding) { return this.algorithm.decryptFile(encryptedBuffer, this.algorithmConfig, encoding); } async encryptFileByName(fileName) { const fileBuffer = (0, fs_1.readFileSync)(fileName); const arrayBuffer = fileBuffer.buffer.slice(fileBuffer.byteOffset, fileBuffer.byteOffset + fileBuffer.byteLength); return this.encryptFile(arrayBuffer); } async decryptFileByName(encryptedResult, fileName) { const decryptedBuffer = await this.decryptFile(encryptedResult.data); const uint8Array = new Uint8Array(decryptedBuffer); (0, fs_1.writeFileSync)(fileName, uint8Array); } arrayBufferToString(buffer) { const decoder = new TextDecoder('utf-8'); return decoder.decode(buffer); } stringToArrayBuffer(str) { const encoder = new TextEncoder(); return encoder.encode(str).buffer; } } exports.EncryptionService = EncryptionService;