software-engineer-blog logoSoftware Engineer Blog

Module 12 · Security

Unit 46 of 49

Unit 46 · Module 12 · Security

Storing passwords — hashing that is slow on purpose

Both bcrypt and SHA-256 are one-way. Only one of them survives the leak.

Unit 46 of the free 49-unit computer-science course, in security. 2 topics to watch or read, 3 interview questions answered in full and a short self-check.

Watch or read

2 topics make up this unit. Take each one whichever way suits you, then answer the questions below.

bcrypt vs SHA-256

"We hashed the passwords, so they're safe." Both bcrypt and SHA-256 are one-way hashes — but only one survives a database leak. SHA-256 isn't broken; it's fast, and fast is exactly the bug for password storage. Here's how salting, cost factors and GPU guess rates turn the same leak from hours into centuries — plus what bcrypt actually costs you, and why the fast hash is still the right call for API keys in an LLM app.

ReelRead

Encryption vs hashing

Encryption is reversible (lock and unlock with a key); hashing is one-way (no way back). Learn when to use each, why passwords must always be hashed, and how salt and slow hashing defeat brute-force attacks.

ReelRead

Interview questions this unit unlocks

Asked out loud, answered out loud. Read the answer, then say it in your own words.

Why is bcrypt the right choice for a password when SHA-256 is stronger?

Because they are built for opposite goals. SHA-256 is designed to be fast, which is what you want for integrity and exactly what you do not want here — a GPU tries billions of SHA-256 guesses a second. bcrypt, scrypt and Argon2 are deliberately slow and tunable, with a work factor you raise as hardware improves, and they salt every password so two identical passwords do not produce the same hash and a rainbow table is worthless.

The number that makes it land: the same GPU that does billions of SHA-256 hashes a second does tens of thousands of bcrypt hashes. That ratio is the entire security margin.

What is a salt, and what is a pepper?

A salt is a unique random value stored alongside each hash. It does not need to be secret; its job is to make every hash unique so an attacker has to attack each password separately rather than the whole table at once. A pepper is a single secret value kept outside the database — in an environment variable or a KMS — and mixed in, so a stolen database alone is not enough to start guessing.

What is the difference between encryption and hashing?

Encryption is reversible with a key and exists so that someone authorised can read the data back. Hashing is one-way and exists so that nobody can, including you. Passwords are hashed because the system never needs the original — it only needs to check a candidate. Anything you must be able to display again, like an API key you show once or a stored document, is encrypted, and then the real question becomes where the key lives.

Self-check — 3 questions

Answer alone, at 2am, with no interviewer in the room.

Part of Everything You Need to Know About Computer Science.