#!/bin/bash
ERROR_CODE="GATEWAY_TIMEOUT"
AEAD_KEY_HEX="deadbeef..." # 32-byte key, securely provided
NONCE_HEX=$(openssl rand -hex 12)

# Compute the hash of the error string
ERROR_HASH=$(printf "%s" "$ERROR_CODE" | sha256sum | awk '{print $1}')

# Encrypt the hash using AES-256-GCM and get ciphertext + tag
# Note: This is a simplified example. Real implementations should 
# handle outputs carefully.
ENCRYPTED_OUTPUT=$(echo -n "$ERROR_HASH" | openssl enc -aes-256-gcm \
  -K "$AEAD_KEY_HEX" -iv "$NONCE_HEX" -A "" | xxd -p -c 256)
CIPHERTEXT=${ENCRYPTED_OUTPUT::-32}
TAG=${ENCRYPTED_OUTPUT: -32}

# The final payload is nonce || ciphertext || tag
printf "%s%s%s" "$NONCE_HEX" "$CIPHERTEXT" "$TAG" > error.status
