Backend & Full Stack Engineer Guide: Applied Cryptography with AES-256-GCM, Web Crypto API, HMAC Webhook Signatures & PostgreSQL pgcrypto
Whether protecting zero-knowledge digital wills (like our Wasiat platform), verifying payment webhook signatures, or encrypting tenant API credentials at rest, a Senior Backend Engineer and Full Stack Engineer must apply authenticated encryption and constant-time signature verification correctly.
1. Authenticated Envelope Encryption with AES-256-GCM in Python
Never use unauthenticated block cipher modes (like AES-CBC without an HMAC) or static IVs. Below is the production Python pattern using `cryptography` (`AESGCM`) with a fresh 96-bit CSPRNG nonce per encryption operation and Associated Authenticated Data (`aad`) bound to the tenant and record ID:
import os
import base64
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def encrypt_tenant_secret(master_key_32b: bytes, tenant_id: str, plaintext: str) -> str:
aesgcm = AESGCM(master_key_32b)
nonce = os.urandom(12) # 96-bit NIST-recommended GCM nonce
aad = f"tenant:{tenant_id}".encode("utf-8")
ciphertext = aesgcm.encrypt(nonce, plaintext.encode("utf-8"), aad)
return base64.urlsafe_b64encode(nonce + ciphertext).decode("ascii")
def decrypt_tenant_secret(master_key_32b: bytes, tenant_id: str, token_b64: str) -> str:
raw = base64.urlsafe_b64decode(token_b64.encode("ascii"))
nonce, ciphertext = raw[:12], raw[12:]
aesgcm = AESGCM(master_key_32b)
aad = f"tenant:{tenant_id}".encode("utf-8")
return aesgcm.decrypt(nonce, ciphertext, aad).decode("utf-8")
2. Constant-Time Webhook Signature Verification (`hmac.compare_digest`)
When validating incoming payment or GitHub/Slack webhooks in a backend API, never compare digest strings with `==`, which leaks timing side-channel information. Always compute the expected `HMAC-SHA256` over the raw request bytes + timestamp header and compare via `hmac.compare_digest(expected, received)`.
3. Related Backend & Full Stack Engineering Guides
- Hire a Senior Backend Engineer: Python, Django, FastAPI, Golang & PostgreSQL Pillar Guide
- Hire a Senior Full Stack Engineer: React, Next.js 15, TypeScript & Client-Side Security
⚡ Hire Aldian Fazrihady — Senior Cloud, GCP, Backend, AI Application & Full Stack Engineer
Need production cloud architecture, GCP/AWS infrastructure, high-throughput backend systems, AI agents, or full stack web engineering? Book a 1-Day Programming Sprint for $100 USD (International Card / Apple Pay) or Rp 1,000,000 (Indonesian QRIS / Bank Virtual Account), or hire directly on Upwork:
Book $100 / Rp 1M 1-Day Sprint → Hire on Upwork (100% Job Success) →