Learning path
M6

Crypto & Secrets

Cryptography fails not because the math is weak, but because it’s used wrong. Pick boring, standard primitives — then protect the keys like the crown jewels they are.

Don’t roll your own crypto

Use vetted, standard libraries and primitives — libsodium, your platform’s crypto, a managed KMS. Hand-built ciphers and clever schemes fail in ways only an expert (or attacker) sees. Your job is to use crypto correctly, not invent it.

Use this, not that

The right primitive for each job. When in doubt, choose the modern AEAD/curve and move on.

Symmetric encryption

Use

AES-256-GCM, ChaCha20-Poly1305

Avoid

DES, 3DES, RC4, AES-ECB

Use AEAD — it encrypts and authenticates in one step.

Hashing (integrity)

Use

SHA-256, SHA-3, BLAKE2

Avoid

MD5, SHA-1

MD5 and SHA-1 are collision-broken — never for integrity or signatures.

Password storage

Use

Argon2id, scrypt, bcrypt

Avoid

Plain SHA/MD5, unsalted, fast hashes

Password hashes must be slow and salted to resist cracking.

Asymmetric & signatures

Use

Ed25519, ECDSA P-256, RSA-2048+

Avoid

RSA-1024, custom curves

Stick to vetted curves and key sizes; small keys are forgeable.

Key exchange

Use

X25519, ECDHE (forward secrecy)

Avoid

Static RSA key exchange

Ephemeral exchange keeps past traffic safe if a key later leaks.

Randomness

Use

CSPRNG: secrets, /dev/urandom

Avoid

Math.random(), rand(), time-seeded

Predictable randomness breaks keys, tokens, and nonces.

Common crypto bugs

Right primitive, wrong usage. These are where real systems leak.

Nonce / IV reuse

Reusing a nonce with the same key leaks plaintext and can forge messages.

Fix

Unique random nonce per message; never reuse a key+nonce pair.

ECB mode

Identical plaintext blocks produce identical ciphertext — patterns leak.

Fix

Use an AEAD mode (GCM/Poly1305); never ECB.

Encrypt without authenticate

Plain CBC with no MAC enables tampering and padding-oracle attacks.

Fix

Use authenticated encryption (AEAD) so tampering is detected.

Hardcoded keys

A key in source or an image is readable by anyone who gets the artifact.

Fix

Load keys from a vault/KMS at runtime; rotate on exposure.

Non-constant-time compare

Comparing secrets with == leaks them byte-by-byte via timing.

Fix

Use a constant-time comparison for tokens, MACs, and hashes.

Encoding ≠ encryption

Base64 and hex are reversible by anyone — they protect nothing.

Fix

Encrypt with a real cipher and a managed key; don’t confuse the two.

Protect data in transit (TLS)

  • Require TLS 1.2+ everywhere; prefer TLS 1.3.
  • Disable old protocols and weak ciphers (no SSLv3, RC4, export ciphers).
  • Validate certificates — never disable verification “to make it work”.
  • Enable HSTS; redirect all HTTP to HTTPS.
  • Use mTLS for service-to-service trust internally.

Secrets management

The strongest cipher is worthless if the key sits in a repo. Treat secrets as their own asset class.

Never in code

No secrets in source, config, container images, or logs.

Vault & scope

Store in a secrets manager/KMS; grant the narrowest access.

Rotate regularly

Rotate on a schedule and immediately on any suspicion.

Prefer short-lived

Dynamic, short-TTL credentials beat long-lived static keys.

Scan every commit

gitleaks/trufflehog in pre-commit and CI catch leaks early.

Leaked = burned

A secret that touched a public place is compromised — rotate, don’t hide.

Where this shows up in the wild

Weak crypto and leaked keys are a top OWASP risk class. Next: see Cryptographic Failures alongside the rest of the Top 10.

Go to OWASP Top 10 →