SECURITY ENGINEERING · PART 4 · CRYPTOGRAPHY IN PRACTICE · HASHING · 49 OF 229

Password Hashing: bcrypt, Argon2, Salt and Pepper

How to store passwords so a stolen database is not a disaster: why plaintext, encryption and fast hashes like SHA-256 all fail (we computed a million SHA-256 hashes in 0.42 seconds), what a salt and a pepper do, how bcrypt and Argon2id slow attackers down (0.27 s per guess at cost 12), how to read a stored hash, and how to upgrade old hashes safely.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Hash, never store
  • fast hashes fail
  • unique salt per user
  • Argon2id or bcrypt, slow on purpose
  • MFA on top

Notes

The idea

The idea is simple. At sign-up, the application hashes the password and stores only the hash. At login, it hashes what the user typed and compares the result with the stored value. The password itself is never written to disk or to a log. Because a hash is one-way, someone who reads the table does not get the passwords directly. But as we will see, which hash you choose decides whether that protection is real.

How login works
  • sign up: hash the password, store only the hash
  • log in: hash what was typed, compare with the stored hash
  • the password itself is never written anywhere
  • a hash is one-way, so the table does not contain the passwords

What not to do

Three approaches fail. Plaintext: the moment the table leaks, every password is known. Encryption: it is reversible, and the key lives on the same server, so the attacker takes both. And a plain fast hash such as MD five, SHA one or even SHA two fifty six. That last one surprises people, because SHA two fifty six is a strong hash. The problem is not strength. It is speed.

Broken approaches
  • plaintext: everything is lost at once
  • encrypted: the key is on the same server; attacker takes both and decrypts
  • MD5, SHA-1, SHA-256 alone: far too fast to resist guessing
  • all three still appear in real breaches

Why fast is bad

An attacker with the table does not reverse hashes. They guess: hash a candidate password and compare. We computed one million SHA two fifty six hashes in zero point four two seconds, on one core, in plain Python. A graphics card does billions per second. At that speed, every common password, every password from earlier leaks and every short password is found quickly. Hash functions are designed to be fast, and that is exactly wrong here.

guessing speed, measured
# 1,000,000 SHA-256 hashes
# one CPU core, plain Python:
#   0.42 seconds

# a modern GPU: billions of
# SHA-256 guesses per second

# every 8-character password
# and every leaked-password
# list falls quickly

Salt

The first defense is a salt: a random value, unique for each user, mixed into the hash and stored beside it. We hashed the same password twice and got two different results. Without salts, identical passwords have identical hashes, and attackers use huge precomputed tables called rainbow tables to look answers up instantly. With salts, those tables are useless, and every user must be attacked separately. A salt is not secret; it only needs to be unique.

the same password, hashed twice
bcrypt.hashpw(b"password123", salt1)
# $2b$12$...     (60 characters)
bcrypt.hashpw(b"password123", salt2)
# $2b$12$...     a different hash

# salt: random, unique per user,
# stored with the hash, not secret
# defeats precomputed tables;
# forces one attack per user

Slow on purpose

The second defense is to be slow on purpose. Password hashing functions repeat their work thousands of times. We measured one bcrypt hash at cost twelve: zero point two seven seconds. For a user logging in, a quarter of a second is invisible. For an attacker, it means about four guesses per second per core instead of millions. A list of a billion candidate passwords now takes years for a single user, and every user has a different salt.

time for one hash, measured
SHA-256             0.0000004 s
bcrypt, cost 12     0.271 s
Argon2id, default   0.132 s

# login: a quarter of a second,
# nobody notices
# attacker: about 4 guesses per
# second per core, not millions

Which algorithm

Which algorithm? Argon two i d is the current best choice. It uses a large amount of memory as well as time, and memory is what makes graphics cards and custom chips expensive to use for cracking. A common setting is sixty four megabytes and three passes. Bcrypt is older, well proven, and fine at cost twelve or higher; note that it ignores anything beyond seventy two bytes. Scrypt is also memory-hard. Use PBKDF two only where a standard requires it, with a high round count.

What to use
  • Argon2id: the current best; uses memory as well as time, which defeats GPUs (64 MB, 3 passes is common)
  • bcrypt: older, proven, cost 12 or more; 72-byte limit
  • scrypt: also memory-hard
  • PBKDF2-SHA256: only when required, 600,000 rounds

Reading a stored hash

A stored hash describes itself. A bcrypt value starts with a version, then the cost, here twelve, meaning two to the power twelve rounds, then the salt and the hash, sixty characters in total. An Argon two value lists the memory, the passes and the threads, then the salt and the hash. Because the settings travel with each hash, you can raise the cost later and old hashes still verify. Recognising these formats is useful in reviews and incident response.

what is in the column
$2b$12$<22 chars salt><31 chars hash>
  2b  = bcrypt version
  12  = cost: 2^12 rounds

$argon2id$v=19$m=65536,t=3,p=4$
   <salt>$<hash>
  m = memory in KiB (64 MB)
  t = passes   p = threads
# 97 characters in our test

Pepper and upgrades

Two further techniques. A pepper is a secret key kept outside the database, in a key management service, and applied with HMAC before or after hashing. An attacker with only the table cannot even begin to guess. And upgrade on login: when a user signs in and their hash uses old settings, rehash the password with the new ones while you have it. For a legacy table of MD five hashes, wrap each in a strong hash immediately, or force resets.

Going further
  • pepper: a secret key held outside the database (KMS or HSM), applied with HMAC
  • a stolen table alone is then not enough to start guessing
  • upgrade on login: if the hash uses old settings, rehash with new ones
  • legacy MD5: wrap or reset

Beyond the hash

The hash is one layer. Around it, rate-limit login attempts, because attackers also guess online. Check new passwords against lists of breached passwords, and prefer length over complexity rules. Add multi-factor authentication or passkeys, so a stolen password alone is not enough. Never log passwords, including in debug output and error reports. And return the same message for a wrong user and a wrong password, so the login form does not reveal which accounts exist.

Around the hash
  • rate-limit and lock out repeated failures
  • check new passwords against breached-password lists
  • length over complexity; allow long passphrases
  • MFA or passkeys: a stolen password is not enough
  • never log passwords; same error for bad user or pass

Common mistakes

Mistakes. Plaintext, or reversible encryption. A fast hash alone, however strong it sounds. No salt, or one global salt for every user. A cost factor chosen ten years ago and never raised as hardware got faster. A home-made scheme instead of a library. Passwords written to logs. A short maximum length, which hints that the password is being stored, not hashed. And no multi-factor authentication on top.

Avoid these
  • plaintext or reversible encryption
  • MD5, SHA-1 or SHA-256 alone
  • no salt, or one salt for all
  • a cost factor set years ago
  • writing your own scheme
  • passwords in logs
  • a maximum length of 8 or 16
  • no MFA on top

Say it like this

In the interview: never plaintext and never reversible encryption. I use Argon two i d, or bcrypt at cost twelve or more, through a standard library, which gives every user a unique salt. I tune the cost to about a quarter of a second, add a pepper held in a key management service, and rehash on login when settings change. Around that: rate limiting, breached-password checks, multi-factor authentication, and no passwords in logs.

In the interview
  • "Never plaintext or encrypted.
  • Argon2id, or bcrypt at cost
  • 12+, with a unique salt per user, through a standard
  • library. A pepper in a KMS, rehash on login when settings
  • change, rate limiting, breach
  • checks and MFA, and passwords
  • never in logs."

Interview questions

Q1How should passwords be stored?

As the output of a dedicated password hashing function, Argon2id, bcrypt or scrypt, with a unique random salt per user and a cost tuned to take a noticeable fraction of a second. Never in plaintext, never with reversible encryption, and never with a fast hash alone.

Q2Why is SHA-256 not suitable for password hashing?

SHA-256 is designed to be fast, so attackers can test billions of candidate passwords per second on GPUs against a stolen table. Password hashing functions are deliberately slow and often memory-hard, which makes large-scale guessing impractical.

Q3What is a salt?

A random value, unique per password, combined with the password before hashing and stored alongside the hash. It ensures identical passwords have different hashes, defeats precomputed rainbow tables and forces attackers to attack each account separately.

Q4What is the difference between a salt and a pepper?

A salt is unique per user, stored with the hash and not secret. A pepper is a single secret shared by all hashes, stored outside the database in a KMS or HSM. With a pepper, a stolen database alone is not sufficient for offline guessing.

Q5bcrypt or Argon2: which is better?

Argon2id is the current recommendation because it is memory-hard, which strongly limits GPU and ASIC attacks. bcrypt remains secure at a cost of 12 or more and is widely supported, but it truncates input at 72 bytes and is not memory-hard.

Q6What is a rainbow table?

A precomputed table that maps hash values back to likely passwords, allowing instant lookup for unsalted hashes. Per-user salts make them useless, because a separate table would be needed for every salt value.

Q7How do you migrate from weak password hashes?

Wrap existing hashes immediately by applying a strong function to the old hash, and record the scheme. Then on each successful login, rehash the plaintext password directly with the new algorithm. Force resets for accounts that never log in.

Q8How do you choose the bcrypt cost or Argon2 parameters?

Benchmark on production hardware and choose the highest settings that keep a login near 0.25 to 0.5 seconds while leaving capacity for peak load. Review the settings periodically, since hardware gets faster, and rehash on login.