#!/bin/bash
RECEIVED_PAYLOAD=$(cat error.status | xxd -p -c 256)
AEAD_KEY_HEX="deadbeef..." # 32-byte key, securely provided

# Parse the payload
NONCE_HEX=${RECEIVED_PAYLOAD:0:24}
CIPHERTEXT_HEX=${RECEIVED_PAYLOAD:24:-32}
TAG_HEX=${RECEIVED_PAYLOAD: -32}

# Attempt to decrypt
DECRYPTED_HASH=$(printf "%s" "$CIPHERTEXT_HEX" | xxd -r -p | openssl enc -d -aes-256-gcm -K "$AEAD_KEY_HEX" -iv "$NONCE_HEX" -A "" -T "$TAG_HEX")

# Precompute known error hashes
declare -A KNOWN_ERROR_HASHES
KNOWN_ERROR_HASHES[$(printf "%s" "GATEWAY_TIMEOUT" | sha256sum | awk '{print $1}')]="GATEWAY_TIMEOUT"
# ... add other known errors ...

# Check if the decrypted hash is a known error
if [[ -v KNOWN_ERROR_HASHES[$DECRYPTED_HASH] ]]; then
  echo "VERIFIED: The error code is '${KNOWN_ERROR_HASHES[$DECRYPTED_HASH]}'."
else
  echo "UNKNOWN_ERROR or AUTHENTICATION_FAILURE."
fi
