Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 31 ยท Password Security

Password Storage and Hashing

Learn how websites should store passwords: hashing versus encryption, salts, slow hashes such as bcrypt, scrypt and Argon2, and why fast hashes fail.

When a site gets breached, the most valuable thing in the stolen database is often the table of passwords. How those passwords were stored decides whether the attacker gets a list of working logins within minutes or a pile of data that is nearly worthless. Security professionals care about this from both sides: defenders must store passwords correctly, and testers must be able to explain why a given storage method is weak.

This lesson explains what a password hash is, why encryption is the wrong tool, what a salt does, and why password hashing must be deliberately slow. It is the first lesson of the Password Security chapter, and it is purely defensive: nothing here cracks anything.

Hashing is not encryption

Encryption is reversible: with the key you get the original text back. Hashing is a one-way function: you feed in data and get a fixed-size fingerprint, and there is no key that turns the fingerprint back into the input.

For passwords you want one-way. The server never needs to know your password, only whether what you typed now produces the same fingerprint as what you typed at sign-up. If the database stored encrypted passwords, anyone who stole the key (often stored on the same server) would get every password at once.

Encrypt or hash?

Encryption Reversible with a key. For data you must read again.
Hashing One-way fingerprint. For checking, not reading.

Passwords are checked, never read.

1 / 2

A hash has useful properties: the same input always gives the same output, a tiny change in the input changes the output completely, and the output length is fixed. Run the example to see them with SHA-256 from Python's standard hashlib. SHA-256 is a real, widely used hash, but as you will see, it is the wrong choice for passwords on its own.

Python What a hash looks like

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.

All outputs have the same length, and password and Password have nothing visibly in common.

Why a plain hash is not enough

Suppose a site stores sha256(password). Two problems appear at once.

Identical passwords give identical hashes. If thousands of users chose 123456, they all share one hash, and an attacker who works out one of them knows all of them. Seeing which hashes repeat also reveals which users picked common passwords.

Precomputation. The attacker can hash a huge list of likely passwords once and store the results in a lookup table (or a more compact structure called a rainbow table). After that, every leaked database is "cracked" by lookup, not by computation.

The fix for both is a salt: a random value, unique per user, mixed into the password before hashing and stored next to the hash. It is not secret. Its job is to make the same password hash differently for every user and to make precomputed tables useless, because the attacker would need a separate table for every salt.

Python Salts make equal passwords differ

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 first block prints the same hash twice. The second block prints different hashes (the salts are random, so the exact digits change on every run, but they never match).

Why fast hashes are bad for passwords

SHA-256, SHA-1 and MD5 were designed to be fast, because they are used to check files and signatures. A modern graphics card can compute billions of SHA-256 hashes per second. If an attacker steals a salted SHA-256 database, salts stop precomputation but not simple guessing: they try the most likely passwords against each user at full speed.

Password hashing therefore wants the opposite property: slow, and costly to run in parallel on special hardware. Purpose-built password hashing functions have a tunable work factor:

  • PBKDF2 repeats a hash many times (the iteration count). Widely standardized and available everywhere, but cheap to parallelize on GPUs.
  • bcrypt (1999) has a cost parameter and uses a lot of small memory accesses, which is awkward on GPUs. Passwords are limited to 72 bytes.
  • scrypt needs a large amount of memory by design, which makes mass guessing expensive.
  • Argon2 won a public password hashing competition in 2015. Argon2id is the usual recommendation today, with settings for memory, iterations and parallelism.

Do not invent your own scheme. Use the password API your framework or language provides. In PHP that is password_hash(); in many other languages it is a maintained Argon2 or bcrypt library. These libraries create the salt, store the parameters inside the result string, and make verification a single call.

A stored bcrypt value looks like this (shown as read-only text, an example only):

TEXT
$2b$12$KIXQJ0Zf3m8Qk0hQ1u6kPeQj1m7v0y4l8x2r5s9t3u6w1y4z7a0bC
 |  |  |
 |  |  +-- salt and hash, encoded together
 |  +----- cost factor (2^12 rounds)
 +-------- algorithm version

Because the algorithm, cost and salt live inside the string, you can raise the cost later: on the next successful login, verify with the old parameters, then store a new hash with stronger ones. This is called rehashing on login.

Feeling the work factor

Python's hashlib includes PBKDF2 in normal installations, but the in-browser Pyodide build used by this site does not provide pbkdf2_hmac. The next stand-in builds the same "repeat the hash" idea by hand, with plain SHA-256 in a loop, so you can see cost scale with iterations. It is a teaching model, not a production function: real PBKDF2 mixes in the password at every round using HMAC, which this loop does not.

Python Cost grows with the iteration count

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 seconds depend on your computer, so they differ from run to run, but the trend is clear: the cost grows in proportion to the iteration count. A legitimate user pays that cost once per login. An attacker pays it for every single guess.

Practical rules

  • Hash passwords on the server with Argon2id, bcrypt or scrypt through a well-maintained library.
  • Let the library create a unique random salt; never reuse salts or use the username as a salt.
  • Choose a cost so one hash takes roughly a tenth of a second or more on your production hardware, and review it as hardware improves.
  • Compare hashes with the library's verify function, which takes care of constant-time comparison.
  • Never log passwords, never email them, and never store them reversibly.
  • Consider a pepper: a secret value kept outside the database (in an application secret store) and mixed in. It helps if only the database leaks, but it is an addition to a good hash, not a replacement.
  • Do not use MD5, SHA-1 or unsalted SHA-256 for passwords.

On Kali, testers meet this topic when they review a storage design or assess the strength of a leaked hash type in an authorized engagement. Tools such as hashid can suggest which algorithm produced a given string. That is identification, not cracking. Strength estimates come next in Password Strength, Entropy and Cracking Resistance.

What is real and what is the stand-in

SHA-256, salts, bcrypt strings, scrypt and Argon2 are real and used as described. The loop in the last example is a simplified stand-in for PBKDF2 because the browser runtime lacks pbkdf2_hmac; do not copy it into production code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are passwords hashed instead of encrypted?

    Verification does not require reading the password. A one-way function removes the risk of a single key unlocking every password.
  2. 2. What problem does a per-user random salt solve?

    A unique salt means equal passwords produce different hashes and each salt would need its own lookup table. The salt itself is not secret.
  3. 3. Why is unsalted or lightly salted SHA-256 a poor choice for password storage?

    SHA-256 is sound as a hash but built for speed. Password hashes need an adjustable, expensive work factor.
  4. 4. Which set contains only algorithms designed for password hashing?

    bcrypt, scrypt and Argon2id are slow, tunable password hashing functions. The others are fast hashes or encryption algorithms.

Summary

  • Hashing is one-way and suits password checking; encryption is reversible and does not.
  • A salt is unique per password, stored with the hash, and defeats precomputed tables and duplicate detection.
  • General-purpose hashes are fast, which helps attackers; password hashes are deliberately slow and tunable.
  • Use Argon2id, bcrypt or scrypt through a maintained library, raise the cost over time and rehash on login.
  • Never invent your own scheme or store passwords reversibly.

Next: Password Strength, Entropy and Cracking Resistance.