Every time you create an account online, you hand over a password and trust the website to keep it safe. But what does “safe” actually mean? A well-built system never stores your password as you typed it. Instead, it relies on two ideas that every developer and security learner should understand: hashing and salting. This article explains both in plain language, shows why storing plain-text passwords is a serious mistake, and outlines the practices recommended today.
The problem with storing passwords as they are
Imagine a database table with a column called password containing values like sunshine123. If an attacker ever gains access to that database, through a vulnerability, a misconfigured backup or a malicious insider, every single account is immediately compromised. Worse, many people reuse passwords, so the damage spreads to email, banking and social media accounts on other sites.
Encrypting the passwords is better, but still not ideal. Encryption is reversible by design: whoever holds the key can decrypt everything. If the key is stolen along with the data, the protection disappears. For passwords, we need something that cannot be reversed at all.
What is a hash function?
A cryptographic hash function takes any input and produces a fixed-length output, often shown as a long string of letters and numbers. Good hash functions have a few essential properties:
- Deterministic: the same input always produces the same output.
- One-way: it is computationally impractical to recover the original input from the output.
- Avalanche effect: changing a single character in the input produces a completely different output.
- Collision resistant: it should be extremely hard to find two different inputs with the same output.
Instead of saving your password, the website saves its hash. When you log in, the site hashes what you typed and compares the result with the stored hash. If they match, you are in. The site can verify your password without ever needing to know it.
Why hashing alone is not enough
Because hash functions are deterministic, everyone who chooses the password password123 ends up with the identical hash. Attackers take advantage of this in two main ways:
- Precomputed tables: attackers can calculate the hashes of millions of common passwords in advance (including so-called rainbow tables) and simply look up stolen hashes to find matches.
- Spotting duplicates: if many rows in a leaked database share the same hash, the attacker knows those users chose the same password, and cracking one cracks them all.
On top of that, general-purpose hash functions such as MD5 and SHA-1 were designed to be fast. Speed is great for checking file integrity, but terrible for password storage, because it lets an attacker with modern graphics cards test enormous numbers of guesses per second. MD5 and SHA-1 also have known weaknesses and should not be used for passwords.
Adding salt: making every hash unique
A salt is a random value generated for each user at the moment the password is created or changed. The system combines the salt with the password before hashing and stores the salt alongside the resulting hash. The salt does not need to be secret; its job is to make each hash unique.
Here is what changes when salting is used:
| Scenario | Two users with the same password | Precomputed tables |
|---|---|---|
| Plain text | Password visible directly | Not even needed |
| Hash without salt | Identical hashes | Effective |
| Hash with unique salt | Different hashes | Practically useless |
With a unique salt per user, an attacker cannot reuse work across accounts. Each password has to be attacked individually, which dramatically increases the cost of cracking a large database.
Slow on purpose: password hashing algorithms
To counter fast guessing, security experts recommend algorithms built specifically for passwords. They are deliberately slow and configurable, so the work factor can be increased as hardware improves. The most widely recommended options are:
- Argon2: winner of the Password Hashing Competition in 2015. Its Argon2id variant is a common default recommendation because it is resistant to both GPU-based and certain side-channel attacks. It lets you tune memory use, time cost and parallelism.
- bcrypt: an older, battle-tested algorithm with a built-in salt and an adjustable cost factor. Still widely used in many frameworks.
- scrypt: designed to be memory-hard, making large-scale hardware attacks more expensive.
- PBKDF2: applies a hash function many thousands of times. It is accepted in many standards, though it requires a high iteration count to be effective.
A few hundred milliseconds of extra work is barely noticeable for a user logging in once, but it adds up to a massive barrier for someone trying billions of guesses.
What about “pepper”?
Some systems add a pepper: a secret value shared across all passwords and stored separately from the database, for example in a configuration vault or hardware security module. If attackers steal only the database, they lack the pepper and their cracking attempts fail. A pepper complements salting and slow hashing; it does not replace them.
Good practices for developers
- Never write your own crypto. Use well-maintained libraries or the built-in password functions of your language or framework.
- Choose a modern algorithm such as Argon2id or bcrypt, with a sensible cost setting.
- Generate a unique, random salt for every password. Most good libraries handle this automatically.
- Never log passwords, not even in debug output or error messages.
- Plan for upgrades. Store the algorithm and parameters with each hash so you can re-hash with stronger settings when users next log in.
- Combine with other defenses, such as rate limiting login attempts and offering multi-factor authentication.
What this means for everyday users
Even with excellent hashing, weak and reused passwords remain the easiest targets, because attackers start by guessing the most common choices. Long, unique passphrases and a password manager make cracking far harder. And if a service ever emails you your original password after you click “forgot password”, that is a red flag: it suggests the password may be stored in a recoverable form.
Conclusion
Secure password storage boils down to a simple principle: the system should be able to check a password without ever being able to reveal it. Hashing provides the one-way transformation, salting makes each result unique, and slow, purpose-built algorithms make large-scale guessing impractical. Understanding these concepts is a foundation for anyone working in software development or information security.
If you want to go deeper into topics like authentication, cryptography and secure coding, take a look at the free Cyber Security courses available on Cursa.