Course contents · Lesson 7 of 22

Encryption vs Hashing: How Passwords Stay Secret

The Web and Staying Safe · Lesson 7 of 22 · 8 min Foundations

You click "Forgot password", and instead of telling you your old password, the site emails a link to set a new one. That is not awkwardness: a well-built site cannot read your password, even though it checks it at every login. This lesson explains encryption vs hashing, two ways to protect data that work in opposite directions, and why your password gets the second.

Encryption: a locked box with a key

Encryption scrambles readable data, the plaintext, into ciphertext that looks like random noise. A key, a long secret number, controls the scrambling. With the right key you decrypt it back to exactly the original. Without it, recovering the data is impractical, even for powerful computers.

Symmetric encryption uses one shared key to lock and unlock. It is fast, so it protects big things: files, backups, phone storage and most web traffic. The most widely used method is AES (Advanced Encryption Standard). Its weak spot is sharing: both sides need the same key, and it must reach the other side without anyone copying it.

Asymmetric encryption, also called public-key encryption, uses a matched pair: a public key you can give anyone, and a private key you never share. What the public key locks, only the private key unlocks, like handing out open padlocks that only you can open. It is much slower, so it mostly handles small jobs, such as helping two strangers agree on a shared key. Lesson 8 shows HTTPS doing exactly that.

Hashing: a one-way fingerprint

A hash function turns any input, a word or a whole film, into a short, fixed-size value called a hash. SHA-256 (Secure Hash Algorithm, 256-bit) always produces 256 bits, written as 64 hexadecimal characters: 0 to 9 and a to f. There is no key: anyone who hashes the same input gets the same hash.

Three properties make a hash useful. The same input always gives the same hash. A tiny change, even one capital letter, gives a completely different hash. And it is one-way: no known method works backwards from a hash to its input, just as a fingerprint cannot rebuild a person. Finding two different inputs with the same SHA-256 hash is, as far as anyone knows, practically impossible too.

One-way does not mean unguessable. Anyone holding a hash can hash a guess and compare, the way police compare a print with a suspect's. That is hopeless for a long, unpredictable input, but for a short, common password it is the whole problem. In the diagram, notice that the encryption arrows go both ways, while hashing has no way back.

Two panels side by side. Left, encryption, a locked box: the message hello is encrypted with a key into scrambled text, and the same key decrypts it back to hello, so it is reversible with the key. Right, hashing, a fingerprint: hello is hashed with SHA-256, with no key, into a hash starting 2cf24dba5fb0a30e, and a crossed-out arrow shows there is no way back. Hello with a capital H gives a completely different hash starting 185f8db3, and every SHA-256 hash is 64 hex characters. The scrambled text is illustrative; the hashes are real
Encryption is a round trip with a key; hashing is a one-way trip with no key.

How websites store your password

A well-built site never stores your password, not even encrypted: the key would have to sit nearby to check logins, and a thief who stole both could unlock every account. It stores a hash instead. First it makes a salt, a random value unique to your account, and mixes it with your password. Then it runs a deliberately slow algorithm such as bcrypt or Argon2. The salt is not secret; it is saved beside the hash.

At login, the site repeats the recipe, as the flowchart shows. Your password arrives inside HTTPS encryption, so others on the Wi-Fi cannot read it. The site hashes what you typed with your salt and the same settings, then checks whether the result equals the stored hash.

Flowchart of a login. Step 1: you type your password, which is sent over HTTPS. Step 2: the site looks you up and finds your salt and your stored hash. Step 3: it runs a slow hash, bcrypt or Argon2, on the typed password plus the salt, giving a new hash. Step 4: it asks whether the new hash and the stored hash match. Yes means you are logged in; no means wrong password, and you can try again. A note says there is nothing to decrypt: the site keeps no copy of your password, only the salt and the hash
The site checks your password by hashing it again, never by unlocking a stored copy.

That is why "Forgot password" sends a reset link: the site kept nothing it could turn back into your password. A site that emails you your old password is storing it reversibly: a warning sign.

Why plain SHA-256 is not enough, and what salt adds

SHA-256 is built to be fast: ideal for checking files, terrible for passwords. Attackers who steal a database of hashes guess offline, on their own computers, where no login page can lock them out. A single modern graphics card can compute billions of SHA-256 hashes a second, so common passwords fall in moments.

Slow algorithms have a cost setting, such as bcrypt's work factor or Argon2's time and memory settings, tuned so one hash takes a small fraction of a second. You never notice it at login, but an attacker's billions of guesses a second shrink to a tiny fraction. Argon2 also needs lots of memory per guess, which blunts graphics cards.

Salt fixes a different weakness. Without it, everyone whose password is sunflower7 shares one hash, so cracking it once cracks them all, and attackers can look hashes up in huge ready-made tables for common passwords, such as rainbow tables. With a unique salt, identical passwords get different hashes, the tables are useless, and each account must be attacked separately. Neither trick saves a password that is on every common-password list.

Quick chooser: encrypt or hash?

One question settles most cases: will anyone need the original data back? Encrypt messages, files and backups, because someone with the key must read them later. Hash when you only need to check a match: a password at login, or a downloaded file against the SHA-256 value its publisher lists.

The flowchart adds a second question for the hashing branch. Notice that only passwords need the slow, salted kind: a file is not a secret, so a fast hash is enough to tell you whether it has changed.

Flowchart with two questions. First: do you need the original data back? Yes leads to encrypt it, unlock later with the key, for files, messages and backups. No leads to hash it, a one-way fingerprint, and a second question: is it a password? Yes leads to a slow salted hash, bcrypt or Argon2, for stored passwords. No leads to a fast hash such as SHA-256, to check that a file is unchanged. A note says hashing is not encryption: no key can undo a hash
Need it back? Encrypt. Only need to check it? Hash, slowly and with salt if it is a password.

Check yourself

After you click "Forgot password", a site emails you your current password. What does that tell you about how it stores passwords?

It stores them reversibly, as plain text or encrypted. With salted hashes, the site would have nothing to turn back into your password, only a reset link to offer.

Two people on a well-built site both choose the password sunflower7. Why do their stored hashes look completely different?

Each account has its own random salt mixed in before hashing, so the two inputs differ. That stops one cracked hash, or a ready-made table, from exposing both accounts.

A backup app must keep your photos private in the cloud and restore them after your laptop dies. Should it encrypt or hash them?

Encrypt. You need the original photos back, and only encryption can be undone with a key; a hash would leave just a fingerprint of each photo.