Encryption, Hashing, and Integrity
Tell encryption, encoding, hashing, and signing apart, and choose the right primitive for confidentiality, integrity, and authenticity.
By the end, you’ll be able to…
- Distinguish encryption, encoding, hashing, and digital signatures.
- Explain symmetric and asymmetric encryption and when each is appropriate.
- Use hashing and HMAC to verify integrity and authenticity.
- Recognise common cryptographic failures such as nonce reuse and rolling your own crypto.
01Four Different Jobs
Encoding transforms data into a different representation without a secret. Base64 is encoding. It is useful for transporting binary data through text channels, but it provides no confidentiality. Anyone can decode it. A common mistake is treating a Base64 string as if it were encrypted.
Encryption transforms plaintext into ciphertext using a key. Only someone with the correct key should be able to recover the plaintext. Hashing maps arbitrary input to a fixed-size digest. It is one-way by design: you cannot recover the input from the digest, and a good hash makes it impractical to find two inputs with the same digest. A digital signature uses a private key to prove that a message came from the holder of that key and was not altered.
These primitives solve different problems. Use encryption for confidentiality, a hash for integrity checks and password storage, a keyed hash (HMAC) for integrity plus authenticity between parties who share a secret, and a signature for public verifiability. Using the wrong primitive can leave data exposed even when it looks protected.
ENCODING no secret reversible by anyone Base64, hexENCRYPTION secret key reversible with the key AES-GCM, ChaCha20-Poly1305HASHING no key one-way, fixed size SHA-256, SHA-3HMAC shared key integrity + authenticity HMAC-SHA-256SIGNATURE private key public verification Ed25519, ECDSA, RSA-PSS02Symmetric Encryption and Authenticated Modes
Symmetric encryption uses the same key for encryption and decryption. AES is the standard choice. It is fast and well studied. But a block cipher needs a mode of operation, and not every mode provides integrity. Electronic Codebook (ECB) leaks patterns because identical plaintext blocks produce identical ciphertext blocks. It should not be used for new designs.
Use an authenticated encryption mode such as AES-GCM or ChaCha20-Poly1305. These modes protect confidentiality and detect tampering. They require a unique nonce for every encryption under the same key. Reusing a nonce with the same key can catastrophically break confidentiality and integrity. Generate nonces with a cryptographically secure random source, store them alongside the ciphertext, and never reuse them.
Key management is the hard part. A strong algorithm with a key stored in a world-readable file is not secure. Keys need generation from a secure random source, restricted access, rotation, backup, and destruction. Application code should call a well-tested library rather than implementing padding, modes, or key derivation from scratch.
03Hashing and HMAC for Integrity
A cryptographic hash produces a fixed-size digest from arbitrary input. SHA-256 is a common general-purpose hash. SHA-3 is another standard family. MD5 and SHA-1 are broken for collision resistance and should not be used for security decisions. A hash can verify that a downloaded file matches a published value, but only if the published value itself is trustworthy.
A plain hash does not prove who created the message. Anyone can compute the hash of a modified file. HMAC combines a hash with a shared secret, so parties who know the key can verify both integrity and authenticity. HMAC-SHA-256 is widely used for webhook signatures, API request signing, and token validation. Compare HMAC values in constant time to avoid timing leaks.
Hashing is also used for password storage, but not with a fast general-purpose hash alone. Passwords need a deliberately slow, salted key-derivation function such as Argon2id, scrypt, or bcrypt. We will cover that in the next lesson. For data integrity, use hashes and signatures; for passwords, use a password hashing function. The distinction matters.
04Asymmetric Cryptography and Trust
Asymmetric cryptography uses a key pair: a public key that can be shared and a private key that must remain secret. Encryption with the public key can only be decrypted with the private key. A signature created with the private key can be verified by anyone with the public key. This solves key distribution and enables digital signatures, certificate authorities, and secure key exchange.
Asymmetric operations are slower than symmetric ones, so real protocols use both. TLS uses asymmetric cryptography to authenticate the server and agree on a shared session key, then uses symmetric encryption for the actual traffic. That hybrid design gives the security properties of public-key cryptography with the performance of symmetric ciphers.
Trust is the remaining problem. A public key is only useful if you know it belongs to the right party. Certificates bind a public key to a name and are signed by a certificate authority. The whole chain depends on trust anchors in the operating system or browser, on the certificate authority's operational security, and on the client checking the name and validity period. Cryptography does not eliminate the need to decide whom to trust.
The ideas worth keeping.
- Encoding is not encryption; hashing is not encryption; a plain hash is not a signature.
- Use authenticated encryption modes and never reuse a nonce with the same key.
- HMAC provides integrity and authenticity between parties that share a secret.
- Key management and trust decisions are as important as the algorithm.
Make it yours.
Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.
Exercise 1: Choose the right primitive
For each requirement, choose encoding, encryption, hashing, HMAC, or a digital signature, and explain why: (A) verify a downloaded installer has not changed, (B) store a password, (C) send a confidential message to a recipient, (D) prove an API request came from a trusted client that shares a secret, (E) include binary data in a JSON document.
Explore a worked solution
A: Hashing, ideally with a published signature or trusted checksum to protect the digest itself. B: A password hashing function such as Argon2id, not a fast hash. C: Public-key encryption or, more commonly, a hybrid protocol such as TLS or an age-style envelope. D: HMAC with the shared secret. E: Encoding such as Base64, because JSON is text and encoding is not encryption.The requirement determines the primitive. Encoding is not confidentiality, and a plain hash is not authenticity.
Exercise 2: Spot the crypto mistakes
A developer stores AES keys in the source repository, uses AES-ECB, reuses a nonce with AES-GCM, and compares HMAC values with ==. Identify each mistake and the consequence.
Explore a worked solution
Keys in source: anyone with repository access, including history, can decrypt data; rotate the key, move it to a secret manager, and purge history. AES-ECB: identical plaintext blocks produce identical ciphertext, leaking patterns; use an authenticated mode such as AES-GCM. Nonce reuse with GCM: can reveal plaintext relationships and break integrity; generate a unique nonce per encryption. Comparing HMAC with ==: may leak timing information; use hmac.compare_digest.Each mistake is operational, not theoretical. The fix is usually to use a high-level library correctly and keep keys out of code.
What stayed with you?
Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.
Keep exploring.
The official references behind this lesson, when you want a second angle or the full details.
One lesson closer.
Reading is a start. Trying it yourself makes the connection.