Hashing and Password Storage
Chapters and transcript
Hashing and Password Storage An attacker breaks into a website and copies its user database. The passwords inside are stored as hashes. Can he use them? That depends on how they were stored. In this lesson, you will learn what makes a hash function secure, why MD5 and SHA-1 were retired, and how a well-built system should store passwords.
Secure hash functions A hash function takes input of any size and produces a fixed-size output, called the hash, or digest. For example, SHA-256 always produces two hundred fifty-six bits, usually shown as sixty-four hexadecimal characters.
Secure hash functions A cryptographic hash function must have five properties. Deterministic: the same input always gives the same hash. Avalanche effect: a tiny change gives a completely different hash. Preimage resistance: from a hash, you cannot work out an input. Second preimage resistance: given one input, you cannot find another with the same hash. And collision resistance: you cannot find any two inputs that share a hash.
Secure hash functions Here is the avalanche effect in a terminal. We hash Pay Ali five hundred rupees. Then we change only one number, to nine hundred. Compare the two hashes. They share no visible pattern. That is what makes hashes useful for spotting changes.
Secure hash functions Remember, hashing is not encryption. Encryption uses a key, and can be decrypted. Hashing has no key, and there is no way to decrypt a hash. If someone offers to decrypt your hash, what they really do is guess inputs until one matches.
Collisions A collision happens when two different inputs produce the same hash. Why does it matter? Signatures usually sign the hash of a document. If an attacker makes a harmless document and a harmful one with the same hash, a signature on the harmless one also works on the harmful one.
Retired algorithms Two famous algorithms have failed. MD5 is broken. Collisions can be made quickly on an ordinary computer. SHA-1 is broken too. In twenty seventeen, researchers published two different PDF files with the same SHA-1 hash. SHA-256 is secure and widely used, and SHA-3 is a secure, newer alternative. MD5 and SHA-1 are retired for security uses.
Hashing for integrity Hashes help check downloads. The website publishes the SHA-256 hash of a file. You download the file, calculate the hash yourself, and they match. But there is a catch. If an attacker controls the website, he can replace both the file and the published hash. That is why important software is also digitally signed.
Hashing for integrity When two systems share a secret key, they can use a keyed hash. The most common is HMAC. It mixes a secret key into the hash. Only someone with the key can create a valid HMAC. So it proves integrity and authenticity. Payment systems and web APIs often use it to sign requests and webhook messages.
Fast hashes are bad Back to the leak. The attacker has the password hashes. He cannot reverse them, so he guesses. Hash a likely password, compare, repeat. MD5, SHA-1 and SHA-256 are designed to be fast, and graphics cards calculate enormous numbers of them every second. If the hashes are unsalted, he can even use a rainbow table, a huge precomputed list, and just look them up.
Salts and slow hashing Good password storage starts with a salt. A salt is a random value, made fresh for each user and stored next to the hash. The system hashes salt plus password. So two users with the same password get different hashes. Rainbow tables become useless. And attackers must attack each user separately. The salt is not secret. Its job is to make each hash unique.
Salts and slow hashing Next, use a slow algorithm built for passwords. Argon2id is a common recommendation today. It uses memory as well as time, which makes GPU attacks harder. bcrypt is older and still widely used. Raising its cost factor by one roughly doubles the work. scrypt and PBKDF2 are also acceptable when configured well. Finally, an optional pepper is a secret value added to every password, stored apart from the database.
Quick check Quick check. Your team is building a new app and must choose how to store user passwords. Which choice is best? Take a moment.
Try it yourself Try it yourself in Windows PowerShell. Get-FileHash calculates the SHA-256 hash of any file you own. Here, report dot pdf gives one hash. A copy with one letter changed gives a completely different hash. On Linux or macOS, use sha256sum. Next time you download software that publishes a SHA-256 hash, compare it before you install.
Key takeaways Let us put it together. A hash is a one-way, fixed-size fingerprint, not encryption. MD5 and SHA-1 are retired for security. Use SHA-256 or SHA-3. Use HMAC or signatures to prove who sent data. Store passwords with Argon2id or bcrypt, and a unique salt. And a slow hash does not save a weak password. Strong, unique passwords and MFA remain essential.
In the Security Basics path you learned that a hash is a one-way fingerprint of data. This lesson goes deeper. You will learn what makes a hash function secure, why some famous algorithms are now retired, and exactly how a well-built system should store passwords.
What makes a hash function secure
A hash function takes input of any size and produces a fixed-size output, called the hash or digest. For example, SHA-256 always produces 256 bits, usually shown as 64 hexadecimal characters.
A cryptographic hash function must have these properties:
- Deterministic: the same input always gives the same hash.
- Avalanche effect: a tiny change in the input changes the hash completely.
- Preimage resistance: given a hash, you cannot work out an input that produces it.
- Second preimage resistance: given one input, you cannot find a different input with the same hash.
- Collision resistance: you cannot find any two different inputs that share the same hash.
Here is the avalanche effect in action. Only one number in the message changes:
"Pay Ali 500 rupees" -> 58a485b5eeb6e8cd74526f38faa05bf36758930fa5ff625bc9ab9077d9d8f1bc
"Pay Ali 900 rupees" -> 3a7deb696d2bf6f514653b4320ed384ca25ed2c664d44c9cd54b3aef55765a67
The two hashes share no visible pattern. This is what makes hashes useful for spotting changes.
Remember that hashing is not encryption. There is no key, and there is no way to "decrypt" a hash. If someone offers to "decrypt" your hash, what they really do is guess inputs until one matches.
Collisions and retired algorithms
A collision happens when two different inputs produce the same hash. Because the number of possible inputs is unlimited and the output is fixed in size, collisions must exist in theory. A secure hash function makes them practically impossible to find. For SHA-256, finding a collision by trial would take around 2 to the power of 128 attempts, which is far beyond any computer.
Why do collisions matter? Signatures and certificates usually sign the hash of a document, not the document itself. If an attacker can make two documents with the same hash, a signature on the harmless one also works on the harmful one.
Two well-known algorithms have failed this test:
| Algorithm | Output size | Status |
|---|---|---|
| MD5 | 128 bits | Broken. Collisions can be created quickly on an ordinary computer. |
| SHA-1 | 160 bits | Broken. In 2017 researchers published two different PDF files with the same SHA-1 hash. |
| SHA-256 (SHA-2 family) | 256 bits | Secure and widely used. |
| SHA-3 | Various | Secure; a newer design used as an alternative. |
MD5 and SHA-1 are retired for security uses. Browsers no longer accept certificates signed with them, and new systems should not use them for signatures, integrity checks or passwords. You may still see MD5 used to spot accidental file damage or duplicates, where no attacker is involved. That is acceptable, but it gives no protection against deliberate tampering.
Hashing for integrity
Hashes are a simple way to check that a file has not changed. Many software websites publish the SHA-256 hash of each download. After downloading, you calculate the hash yourself and compare. If they match, the file you have is the file they published.
But there is a catch. A plain hash proves the file matches a hash, not that the hash came from the right person. If an attacker controls the website, he can replace both the file and the published hash. This is why important software is also digitally signed, as you learned in the previous lesson.
When two systems share a secret key, they can use a keyed hash instead. The most common one is HMAC (Hash-based Message Authentication Code). It mixes a secret key into the hash. Only someone who knows the key can create a valid HMAC, so it proves both integrity and authenticity. Payment systems and web APIs often use HMAC to sign requests and webhook messages, and some JWT login tokens are signed this way.
Why fast hashes are bad for passwords
When a website's database leaks, attackers get the password hashes. They cannot reverse them, so they guess: hash a likely password, compare, and repeat. The faster the hash, the more guesses per second.
General-purpose hashes like MD5, SHA-1 and SHA-256 are designed to be fast, which is good for checking large files. But for passwords, speed helps the attacker. Modern graphics cards (GPUs) can calculate enormous numbers of these hashes every second, so short or common passwords fall almost at once.
Another old attack uses rainbow tables: huge precomputed lists that map hashes back to common passwords. If a site stores unsalted hashes, an attacker can simply look up each hash in the table, with no guessing needed at the time of the attack.
Salts, peppers and slow algorithms
Good password storage combines three ideas.
1. A unique salt for every password. A salt is a random value, created fresh for each user and stored next to the hash. The system hashes salt + password. Because every salt is different:
- Two users with the same password get different hashes.
- Precomputed rainbow tables become useless.
- Attackers must attack each user's hash separately.
The salt is not secret. Its job is to make each hash unique.
2. A slow, purpose-built password hashing algorithm. These are designed to make every guess expensive:
- Argon2id: the winner of the Password Hashing Competition in 2015 and a common recommendation today. It uses a lot of memory as well as time, which makes GPU attacks harder.
- bcrypt: older and still widely used. It has a cost factor; raising it by one roughly doubles the work.
- scrypt and PBKDF2: also acceptable when configured well.
These algorithms include the salt automatically. A login that takes a fraction of a second is unnoticeable for a user, but makes billions of guesses very slow for an attacker.
3. An optional pepper. A pepper is a secret value added to every password, stored separately from the database, for example in the application's secret settings. If only the database leaks, the attacker is missing the pepper.
A bcrypt hash looks like this. The $2b$ shows the algorithm version, 12 is the cost factor, and the salt and hash follow:
$2b$12$<22 characters of salt><31 characters of hash>
Finally, hashing does not save a weak password forever. A slow hash slows the attacker down, but 123456 will still be among the first guesses. Strong, unique passwords and MFA remain essential.
Try it yourself
You can calculate a file's SHA-256 hash with tools already on your computer. Use any file you own, such as a document in your Downloads folder.
On Windows PowerShell:
Get-FileHash .\report.pdf -Algorithm SHA256
On Windows Command Prompt:
certutil -hashfile report.pdf SHA256
On Linux or macOS:
sha256sum report.pdf
shasum -a 256 report.pdf
Now make a copy of a small text file, change one letter in the copy, and hash both files. The hashes will be completely different. Next time you download software that publishes a SHA-256 hash, compare it with your result before installing.
If you build software, check how your system stores passwords today. If you find plain text, MD5, SHA-1 or unsalted SHA-256, plan a move to Argon2id or bcrypt. A common approach is to rehash each user's password with the new algorithm the next time they log in successfully.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. What is a hash collision?
2. Why is plain SHA-256 a poor choice for storing passwords?
3. Two users both choose the password 'Lahore2024'. With proper salting, what do their stored hashes look like?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake