"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CryptoBox = void 0;
const node_crypto_1 = __importDefault(require("node:crypto"));
const algorithm = "aes-256-gcm";
class CryptoBox {
    key;
    constructor(secret) {
        this.key = node_crypto_1.default.createHash("sha256").update(secret).digest();
    }
    encrypt(plainText) {
        const iv = node_crypto_1.default.randomBytes(12);
        const cipher = node_crypto_1.default.createCipheriv(algorithm, this.key, iv);
        const encrypted = Buffer.concat([cipher.update(plainText, "utf8"), cipher.final()]);
        const tag = cipher.getAuthTag();
        return [iv.toString("base64"), tag.toString("base64"), encrypted.toString("base64")].join(":");
    }
    decrypt(payload) {
        const [iv, tag, encrypted] = payload.split(":").map((part) => Buffer.from(part, "base64"));
        const decipher = node_crypto_1.default.createDecipheriv(algorithm, this.key, iv);
        decipher.setAuthTag(tag);
        return Buffer.concat([decipher.update(encrypted), decipher.final()]).toString("utf8");
    }
}
exports.CryptoBox = CryptoBox;
