KAHIBARO
Discord Login Register

13.3. Password Hashing

Why Password Hashing Matters

When you build a backend, you will almost never store user passwords directly. If your database is leaked and you keep passwords in plain text, every account is instantly compromised.

Password hashing is the standard way to protect stored passwords. It turns a human password into a long, random-looking string that is hard to reverse.

You will use password hashing whenever you:

In this chapter you will learn what hashing is, what makes password hashing special, and how to do it correctly in a modern backend.

**Never store plain text passwords.
Never log plain text passwords.
Never send plain text passwords by email or chat.**


Hashing vs Encryption

Both hashing and encryption transform data, but they have very different purposes.

One-way vs Two-way

For passwords we want a one-way operation. The server should not be able to recover the original password. It only needs to check if a provided password matches the stored hash.

Important Properties of Cryptographic Hash Functions

Cryptographic hash functions like SHA-256, SHA-512, or SHA-3 are designed with these properties:

Example with SHA-256:

InputSHA-256 (first 16 chars)
password123ef92b778bafe771e…
password124bcbe3365e6ac95ea…

Even though only one character changed, the hash output is very different.

However, general-purpose hash functions (like SHA-256) are not enough for password storage. You need stronger protection.


Why Plain Hashing Is Not Enough

You might think:

“I will just store SHA-256(password) in the database. That is safe, right?”

This is better than plain text, but still weak. Modern attackers can try billions of passwords per second using GPUs or custom hardware.

Fast Hash Functions Are a Problem

Cryptographic hash functions are designed to be very fast. That is great for integrity checks, but terrible for passwords.

If your hash function is too fast, an attacker can:

  1. Take a list of common passwords.
  2. Hash each one with SHA-256.
  3. Compare to stolen hashes.
  4. Quickly find matches.

This is called a brute-force or dictionary attack.

Rainbow Tables

A rainbow table is a huge precomputed table of password -> hash pairs.

Example:

PasswordSHA-256 (shortened)
1234568d969eef6ecad3c2…
password5e884898da280471…
qwertyb1b3773a05c0ed01…

If an attacker steals your database of SHA-256 hashes, they can:

To defeat this you need salts and slow, password-specific hashing algorithms.


Salting Passwords

A salt is a random value that you generate for each password and store along with the hash.

How a Salt Works

Process:

  1. Generate a random salt for the user, for example:
    salt = "x83Kf7pQzWy1"
  2. Combine password and salt, for example:
    input = password + salt
  3. Compute hash:
    hash = H(input)
  4. Store salt and hash in the database.

When the user logs in:

  1. Retrieve that user’s salt and hash.
  2. Compute hash_attempt = H(password_attempt + salt).
  3. Compare hash_attempt and stored hash.

Example:

UserPasswordSaltHash (simplified)
Apassword123x83Kf7pQH("password123x83Kf7pQ")
Bpassword123Z1Lm9aT2H("password123Z1Lm9aT2")

Even though both users have the same password, their hashes are completely different.

Why Salts Help

Salts solve several problems:

**Always use a unique, random salt per password.
Never reuse the same salt for all users.
Never use usernames or emails as salts.**

However, salts alone do not make the hash slow. You still need a password hashing algorithm.


Password Hashing Algorithms

Password hashing algorithms are special functions designed to be slow and expensive to compute. This makes brute force attacks much harder.

Popular modern options:

AlgorithmTypeRecommended today?Notes
bcryptCPU hardYesWidely used, very mature
scryptMemory hardYesMore memory usage
Argon2Memory hardYes, preferredWinner of PHC, considered modern best
PBKDF2CPU hardAcceptableOften built in, but older

These algorithms typically include:

Key Properties of Password Hashers

  1. Slow to compute
    You can tune them to take, for example, 100 ms per hash. For users this delay is almost invisible, but for attackers that try billions of guesses it is extremely painful.
  2. Memory hard (for some algorithms)
    Algorithms like Argon2 and scrypt require a lot of memory. This makes attacks with GPUs or specialized hardware more expensive.
  3. Configurable
    You can increase cost as hardware gets faster.

**Never use plain SHA-1, SHA-256, or MD5 alone for password storage.
Always use a dedicated password hashing algorithm like Argon2, bcrypt, scrypt, or PBKDF2.**


How Password Hashing Works in Practice

You do not need to implement hashing yourself. You will use a library. The process looks like this:

During Registration

  1. User sends a password to your backend.
  2. Your backend calls a password hashing function.
  3. The function:
    • Generates a random salt.
    • Applies the password hashing algorithm with the configured cost.
    • Returns a password hash string that usually includes:
      • Algorithm name
      • Cost parameters
      • Salt
      • Final hash
  4. You store that hash string in the database.

Example of a bcrypt hash string:

text
$2b$12$hJf9sZMfH4KbqzCEwmbF6O4y/uUvdGbEgVpeKBUIwZNWqJnnmOegq

This string encodes:

You store this single string in a column like password_hash.

During Login

  1. User sends email and password.
  2. You load the user from the database and get password_hash.
  3. Call a verify function provided by the library:
    • The function reads algorithm and parameters from the stored hash string.
    • It extracts the salt.
    • It runs the hashing algorithm on the provided password.
    • It compares the result to the stored hash, often using a constant time comparison to resist timing attacks.
  4. If verification passes, login succeeds.

You never decode the hash back to the original password. You only perform the same hash and compare.


Cost Factors and Tuning

Password hash algorithms have cost parameters that control how slow the hashing is. The idea is to make hashing:

Examples of Cost Parameters

Choosing Cost Values

Typical approach:

  1. On your server hardware, run a small benchmark.
  2. Adjust parameters until each hash takes something like:
    • 50 ms to 250 ms on the server.

For example, if you use bcrypt:

**Do not leave the cost too low just because it is faster.
Revisit cost settings as hardware improves.**


Storing Password Hashes Safely

You typically have a users table with a column for the password hash.

Example schema:

ColumnTypeExample value
idinteger1
emailtextalice@example.com
password_hashtext$2b$12$hJf9sZMfH4KbqzCEwmbF6O4y/uUvdGbEgVpeKBUIwZNWqJnnmOegq
created_attimestamp2026-08-27 10:05:00

Guidelines:

You might also want a way to mark:

Migrating and Updating Hash Algorithms

Over time, algorithms and best practices change. For example:

You can migrate gradually.

Strategy: Lazy Migration on Login

  1. Keep logic for both the old and new algorithm.
  2. When a user logs in:
    • Detect the type of hash from the stored string.
    • Verify with the old algorithm.
  3. If verification succeeds and the hash is old:
    • Immediately rehash the password with the new algorithm or higher cost.
    • Save the new hash value.
  4. Next time, verification uses the new hash.

This way, users do not need to reset passwords manually.

Example flow:

**Never try to convert one hash to another without the original password.
You must rehash using the plain password during a successful login or password change.**


Common Mistakes and How to Avoid Them

1. Storing Plain Text Passwords

2. Using MD5 or SHA-256 Directly

3. Using a Single Global Salt

4. Rolling Your Own Cryptography

5. Returning Too Much Information on Login Failure

Note that this is more related to authentication logic, but it is often implemented together with password hashing.


Summary

Password hashing is a critical part of building a secure authentication system.

Key ideas:

With these principles, even if your database is leaked, cracking your users’ passwords becomes significantly harder and much more expensive for attackers.

Views: 7

Comments

Please login to add a comment.

Don't have an account? Register now!