A belief you will meet in code reviews: “We store passwords hashed with SHA-256, so even if the database leaks, nobody can read them.” Half of that sentence is true. Nobody can simply read them. But the leap from “cannot read” to “safe” is where applications get hurt, and it is worth taking apart piece by piece.

Myth one: hashing and encrypting are the same idea

Encryption is reversible by design: with the key, you get the original back. A hash function is one-way. You feed in a password, get a fixed-length fingerprint, and there is no key that turns the fingerprint back into the password. For login, that is exactly what you want. The server never needs your password again; it only needs to check that what you typed now produces the same fingerprint as before.

So passwords should be hashed, not encrypted. If a system can email you your old password, it stored it recoverably, and that is a warning sign.

Myth two: one-way means unguessable

An attacker who steals a table of hashes does not reverse them. They guess. They take candidate passwords, hash each one, and compare with the stolen values. Where a match appears, the password is found. The only real question is how many guesses per second they can make.

General-purpose hashes such as MD5 and the SHA family were designed to be fast, because they are meant to checksum large files. On modern graphics cards, fast hashes can be computed in the billions per second. Human-chosen passwords come from a small, predictable space, so a fast hash falls to a determined attacker quickly. This is the real flaw in the SHA-256 claim: not that it is broken, but that it is too quick.

The fix is to use an algorithm built to be slow and, ideally, memory-hungry: bcrypt, scrypt, Argon2 (the winner of the Password Hashing Competition in 2015), or PBKDF2. Each has a tunable cost setting. You choose it so one login check takes a noticeable but tolerable fraction of a second on your servers. That is invisible to a real user logging in once, and ruinous for someone trying a billion guesses. Cost settings should be revisited every few years as hardware improves. Which algorithm and what parameters your organisation or a standard such as OWASP recommends changes over time, so check the current guidance instead of copying a number from an old blog.

Myth three: a salt is a secret

A salt is a random value, unique to each user, that is mixed into the password before hashing. It is stored right beside the hash. It is not secret. People get confused here, so ask what it is for.

Without salts, two users with the password “Chennai@123” produce identical hashes. An attacker looking at the leaked table sees the duplicates at once, and can crack one and win all of them. Worse, they can use precomputed tables of hashes for common passwords, built once and reused against every leaked database. With a unique salt per user, identical passwords produce different hashes and precomputed tables become useless, because the attacker would need a separate table for every salt. They are forced to attack each account individually.

What a salt does not do is make a weak password strong. A single account with the password “password” will still fall in moments, salt or no salt. The salt removes economies of scale; the slow algorithm raises the price of each guess; neither replaces a decent password.

Where the pepper fits

A pepper is a secret value added to every password hash, kept somewhere other than the database, for example in an application secret store or a hardware security module. If an attacker steals only the database through an injection flaw, say, they hold hashes they cannot even start cracking without the pepper. It is a second wall.

Be honest about its limits, though. Peppers add operational complexity: rotating one means handling old and new values together. If the attacker gets the application server, they usually get the pepper too. Many good systems run without one. Treat it as an optional extra layer, not a substitute for the first two measures.

A tiny worked comparison

Say an invented shop’s database leaks with 100,000 accounts. Suppose the attacker can test one billion fast-hash guesses per second, and a slow algorithm tuned to roughly a tenth of a second per check reduces a single machine to about ten per second per core. Even if you multiply by thousands of cores, the gap between those speeds is many orders of magnitude. A guess list that took minutes now takes years for anything beyond the most common passwords. Check the arithmetic for yourself with your own assumptions; the point is the ratio, not the exact numbers.

What to do with an existing system

If you inherit a system using unsalted fast hashes, you cannot recover the original passwords to rehash them. The usual approach is to upgrade on login: when a user signs in successfully, verify against the old scheme, then immediately store a fresh slow, salted hash. Accounts that never log in remain weak, so a forced reset campaign for dormant accounts, or wrapping the old hash inside the new algorithm, are sensible follow-ups.

And please do not write your own scheme. Use the vetted library functions in your language’s framework, let them generate salts and encode parameters in the stored string, and move on to the dozens of other things that need your attention. Password storage is a solved problem when you resist cleverness.

Figures and names in the examples are illustrative.

Leave a Reply

Trending