import crypto from "crypto";

const ALGORITHM = "aes-256-gcm";
const IV_LENGTH = 12;
const SECRET_KEY = crypto.createSecretKey(
  new Uint8Array(crypto.createHash("sha256").update("acm-secret-key").digest())
);

// encrypting entrypass url for security purpose
export function encryptAcmUrl(entrypassUrl: string) {
  const iv = crypto.randomBytes(IV_LENGTH);
  const cipher = crypto.createCipheriv(
    ALGORITHM,
    SECRET_KEY,
    new Uint8Array(iv)
  );

  const encrypted = Buffer.concat([
    cipher.update(entrypassUrl, "utf8"),
    cipher.final() as any,
  ]);

  const authTag = cipher.getAuthTag();

  return [
    iv.toString("base64"),
    authTag.toString("base64"),
    encrypted.toString("base64"),
  ].join(":");
}
