Cryptography basics: hashes, symmetric and public-key encryption, and signatures
The four primitives security work depends on, what each guarantees, and the common mistakes: encoding mistaken for encryption, fast hashes for passwords, and encryption without integrity.
Diagram
Which primitive answers which question
Step 1 of 4
Hash
Has this file changed? SHA-256 gives a fixed-size fingerprint.
sha256sum release.tar.gz 9f2c...e41a release.tar.gz
Treated as secret; anyone can decode it
Billions of guesses per second on a GPU
Ciphertext can be altered undetected
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Encoding is not encryption
- What fails
- Base64, hex, and URL encoding change representation without a key.
- Who is affected
- Anyone whose “encrypted” token is just encoded.
- What you would notice
- A cookie or config value that decodes to readable JSON.
- What you change
- Use authenticated encryption with a managed key, or do not store the secret client-side.
Passwords need slow hashes
- What fails
- General hashes are designed to be fast, which helps guessing.
- Who is affected
- Every user if the password table leaks.
- What you would notice
- SHA-256 or MD5 hashes in a users table with no salt column.
- What you change
- Migrate to Argon2id or bcrypt on next login.
Keys are the system
- What fails
- Strong algorithms with a key in the repository protect nothing.
- Who is affected
- Everyone whose data that key protects.
- What you would notice
- A key in source control or an environment file copied between hosts.
- What you change
- Store keys in a KMS or HSM, rotate them, and log their use.
What each property means
Confidentiality means only key holders can read the data. Integrity means changes are detected. Authenticity means you know who produced it. Non-repudiation, usually from signatures, means the signer cannot plausibly deny it. Choose the primitive by the property you need, not by name recognition.
Try each primitive in a terminal
OpenSSL and standard tools show the behaviors directly. Change one byte of a file and watch the hash change completely, which is the avalanche effect. Encrypt with a passphrase and see that the output is different each time because of a random salt and nonce.
$ printf hello | sha256sum 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 - $ printf hellp | sha256sum 0d0f5a3c... - # one letter changed, entirely different digest $ printf hello | base64 aGVsbG8= # reversible by anyone
Where you meet all four at once
A TLS 1.3 handshake uses ECDHE for key exchange, a certificate signature to prove the server’s identity, HKDF built on a hash to derive keys, and AES-GCM or ChaCha20-Poly1305 for the records. Most cryptographic failures in practice are configuration and key handling, not broken math.
Check yourself
Recognition and the fix. Nothing here is a procedure.
A session cookie decodes from Base64 into {"user":"ana","role":"admin"}. What is the problem?
Go deeper
Specific techniques, exercises, and glossary cards for this topic.
- 01Slow password hashesA password database will be copied eventually. Includes the diagram and a defensive fix.
- 02TLS versions and the certificate inventoryA connection is only as modern as the versions you still accept, and only as named as the certificate you can find. Includes the diagram and a defensive fix.
- 03How password hashes are cracked, and what makes stored passwords resist itOffline cracking turns a stolen hash database into passwords using dictionaries, rules, and masks. Includes the diagram and a defensive fix.
- 04Hash functions and collisions: what MD5 and SHA-1 breaks actually meanA cryptographic hash should make collisions and preimages infeasible. Includes the diagram and a defensive fix.
- 05Side-channel attacks: timing, cache, power, and why constant-time code mattersSide channels leak secrets through how long, how loudly, or how much power a computation takes. Includes the diagram and a defensive fix.
- 06Cryptanalysis in practice: why real systems break at keys, modes, and randomnessModern ciphers like AES are rarely broken directly; systems fail through reused nonces, ECB mode, weak randomness, and poor key handling. Includes the diagram and a defensive fix.
- 07Auditing TLS: protocol versions, cipher suites, and certificates with testssl.shHistoric TLS bugs (POODLE, BEAST, Heartbleed, ROBOT) mostly come down to old protocol versions, weak ciphers, or unpatched libraries. Includes the diagram and a defensive fix.
- 08Downgrade attacks: forcing weaker protocols and how modern TLS prevents itA downgrade pushes two parties onto an older, weaker protocol or cipher than both support. Includes the diagram and a defensive fix.
- 09SAML security: signature validation, XML wrapping, and Golden SAMLSAML single sign-on depends on the service provider validating signed XML assertions correctly. Includes the diagram and a defensive fix.
- 10Password policy that follows NIST SP 800-63B: length, screening, and no forced rotationModern guidance favors long passphrases, screening against breached passwords, no arbitrary complexity rules or scheduled rotation, and password managers plus MFA. Includes the diagram and a defensive fix.
- 11Password storage, and what cracking tools proveJohn the Ripper and Hashcat test whether a stored secret is guessable. Includes the diagram and a defensive fix.
Exercises
- 01Password policy and hash cost explorerChange length, character sets, and hash algorithm, and watch the offline guessing time move. Then test which policy-compliant passwords fall first anyway.
- 02Password hashing and salting visualizerSee what a leaked user table reveals with no salt, one site-wide salt, and per-account salts, using real SHA-256 hashes computed in your browser.
- 03TLS certificate chain readerRead five certificate chains the way a client does: hostname, dates, issuer links, and trust. Find the expired leaf, the missing intermediate, and the bad wildcard.
- 04Terminal mission: Same rows, different hashFinance’s vendor file does not match the hash the bank sent, so someone suspects tampering. Compare the bytes and show that line endings, not edits, changed the hash.