sodcalculator

10 Tech How-To

Password Hashing: What It Is & How It Works

Guide 10-53832 · Updated · Step by step

Password hashing is the process of converting a plaintext password into a fixed-length string of characters using a one-way mathematical function, so the original password cannot be recovered from the stored value. When you enter a password on a website, the system hashes what you type and compares it to the stored hash; if they match, you’re authenticated. This article explains what hashing is, why it’s essential for security, how it works, and common pitfalls to avoid.

What Is Password Hashing?

Password hashing is a cryptographic technique that transforms a password into a fixed-size string of characters (the hash) using a deterministic algorithm. The same password always produces the same hash, but even a tiny change in the password produces a completely different hash. The key property is that hashing is one-way: it’s computationally infeasible to reverse the process to recover the original password. This means a database breach doesn’t expose actual passwords—only their hashes, which are useless without the original.

Hashing is different from encryption. Encryption is two-way: data encrypted with a key can be decrypted with the same (or a related) key. Hashing has no key and no decryption. That’s why it’s the standard for storing passwords: even if an attacker steals the hash database, they can’t simply decrypt the passwords.

How Password Hashing Works

The process is straightforward:

  • Input: The user provides a password (e.g., “correct horse battery staple”).
  • Salt: A random, unique string (the salt) is generated and appended to the password.
  • Hash function: The salted password is run through a cryptographic hash function (like bcrypt, scrypt, or Argon2) that produces a fixed-length output.
  • Storage: The salt and the resulting hash are stored together in the database, typically in a format like $2y$10$... for bcrypt.
  • Verification: When the user logs in, the system retrieves the stored salt, appends it to the entered password, hashes it again, and compares the result to the stored hash. If they match, the password is correct.

The salt ensures that two users with the same password will have different hashes, and it prevents precomputed rainbow table attacks. Modern algorithms like bcrypt, scrypt, and Argon2 are deliberately slow and memory-intensive, making brute-force attacks expensive.

Why Hashing Is Essential for Security

If a website stores passwords in plaintext, a single database breach exposes every user’s password. With hashing, the attacker only gets hashes, which are not directly usable. However, weak hashing algorithms (like MD5 or SHA-1) are fast and can be cracked with brute force or rainbow tables. That’s why modern systems use adaptive algorithms that can be made slower as hardware improves.

Hashing also protects users who reuse passwords across multiple sites. If one site is breached, the attacker can’t use the hash to log into other sites because the hash is not the password. This is why security experts recommend using a unique password per site, but hashing adds a layer of defense even when users don’t follow that advice.

Common Hashing Mistakes

Several pitfalls can undermine password hashing:

  • Using unsalted hashes: Without a salt, identical passwords produce identical hashes, making it easy to spot common passwords and use rainbow tables.
  • Using fast algorithms: MD5, SHA-1, and even SHA-256 are designed for speed, making them easy to brute-force. They should never be used for passwords.
  • Truncating hashes: Some systems store only part of the hash to save space, which increases collision risk and weakens security.
  • Not updating algorithms: As hardware improves, older algorithms become easier to crack. Systems should periodically upgrade to stronger algorithms.
  • Storing hashes without a pepper: A pepper is a secret key added to the hash, but it must be kept separate from the database. If not, it’s useless.

Choosing a Password Hashing Algorithm

The most widely recommended algorithms for password hashing are:

  • bcrypt: Based on the Blowfish cipher, it includes a cost factor that makes it slow. It’s widely supported and has been a standard for years.
  • scrypt: Designed to be memory-intensive, making it harder to attack with specialized hardware like GPUs.
  • Argon2: The winner of the Password Hashing Competition (2015), it’s considered the most modern and secure option, offering tunable memory and CPU usage.

When choosing, consider the trade-off between security and user experience. A higher cost factor makes hashing slower, which deters attackers but also adds latency to login. The cost should be set as high as your server can handle without noticeable delay (typically 100–300ms).

What About Password Managers and Two-Factor Auth?

Password hashing is about how the server stores passwords, but users also play a role. Password managers generate and store strong, unique passwords for each site, which reduces the risk of credential stuffing. Two-factor authentication (2FA) adds a second verification step, so even if a password hash is cracked, the attacker still needs the second factor. While hashing protects the stored password, 2FA protects the account even if the password is compromised.

It’s important to note that hashing doesn’t protect against phishing or keyloggers—if the user types the password into a malicious site, the attacker gets it directly. That’s why user education and HTTPS are also critical.

Frequently Asked Questions

Is password hashing the same as encryption?

No. Encryption is reversible with a key; hashing is one-way and irreversible. Hashing is used for password storage, while encryption is used for data confidentiality.

Can two different passwords produce the same hash?

Yes, this is called a collision. Cryptographic hash functions are designed to make collisions extremely unlikely, but they are theoretically possible. Modern algorithms like bcrypt and Argon2 have a very large output space, making collisions practically impossible.

Why do some websites still use MD5 for passwords?

Legacy systems sometimes use MD5 because it was common in the past. However, MD5 is fast and vulnerable to brute-force attacks, so it should be replaced. Many modern frameworks have moved to bcrypt or Argon2.

How long does it take to crack a hashed password?

It depends on the algorithm, the cost factor, and the password’s complexity. A strong password (e.g., 12+ random characters) with bcrypt could take centuries to crack with current hardware. A weak password like “123456” could be cracked in seconds.

Should I use the same password for multiple sites?

No. If one site is breached and your password is cracked, attackers will try it on other sites. Using unique passwords or a password manager prevents this.

What is a salt and why is it needed?

A salt is a random string added to the password before hashing. It ensures that identical passwords produce different hashes, preventing rainbow table attacks and making it harder to detect common passwords.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →