How Password Hashing Works Under the Hood
Every website you log in to holds something it should never be able to read: your password. The trick is password hashing. Instead of storing your password, a site stores a one-way fingerprint of it: a fixed string that
Every website you log in to holds something it should never be able to read: your password. The trick is password hashing. Instead of storing your password, a site stores a one-way fingerprint of it: a fixed string that proves you typed the right password without revealing what it is. Steal the database and you get fingerprints, not passwords.
What is password hashing, exactly?
Password hashing runs a password through a one-way mathematical function that produces a fixed-length string called a hash. The same password always produces the same hash, but the hash can never be reversed back into the password. A website stores the hash, not the password, so a stolen database does not hand attackers your login.
Encryption is reversible: a key turns ciphertext back into plaintext. Hashing has no key and no reverse gear. When you log in, the site hashes what you typed and compares it with the stored hash.
This is why a good site can never email you your forgotten password. If a site ever sends your actual password back in plain text, it is storing passwords the wrong way.
How does a hash function turn a password into a fingerprint?
A hash function takes input of any length and squeezes it into a fixed-length output through repeated mixing steps: bit shifts, modular additions and logical operations. Change one character of the input and the output changes completely, in a seemingly random way. That behaviour is called the avalanche effect, and it is what makes a hash a reliable fingerprint.
SHA-256 always outputs 64 hexadecimal characters, no matter the input size — text encodings like Base64 do the same job for binary data. MD5 always outputs 32. Two passwords that differ by one letter produce hashes with nothing in common.
Not every hash function is still trustworthy. MD5 was broken by practical collisions in 2004, and SHA-1 fell in 2017 when Google and CWI Amsterdam demonstrated the first real collision, the SHAttered attack. A collision means two different inputs producing the same hash — fatal for a fingerprint. SHA-256 and SHA-512 are the safe defaults today, and our free Hash Generator produces all five.
What is a salt, and why does every password need one?
A salt is a random string added to a password before hashing, different for every user. Without a salt, two users with the same password get the same hash, and attackers can pre-compute hashes for millions of common passwords, then look every hash up in a table. Those pre-computed tables are called rainbow tables, and a salt makes them useless.
When you create an account, the site generates a random salt and stores it next to your hash. It hashes the combination of your password and that salt, not the password alone. Two users who both chose "sunshine123" end up with different hashes, because their salts differ.
The salt is not a secret. It sits in the database in plain sight, and that is fine: its job is uniqueness, not secrecy. With unique salts, one pre-computed table cannot cover the whole database; each password must be attacked individually.
Why is a fast hash like SHA-256 wrong for passwords?
SHA-256 is a brilliant fingerprint, but it is far too fast for passwords. A graphics card can compute billions of SHA-256 hashes per second, so an attacker who steals a database of SHA-256 password hashes can test every word in a dictionary, and every variation, in minutes. Speed is a feature for file checksums and a disaster for password storage.
The fix is to make hashing deliberately slow, using key derivation functions that repeat the hashing step thousands or millions of times. NIST's digital identity guidelines (SP 800-63B, June 2017) require passwords to be salted and hashed with a suitable one-way key derivation function, with at least 10,000 iterations for PBKDF2.
That slowness is invisible to you and crippling to an attacker: hashing your password 100,000 times takes a fraction of a second on login, but for an attacker guessing millions of passwords, that fraction multiplies into years.
How do bcrypt, scrypt and Argon2 slow attackers down?
bcrypt, scrypt and Argon2 are the three password-hashing algorithms security professionals recommend. All three slow guessing down on purpose, but in different ways. bcrypt, built in 1999, repeats an expensive Blowfish-based computation with a tunable cost factor, so doubling the cost doubles the attacker's work. It is battle-tested and the default in most web frameworks.
scrypt, from 2009, adds memory to the cost: it forces the computation to use a large block of RAM, which stops attackers from running millions of guesses in parallel on cheap graphics cards. Argon2, the winner of the 2015 Password Hashing Competition, lets you tune time, memory and parallelism separately, and its Argon2id variant is the current recommendation from OWASP.
| Algorithm | Year | What makes guessing expensive | Best for |
|---|---|---|---|
| PBKDF2 | 2000 | Many iterations of a fast hash | Systems that need FIPS compliance |
| bcrypt | 1999 | Tunable cost factor, Blowfish-based | Most web apps, the safe default |
| scrypt | 2009 | Memory-hard, blocks GPU parallelism | High-value accounts |
| Argon2id | 2015 | Tunable time, memory and parallelism | New systems, the current top pick |
One rule covers all four: never hash passwords with plain MD5 or SHA-256. Fast hashes belong to file checksums and API signatures, not to logins. If you run a website, your framework almost certainly has bcrypt or Argon2 built in — use it, or our htpasswd generator.
How does a website check your password without knowing it?
When you log in, the site never decrypts anything, because there is nothing to decrypt. It takes the password you typed, combines it with the salt stored next to your account, runs the same slow derivation function, and compares the new hash with the hash it stored at signup. If the two hashes match exactly, the password was right.
The comparison is done in constant time, so the check takes the same amount of time whether the first character matches or not. A comparison that stops at the first mismatch leaks how many characters were right, and attackers can measure that timing difference over a network.
A good login flow adds two more layers. First, the password travels over an encrypted connection, so nobody on the network can read it. Second, the account locks or slows down after a handful of failed attempts, so the front door cannot be brute-forced. Hashing protects the database; rate limiting protects the login page. The same one-way idea signs JSON Web Tokens.
FAQs
Is password hashing the same as encryption?
No. Encryption is reversible: with the right key, ciphertext turns back into the original password. Hashing is one-way: there is no key and no way back. That is why sites hash passwords — even the site itself cannot recover yours, so a stolen database does not expose it.
Can a hashed password ever be cracked?
Yes, if the hash is weak. Fast unsalted hashes like MD5 fall to dictionary attacks in minutes, because attackers pre-compute common passwords. Slow salted hashes like Argon2id resist cracking because each guess costs time and memory. The algorithm and the password's strength decide the outcome.
What is the difference between a hash and a salt?
A hash is the one-way fingerprint produced from your password. A salt is a random value mixed in before hashing, unique per password and stored openly next to the hash. The hash hides the password; the salt stops attackers from attacking every account at once with pre-computed tables.
Why do some sites say my password must be at least 8 characters?
Length is the cheapest security there is. Every extra character multiplies the number of possible passwords, and NIST's guidelines require at least 8 characters for user-chosen passwords. A long passphrase of ordinary words beats a short password full of symbols, and it is far easier to remember — build one with our Password Generator.
Is it safe to check a hash in an online hash generator?
Yes, as long as you never paste a real password into one. A hash generator is safe for learning, checksums and testing: it runs in your browser and nothing is uploaded. But a hash of your real password is still a fingerprint of it, so use test strings and sample text only.
Password hashing is a small piece of engineering with an outsized job: it lets a website prove who you are without ever knowing your secret. A one-way hash hides the password, a unique salt defeats pre-computed tables, and a deliberately slow algorithm makes guessing too expensive to bother with.
Try the free Hash Generator: type any text and watch MD5, SHA-1, SHA-256, SHA-384 and SHA-512 fingerprints appear instantly, all in your browser.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.