Paths / Security Basics / Lesson 4

Passwords and Hashing

Video + 10 min read · includes a lab

Customer
shop.example203.0.113.10
Password database
Attacker
They can read:sara: sunshine1 bilal: tiger2024
Log in
Copy of database
Passwords and Hashing
0:00 / 4:14 · Passwords and Hashing
Chapters and transcript

Passwords and Hashing Every day, customers log in to this online shop. The website keeps their passwords in a database. Now imagine that database leaks. If the passwords were saved as plain text, the attacker can read every single one, at once. So how should a website store your password? Let us find out.

What is a hash? Good websites never store your real password. They store a hash of it. A hash function turns any input into a fixed-length fingerprint. For example, SHA-256 turns the word hello into this long string of letters and numbers. And that string is what the website keeps.

Hashing in action Let me show you on the laptop. I hash the word hello with SHA-256, and get this long fingerprint. Now I hash Hello, with a capital H. Only one letter changed, but look at the result. The hash is completely different. And if I hash hello again, I get the first hash back, exactly the same.

What is a hash? That demo shows the three key properties of a hash. It is one-way: you cannot turn the hash back into the password. The same input always gives the same output. And a tiny change gives a totally different output, so hello and Hello look nothing alike.

Logging in So how does login work if the website only has a hash? You type your password. The website hashes what you typed. Then it compares that hash with the stored one. If the two match, you are in. The website never needs to keep your real password.

Cracking hashes If a hash cannot be reversed, how are passwords cracked? Attackers guess. They take a wordlist of likely passwords. They hash each guess and compare it with the leaked hash. No match, so they try the next one. Match! The password was I love you, written as one word. This is a dictionary attack, and common passwords like this fall in seconds.

Cracking hashes There are two main ways to guess. A dictionary attack uses lists of real passwords that people have used, so common passwords fall fast. A brute-force attack tries every possible combination instead. It works on short passwords, but it becomes impossibly slow as passwords get longer. That is why length matters so much.

How developers defend Developers defend in four ways. Salting adds a random value to each password before hashing, so two users with the same password get different hashes. Slow algorithms made for passwords, like bcrypt, scrypt or Argon2, make every guess expensive. Fast ones like MD5 are not suitable. Rate limiting blocks repeated logins. And two-factor authentication means a cracked password is not enough.

Quick check Quick check. Two users on the same website both choose the password sunshine1. The website uses a salt for each password. What is true about their stored hashes? Take a moment, then choose.

Credential stuffing Now see why reuse is dangerous. One website leaks, and the attacker gets your email and password. They try the same pair on your email account. You reused it there, so they are in. On the shopping site, you used a unique password, so the try fails. This is called credential stuffing.

How you defend You can defend too. Use long passwords. A passphrase of random words, like river candle yellow orbit, beats a short clever one like P at s s w zero r d. Never reuse passwords. Use a password manager to create and remember unique ones. And turn on two-factor authentication wherever you can.

Putting it together Let us put it together. Websites store a hash, not your password. A hash is one-way, so attackers crack it by guessing with wordlists. Developers defend with salts, slow hashes, rate limits and two-factor. You defend with long, unique passwords, a password manager and two-factor. Now read the lesson below, then try the lab, where you will crack a leaked hash yourself.

Passwords are still the main way we log in. In this lesson you will learn how websites store them and why weak passwords are cracked so quickly.

Websites should never store your real password

If a website saved your password as plain text and its database leaked, every password would be exposed instantly. Instead, good websites store a hash of your password.

What is a hash?

A hash function turns any input into a fixed-length "fingerprint". For example, with SHA-256:

hello   → 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Hello   → 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

Hash functions have three key properties:

  • One-way: you cannot turn the hash back into the password
  • Same input, same output: hello always gives the same hash
  • Tiny change, totally different output: hello and Hello look nothing alike

When you log in, the website hashes what you typed and compares it with the stored hash. If they match, you are in.

How attackers crack hashes

If hashes cannot be reversed, how are passwords cracked? Attackers guess:

  1. Take a list of likely passwords (a wordlist)
  2. Hash each guess
  3. Compare each result with the leaked hash

This is a dictionary attack. Leaked password lists contain millions of real passwords that people have used, and fast computers can test billions of guesses per second against weak hashes. That is why pakistan123 or iloveyou falls in seconds.

A brute-force attack tries every possible combination instead. It works on short passwords, but becomes impossibly slow as passwords get longer.

How developers defend

  • Salting: add a random value (the salt) to each password before hashing, so two users with the same password get different hashes
  • Slow hash algorithms: use algorithms designed for passwords, such as bcrypt, scrypt or Argon2, which make each guess expensive. Fast algorithms like MD5 or plain SHA-256 are not suitable for passwords.
  • Rate limiting: block repeated login attempts
  • Two-factor authentication: even a cracked password is not enough on its own

How you defend

  • Use long passwords. A passphrase like river-candle-yellow-orbit beats P@ssw0rd!
  • Never reuse passwords. When one site leaks, attackers try the same password everywhere (this is called credential stuffing)
  • Use a password manager to create and remember unique passwords
  • Turn on two-factor authentication wherever you can

Now try it yourself: in the lab, you will crack a leaked hash using a wordlist, exactly the way attackers do.

HANDS-ON LAB

Crack the Hash

A password hash leaked from a database. Find the password behind it.

Start the lab →

Check your understanding

Answer every question correctly to complete this lesson. If you miss one, try the quiz again.

1. Why do websites store a hash instead of your actual password?

2. What is a salt?

3. Which password is strongest?

Locked: answer every quiz question above correctly to complete this lesson.

Spotted a mistake or something unclear? Report a mistake