Data breaches make the news every year, and a recurring detail is how the stolen passwords were stored. Sites that protected passwords properly often limited the damage. Sites that stored them in plain text, or with outdated methods, exposed millions of people. If you build or maintain anything with user accounts, this is one of the most important topics you can learn. This guide explains the right approach in plain language.
The Golden Rule: Never Store Plain Passwords
A database should never contain actual passwords. If an attacker steals a copy of the database, whether through a bug, a leaked backup or a malicious insider, plain text passwords can be used immediately. Because many people reuse passwords, the damage spreads to other services too. Encryption is also the wrong tool, because encrypted data can be decrypted, and the key must be stored somewhere. Passwords should be hashed.
What Hashing Does Here
A hash function turns an input into a fixed-length fingerprint. It is one-way: you cannot compute the original password from the hash. When a user logs in, you hash the password they typed and compare it to the stored hash. If they match, the password is correct, and you never needed to store the password itself.
Why a Plain Fast Hash Is Not Enough
Algorithms like SHA-256 are designed to be fast, which is great for verifying files but terrible for passwords. An attacker with a stolen list of hashes can try billions of guesses per second on modern hardware, using lists of common passwords and variations. Two further weaknesses make it worse:
- Identical passwords produce identical hashes, so cracking one reveals every user who chose the same password.
- Precomputed tables of hashes for common passwords, known as rainbow tables, can reverse unsalted hashes almost instantly.
Salting
A salt is a unique random value generated for each password and added to it before hashing. Because every user has a different salt, identical passwords produce different hashes, and precomputed tables become useless. The salt is not secret and is stored alongside the hash. Modern password hashing functions generate and embed the salt automatically, so you rarely handle it yourself.
Use a Purpose-Built Password Hashing Algorithm
These algorithms are intentionally slow and configurable, so you can make each guess expensive for an attacker while remaining quick enough for a single login.
- Argon2id is widely recommended today. It is memory-hard, which makes large-scale attacks on specialized hardware more costly.
- bcrypt has been used successfully for many years and remains a solid, widely supported choice.
- scrypt is also memory-hard and well regarded.
- PBKDF2 is acceptable where standards compliance requires it, with a high iteration count, but it is less resistant to specialized hardware.
Always check the current recommendations from a trusted security organization, because the recommended cost settings increase over time as hardware becomes faster.
A Simple PHP Example
PHP includes built-in functions that follow best practice and handle salting for you.
To store a password:
$hash = password_hash($password, PASSWORD_DEFAULT);
Save $hash in your database column. Use a column that can hold at least 255 characters, so that future algorithm changes do not cause truncation.
To verify a login:
if (password_verify($password, $hash)) { /* success */ }
To upgrade old hashes over time:
if (password_needs_rehash($hash, PASSWORD_DEFAULT)) { $newHash = password_hash($password, PASSWORD_DEFAULT); /* save it */ }
PASSWORD_DEFAULT currently selects bcrypt, and you can choose PASSWORD_ARGON2ID where it is available. Never write your own hashing scheme, and never combine a fast hash with a homemade salt.
What About a Pepper?
A pepper is an additional secret value, stored outside the database such as in server configuration or a hardware module, and mixed into the process. If an attacker steals only the database, the pepper makes cracking harder. It adds complexity and requires careful management, so it is optional extra protection rather than a replacement for proper hashing and salting.
Other Practices That Protect Accounts
- Enforce a sensible minimum length, and allow long passwords and passphrases. Avoid pointless rules that force awkward symbol combinations.
- Check new passwords against lists of known breached passwords.
- Offer two-factor authentication.
- Limit login attempts and add delays or temporary lockouts to slow guessing attacks.
- Always serve login pages over HTTPS.
- Never email passwords, and never show them in logs or error messages.
- Use secure, single-use, expiring tokens for password resets, and invalidate them after use.
- Compare tokens using timing-safe comparison functions.
Common Mistakes
- Using MD5 or SHA-1, even with a salt. These are unsuitable for passwords.
- Using one global salt for all users instead of unique salts.
- Truncating passwords before hashing.
- Writing custom cryptography instead of using vetted libraries.
- Storing "security questions" in plain text.
- Keeping old plain-text or weakly hashed passwords after migrating.
Experiment With Hashes
To understand how hashes behave, such as how changing a single letter completely changes the result, you can generate SHA-1, SHA-256 and SHA-512 hashes with our free Hash Generator. It runs entirely in your browser. Remember that these general-purpose hashes are for learning and file verification, not for storing passwords.
Final Thoughts
Protecting user passwords comes down to a short list: never store them in plain text, never use a fast hash, use a dedicated algorithm such as Argon2id or bcrypt with automatic salting, and combine it with good account security practices. Your users trust you with credentials they probably use in other places, so treat that trust seriously.