"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.encrypt = encrypt;
exports.decrypt = decrypt;
exports.maskSecret = maskSecret;
const crypto_1 = __importDefault(require("crypto"));
const ALGORITHM = 'aes-256-cbc';
// Use process.env.ENCRYPTION_KEY or fallback to a default secure key
const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY || 'd6F3E0C1B2A3948576E5D4C3B2A10099'; // Must be 32 bytes
const IV_LENGTH = 16;
function encrypt(text) {
    if (!text)
        return '';
    const iv = crypto_1.default.randomBytes(IV_LENGTH);
    const cipher = crypto_1.default.createCipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY), iv);
    let encrypted = cipher.update(text);
    encrypted = Buffer.concat([encrypted, cipher.final()]);
    return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(text) {
    if (!text)
        return '';
    const textParts = text.split(':');
    const ivPart = textParts.shift();
    if (!ivPart)
        return '';
    const iv = Buffer.from(ivPart, 'hex');
    const encryptedText = Buffer.from(textParts.join(':'), 'hex');
    const decipher = crypto_1.default.createDecipheriv(ALGORITHM, Buffer.from(ENCRYPTION_KEY), iv);
    let decrypted = decipher.update(encryptedText);
    decrypted = Buffer.concat([decrypted, decipher.final()]);
    return decrypted.toString();
}
function maskSecret(secret) {
    if (!secret)
        return '';
    return '************';
}
//# sourceMappingURL=encryption.utils.js.map