• Not Exported

    Decrypts previously encrypted data

    Parameters

    • encryptedData: string

      Encrypted data with authentication tag

    • iv: string

      Initialization vector in hex format

    Returns string

    Decrypted text content

    Error when encrypted data is invalid

    function decrypt(encryptedData: string, iv: string): string {
    const key = getEncryptionKey();
    const ivBuffer = Buffer.from(iv, "hex");

    // Validate encrypted data
    if (!encryptedData) {
    throw new Error("Encrypted data is undefined or empty");
    }

    // Extract data and authentication tag
    const encryptedParts = encryptedData.split(":");
    const encrypted = encryptedParts[0];
    const authTag = Buffer.from(encryptedParts[1], "hex");

    const decipher = crypto.createDecipheriv(ALGORITHM, key, ivBuffer);
    decipher.setAuthTag(authTag);

    let decrypted = decipher.update(encrypted, "hex", "utf8");
    decrypted += decipher.final("utf8");

    return decrypted;
    }