1000-ft view: crypto goes wrong when we choose weak algorithms, reuse IVs/nonces, or skip authentication and key lifecycle work. These secure demos show Password hashing, Token entropy, AES-GCM at rest, IV reuse avoidance, and Key versioning/rotation.
Uses shared DB tables (a02_*). Seed data lives in db/seed.sql.
Compare with the Insecure version.
Secure idea: store only slow password hashes
(e.g., PASSWORD_DEFAULT), verify with password_verify(), and
rehash on upgrade with password_needs_rehash().
Secure idea: use a CSPRNG for tokens:
$tok = bin2hex(random_bytes(32)). Store and scope tokens (purpose, TTL, user).
source='secure' to a02_api_tokens.Secure idea: encrypt sensitive fields with an
AEAD cipher (AES-256-GCM). Use a random IV per record and store cipher, iv, tag,
and key_version. Decrypt only when necessary.
123-45-6789) and save — requires login.Secure idea: for GCM, generate a new random 96-bit IV each time. Identical plaintexts should produce different ciphertexts (and always include an auth tag).
Secure idea: track a key_version with each ciphertext.
Keep an active key and a registry of old keys. Rotation => set a new active version, then re-encrypt rows.