Password Storage

What to know about password storage in DB.

Overview

  1. Password should not stored in plain text
  2. Hashing is used instead of encryption
  3. Slow hashing (Argon2id, bcrypt, PBKDF2) is used instead of fast hashing (SHA-256)
  4. Unique salt is used, in defense of precomputated lookpup tables (rainbow table etc.)
  5. Use pepper for defense in additional depth

Key concepts

1. Encryption Vs. Hashing

Encryption is two-way function, where encrypted password itself is reversible thru a key. Hashing is one-way function, where it cannot be decrypted; even it is obtained, there is no way to decrypt it.
Use hashing, because it is not decryptable.

2. Salting

A salt is a unique, randomly generated value used as an additional input to the password hashing function: passwordHash(password, salt) -> hash.
Modern password hashing algorithms/libraries such as Argon2id usually handle salting automatically. The salt does not need to be secret. It is commonly encoded together with the algorithm parameters and resulting hash in the stored password hash.
If an attacker obtains the password database, they can normally obtain both the hash and the salt. The purpose of a salt is not to prevent password guessing. Instead, a unique salt ensures that users with the same password have different password hashes. This prevents attackers from reusing the same precomputed hash or cracking work across multiple users.
Therefore, the attacker must perform the password-guessing computation separately for each user’s salt.

3. SQL Injection

Untrusted input became part of SQL syntax.

Use Prameterized Query instead of string concatenation.

4. Pepper

A pepper is a secret value used as an additional protection for password hashes.
Unlike a salt, it must remain secret and should be stored separately from the password database, such as in a secrets manager or HSM.
If only the password database is compromised, the attacker still lacks the pepper needed to reproduce the password verification process. Pepper rotation is more difficult because the server does not retain users’ plaintext passwords.

Pre-Hashing Pepper passwordHash(password, salt, pepper) -> hash

Post-Hashing Pepper passwordHash(password, salt) -> HMAC(hashedOncePassword, pepper)

5. Work Factor

Work factor controls how computationally expensive password hashing is. Different password hashing algorithms expose it differently. For example, bcrypt uses a cost factor that increases the amount of work exponentially, while Argon2id provides parameters such as memory cost, time cost, and parallelism.
The work factor/parameters are normally stored as part of the encoded password hash, so the same parameters can be used during password verification.
A higher work factor makes each password guess more expensive, which slows down offline password cracking. However, it also makes legitimate password verification more expensive, so it should be configured to balance security, server resource usage, and acceptable login latency.

Interview questions

1. What is the difference between Hashing and encryption?
2. Why slow hashing?
3. What is salt and what is it used for?
4. what is Pepper used for?
5. What is SQL injection?
6. What does work factor do?

References

Cheat Sheet for Storage