Skip to main content
Foundations: web, servers, containersLesson 4 of 13

Cryptography a Developer Needs

You rarely write crypto code, but you configure it every day: TLS, JWTs, SSH keys, signed images, secrets in a KMS. Follow one message from a sender to a receiver and see which tool guards which property, and how each one breaks when misused.

Tool—
Guarantees—
Key—
You meet it in—
SenderHas a messageNetworkAttacker can readReceiverWants to trust itSHA-256Hash · no keyHMACHash + shared keyAES-GCMOne key, both waysPrivate keyNever leaves ownerECDHEKey exchangePublic keyShare with anyonePassword DBSlow hashes onlyMessageMaybe changed
RequestResponse / reportWatchAppears in a later step

What you will learn

  • A hash checks integrity but anyone can recompute it
  • HMAC and AES-GCM: one shared key for integrity and secrecy
  • ECB mode, nonce reuse, MD5 and SHA-1
  • Signatures: sign with the private key, verify with the public key
  • Why passwords are hashed slowly, never encrypted

Which tool answers which question​

ToolIntegritySecrecyWho sent itKeyWhere you see it
Hash (SHA-256)Yes, if the digest is trustedNoNoNoneImage digests, file checksums
HMACYesNoSomeone holding the shared keyOne shared secretWebhook signatures, JWT HS256, API request signing
AEAD (AES-GCM, ChaCha20-Poly1305)YesYesSomeone holding the shared keyOne shared secretTLS records, disk and database encryption, KMS data keys
Signature (Ed25519, ECDSA, RSA)YesNoThe private key holder, checkable by anyonePrivate signs, public verifiesJWT RS256/ES256, cosign, SSH, certificates
Key exchange (ECDHE)—Makes a shared keyOnly with a signature on topThrowaway key pairsTLS 1.3, SSH
Password hash (Argon2id, scrypt, bcrypt)Checks a guess——Per-user salt, cost settingsUser login databases
MistakeWhat goes wrongDo instead
Homemade scheme or protocolSubtle flaws that tests never catchUse a vetted library's high-level API, and TLS for transport
ECB modeEqual blocks give equal ciphertext, patterns leakUse an AEAD mode (GCM)
Reusing a GCM noncePlaintext XOR leaks, tags can be forgedRandom 96-bit nonce or counter per message, rotate keys
MD5 or SHA-1 for securityCollisions let an attacker swap contentSHA-256 or better
Encrypting or fast-hashing passwordsA key leak or a GPU recovers themArgon2id, scrypt or bcrypt with a salt
== to compare MACsTiming leaks how many bytes matchedConstant-time compare

Try it yourself​

echo -n 'hello' | openssl dgst -sha256 # Hash: same input, same digest
echo -n 'hello!' | openssl dgst -sha256 # One character changes everything
echo -n 'payload' | openssl dgst -sha256 -hmac 'secret' # HMAC with a shared key

openssl genpkey -algorithm ed25519 -out key.pem # Private key
openssl pkey -in key.pem -pubout -out pub.pem # Public key derived from it
echo 'release v1' > msg.txt
openssl pkeyutl -sign -inkey key.pem -rawin -in msg.txt -out msg.sig # Sign with private
openssl pkeyutl -verify -pubin -inkey pub.pem -rawin -in msg.txt -sigfile msg.sig # Verify with public

openssl s_client -connect example.com:443 -tls1_3 </dev/null 2>/dev/null | grep -iE 'cipher|temp key|group' # AEAD cipher and key exchange group (X25519, or hybrid X25519MLKEM768)
ssh-keygen -t ed25519 -f ./demo_key -N '' && ssh-keygen -lf ./demo_key.pub # SSH key pair and its SHA256 fingerprint
htpasswd -nbBC 12 alice 'correct horse' # bcrypt hash, cost 12 ($2y$12$...)

Check yourself

  1. A file is published with its SHA-256 digest on the same server. An attacker controls that server. What does the digest protect against?
  2. Why do JWTs for many APIs use RS256 or ES256 instead of HS256?
  3. An attacker records TLS 1.3 traffic today and steals the server's private key next year. What can they decrypt?

Further reading: OWASP Cryptographic Storage Cheat Sheet · OWASP Password Storage Cheat Sheet · RFC 8446: TLS 1.3 · Encrypting Secret Data at Rest (Kubernetes) · HTTPS and Certificates · How Logging In Works (Sessions, JWT, OIDC)