All articles

bcrypt and Argon2: why passwords are hashed differently

SHA-256 is an excellent hash – and still the wrong tool for passwords. On deliberate slowness, memory hardness and the question of what to pick today.

In the last post I took a bcrypt hash apart and claimed along the way that password hashing works fundamentally differently from the hashing you otherwise know. Time to make good on that.

Because anyone who knows hash functions from an apprenticeship or a degree usually has SHA-256 or MD5 in mind. And anyone who approaches passwords with that in mind reliably builds something broken.

The misunderstanding: speed

SHA-256 is an outstanding hash. It is fast, it is well studied, it distributes cleanly. If I want to check whether a downloaded file was damaged in transit, that is exactly the right property: I want the checksum over a few gigabytes in seconds, not in hours.

With passwords, that property turns against you.

To see why, you have to picture the attack. The interesting case is not someone trying passwords at the login form – that gets noticed, and a lockout after too many failed attempts deals with it. The interesting case is: the database is gone. A backup lay exposed, an SQL injection, a compromised service provider. The attacker has the hashes and guesses offline from now on, on their own hardware, without time pressure and without anyone noticing.

And now the speed becomes the problem. An ordinary graphics card computes SHA-256 in the order of billions of hashes per second. Against a list of the most common passwords, such a database is through in minutes. Not because SHA-256 is broken – but because it does exactly what it was built for, only this time to the attacker’s benefit.

Why the salt only solves half the problem

The usual next step is a salt: a random value per password, hashed along with it and stored in the clear beside it.

That helps too – only against something other than many people assume. A salt stops precomputed tables from working, and it ensures that two users with the same password do not get the same hash. So the attacker can no longer go after the whole database in one sweep, but has to take on each entry individually.

What the salt does not do: slow down a single attack. Anyone targeting one particular account still computes billions of attempts per second. The salt multiplies the work, but it does not make the individual attempt any more expensive.

That is exactly where the methods discussed here come in.

bcrypt: deliberately slow

bcrypt turns the requirement around. It is good not despite but because of its slowness. And that slowness is adjustable – via the cost factor I already pointed out inside the hash in the last post:

$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj/rCWDfe1Nu
    └┬┘
     └─ cost factor: 2¹² = 4096 rounds

The 12 is an exponent, not a factor. It means 2¹² – that is, 4096 rounds of the internal, deliberately expensive key setup. The decisive point: every increase by one doubles the effort. Going from 12 to 13 is not a small adjustment, it is twice the computing time – for me when logging in just as much as for the attacker when guessing.

That is the real trick. Hardware gets faster, and the parameter simply grows with it. A hash stored today with cost factor 12 can be recomputed with 14 in five years’ time, without changing anything about the method.

There are two things worth knowing about bcrypt, though.

The first is a practical pitfall: bcrypt processes at most 72 bytes. Anything beyond that is silently truncated. Choosing a 90-character passphrase therefore makes you no safer than the first 72 – and you get no indication of it. Anyone running passwords through another hash first to get around this limit should know exactly what they are doing, because there is plenty to get wrong there.

The second is the reason there is a successor: bcrypt needs only about 4 KB of memory. That is little enough to fit comfortably into the fast cache of a GPU – and that lets an attacker run a great many computations at once. bcrypt makes the individual attempt expensive, but it stops nobody from computing ten thousand of them in parallel.

Argon2: expensive in memory

This is where Argon2 comes in, the winner of the 2015 Password Hashing Competition and, since 2021, described in RFC 9106.

The idea: if computing time can be beaten with more silicon, then the method had better cost something that cannot be multiplied so easily. And that is memory. While hashing, Argon2 fills a configurable region of memory and accesses it repeatedly. Anyone wanting to run a thousand computations in parallel needs a thousand times that memory – and on a graphics card or a dedicated chip, memory is the expensive resource that cannot be added at will.

Accordingly, Argon2 has three parameters instead of one:

  • m – how much memory is used (in KiB)
  • t – how many passes run over that memory
  • p – how many lanes of computation run in parallel

And there are three variants, which readily cause confusion:

  • Argon2d accesses memory in a data-dependent way. Fast and strong against GPU attacks, but the access patterns depend on the password – and can therefore, in theory, be observed through side channels.
  • Argon2i accesses memory independently of the data and is therefore immune to such side channels, but weaker when the number of passes is low.
  • Argon2id is the mixture: the first part runs data-independently, the rest data-dependently. This is the variant to pick unless you know something to the contrary.

The finished hash carries its parameters around just as bcrypt’s does, only in more detail:

$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG
 └──┬───┘ └┬─┘ └──────┬──────┘ └────┬────┘ └──────────────┬───────────────┘
    │      │          │             │                     └─ hash
    │      │          │             └─ salt
    │      │          └─ 19 MiB memory, 2 passes, 1 lane
    │      └─ version 1.3 (0x13 = 19)
    └─ variant

Here too: everything needed to verify is in the hash itself. Which is why logins keep working even long after the parameters have been raised – the old hash brings its own along.

So what do I pick?

The OWASP recommendation for password storage is clearer in its ranking than many expect:

  1. Argon2id – first choice
  2. scrypt – if Argon2id is not available
  3. bcrypt – explicitly for legacy systems
  4. PBKDF2 – where FIPS-140 compliance is required

So bcrypt is not broken, but it is no longer what you start something new with.

As starting values, OWASP names m=19456 (19 MiB), t=2, p=1 as a minimum for Argon2id, or alternatively m=47104 (46 MiB) with t=1. For bcrypt, a cost factor of at least 10 applies; in practice one tends to use 12 today.

Those numbers are only the starting point, though. The actual rule is: as expensive as possible without making the login unreasonable. And that cannot be read off a table, it has to be measured on your own hardware – with the server metal you actually have, and under load, not on an idle development machine. A good target is in the region of 100 milliseconds per check; if you have more headroom, take more. And do not forget: this cost is incurred on every single login, and correspondingly often when the login page gets hammered.

From the old to the new

That leaves the question of what happens to the hashes already in the database. They cannot be converted – that is rather the whole point.

The usual route is to do it at login time: the user enters their password, it is checked against the old hash as before, and if it matches, the plaintext password is available for a moment anyway. That is exactly when it gets rehashed with the new method and replaced. Whoever logs in migrates along, without noticing a thing. The same approach works, incidentally, when you only raise the cost factor – the libraries usually ship a ready-made query for whether an existing hash still matches the current parameters.

For everyone who does not come back for years, the only remaining option is eventually to invalidate the leftovers and have them set a new password via “forgot password”.

And the most important thing last

Do not build any of this yourself. No homemade construction out of SHA-256 in a loop, no self-invented salt scheme. Every platform worth taking seriously ships vetted implementations, and the right job is to choose suitable parameters and review them regularly – not to reimplement cryptography.

If you want to look at the two formats in peace: bcrypt.rellit.de and argon2.rellit.de produce and verify the hashes in the browser, breaking down every component the way I have described here. The most illuminating thing is to turn the cost factor up step by step and watch the computation become noticeably sluggish. That effect is precisely the protection.