Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 38 ยท Cryptographic Failures and Injection

Hashing, Encryption and Password Storage in Practice

Learn the difference between hashing, encryption and encoding, why passwords need slow salted hashes, and see salts, iterations and constant-time comparison in runnable Python.

Three words are constantly confused: encoding, encryption and hashing. Mixing them up leads to real breaches, for example "encrypting" passwords with a key that sits in the same database, or "protecting" tokens with Base64.

Operation Reversible? Needs a key? Purpose
Encoding (Base64, URL encoding) Yes, by anyone No Represent data in another format. Not security.
Encryption (AES-GCM) Yes, with the key Yes Confidentiality: data you must read again later
Hashing (SHA-256, Argon2) No No (or a salt) Fingerprints and password verification
MAC / signature (HMAC, RSA, Ed25519) Not applicable Yes Integrity and authenticity

Passwords are hashed, not encrypted

A server does not need to know your password; it only needs to check that what you type now matches what you chose before. So it stores a hash of the password. If the database leaks, attackers should have to guess passwords one by one, not read them.

But not every hash is good for passwords.

Why plain SHA-256 is wrong for passwords

General hashes (MD5, SHA-1, SHA-256) are designed to be fast: billions per second on a graphics card. An attacker with a leaked database simply hashes a huge list of common passwords and compares. Two other problems:

  • No salt: identical passwords give identical hashes, so one guess cracks every user who chose it, and precomputed "rainbow tables" work.
  • Fast: millions of guesses per second.

The solution: salted, slow, adaptive hashes

  • A salt is a unique random value stored with each hash. It makes every hash different, even for equal passwords, and kills precomputed tables.
  • A work factor (iterations, memory) makes each guess expensive on purpose.

Use a purpose-built password hashing function:

Function Notes
Argon2id Current recommendation; tunable memory, time and parallelism
scrypt Memory-hard, also good
bcrypt Older and widely supported; limits input to 72 bytes
PBKDF2 Acceptable with very high iteration counts, often required by standards

Use your language's library (password_hash in PHP, argon2-cffi or bcrypt in Python, Spring Security's encoders, ASP.NET Identity) and let it pick the salt and format. Never write the loop yourself in production.

Python Unsalted fast hashes fall to a wordlist

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The result leaks two facts at once: the weak passwords are recovered instantly, and equal hashes reveal that two users chose the same password.

Salting and stretching

The stand-in below implements PBKDF2-HMAC-SHA256 from the standard hmac module (Python normally offers hashlib.pbkdf2_hmac; this browser runtime lacks it, so we write the loop by hand for teaching). Iteration counts are tiny here so it runs fast; real systems use hundreds of thousands.

Python Stand-in: salted, slow password hashing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Observe the three ideas: a random salt (so equal passwords differ), many iterations (so each guess costs time), and a stored format that records the parameters so you can raise them later. hmac.compare_digest compares in constant time so the response time does not reveal how many leading characters matched.

Pepper and upgrades

A pepper is an extra secret, kept outside the database (in a KMS or environment), mixed into the hash. It helps if only the database leaks, but adds key-management work. More important is upgrade support: store the algorithm and cost with each hash, and when a user logs in successfully, re-hash with the current settings if the old parameters are weaker.

Password policy that works

Current guidance (NIST SP 800-63B) favours length over complexity rules:

  • Minimum length of 8, allow at least 64 characters, accept spaces and all Unicode.
  • Do not force periodic changes without evidence of compromise.
  • Check new passwords against a list of breached and common passwords.
  • Offer MFA and passkeys.
  • Rate-limit logins and report suspicious attempts (covered under authentication failures later in the track).
Python A modern password checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what is absent: no demand for "one uppercase, one digit, one symbol". Those rules lead to Password1!, not to security.

When you really must encrypt

Some data (a payment token, a recovery email) must be read again, so encryption, not hashing, is right. Use authenticated encryption from a vetted library, keep keys in a KMS and rotate them. Store the ciphertext with its nonce and a key version so rotation works.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should passwords be hashed rather than encrypted?

    If the server can decrypt passwords, so can an attacker who steals the key. A one-way hash removes that option.
  2. 2. What does a salt do?

    Each password gets its own random salt, so equal passwords produce different hashes.
  3. 3. Which is the best choice for new password storage?

    Purpose-built password hashes are slow and salted. MD5 and SHA-256 are fast general hashes, and Base64 is just encoding.
  4. 4. Why use hmac.compare_digest instead of == for secrets?

    A constant-time comparison avoids leaking, through timing, how many leading bytes matched.

Summary

  • Encoding is not security, encryption is reversible with a key, hashing is one-way, and MACs prove integrity.
  • Store passwords with Argon2id, scrypt, bcrypt or PBKDF2: salted, slow and with recorded parameters.
  • Fast unsalted hashes fall to wordlists and reveal duplicate passwords.
  • Compare secrets in constant time and upgrade hashes at login.
  • Prefer long passwords, breach checks and MFA to composition rules.

Next: SQL Injection and Parameterized Queries