"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BufferEnc = void 0;
const crypto_1 = __importDefault(require("crypto"));
const zlib_1 = __importDefault(require("zlib"));
class BufferEnc {
    algorithm = 'aes-256-cbc';
    key = crypto_1.default.randomBytes(32);
    iv = crypto_1.default.randomBytes(16);
    encrypt(buf) {
        let cipher = crypto_1.default.createCipheriv('aes-256-cbc', Buffer.from(this.key), this.iv);
        let encrypted = cipher.update(zlib_1.default.deflateSync(buf));
        encrypted = Buffer.concat([encrypted, cipher.final()]);
        let d = { iv: this.iv.toString('hex'), encryptedData: encrypted.toString('hex') };
        return Buffer.from(JSON.stringify(d));
    }
    decrypt(buf) {
        let txt = buf.toString();
        let obj = JSON.parse(txt);
        let iv = Buffer.from(obj.iv, 'hex');
        let encryptedText = Buffer.from(obj.encryptedData, 'hex');
        let decipher = crypto_1.default.createDecipheriv('aes-256-cbc', Buffer.from(this.key), iv);
        let decrypted = decipher.update(encryptedText);
        decrypted = Buffer.concat([decrypted, decipher.final()]);
        return zlib_1.default.inflateSync(decrypted);
    }
}
exports.BufferEnc = BufferEnc;
//# sourceMappingURL=BufferEnc.js.map