bcrypt vs Argon2: Choosing a Password Hash
Both are deliberately slow. Learn the parameters, the memory cost, and which password hash to choose for a new application.
By Velloreth
Why a password hash is slow
A password hash is deliberately slow. The slowness is the security property.
The arithmetic: a general-purpose hash such as SHA-256 computes billions of hashes per second on a GPU. A password hash is tuned to take 100 to 250 milliseconds. That is a reduction of five orders of magnitude in the attacker’s rate.
The threat model: the attacker has the database and wants the passwords. The cost per guess is what stands between them and the plaintext.
A fast hash is the wrong tool. SHA-256, MD5 and SHA-1 are designed to be fast, which is exactly what an attacker wants.
bcrypt
bcrypt is based on the Blowfish cipher and it has a tunable cost.
$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewKyNiAYMyzJxK
| | | |
| | salt (22 chars) hash
| cost (12 = 2^12 rounds)
version
The cost factor is the base-2 logarithm of the rounds. Each increment doubles the time.
The salt is in the output, so there is nothing to store separately.
The 72-byte limit is the important quirk: bcrypt truncates the input at 72 bytes. A long passphrase is silently truncated, and two passwords that share the first 72 bytes produce the same hash.
The fix for the limit is to pre-hash with SHA-256 and base64-encode, which produces a fixed-length input.
const preHashed = crypto.createHash('sha256').update(password).digest('base64');
const hash = await bcrypt.hash(preHashed, 12);
The null-byte quirk is a related issue in some implementations, and the pre-hash also resolves it.
bcrypt is well understood. It has been analysed for over two decades, and it is available in every language.
Argon2
Argon2 won the Password Hashing Competition in 2015 and is the current OWASP recommendation.
The three variants:
| Variant | Property |
|---|---|
| Argon2d | GPU-resistant, side-channel vulnerable |
| Argon2i | side-channel resistant, GPU vulnerable |
| Argon2id | both, and the one to use |
The parameters:
| Parameter | Meaning |
|---|---|
| memory | the memory cost in KiB |
| iterations | the time cost |
| parallelism | the number of lanes |
| salt | a random value |
| hashLength | the output length |
The memory cost is the key addition. A GPU has many cores and limited memory per core, so a hash that requires 64 MiB per guess limits the parallelism. This is what makes Argon2 stronger than bcrypt against a GPU attack.
A recommended configuration (OWASP):
memory: 19456 KiB (19 MiB)
iterations: 2
parallelism: 1
A higher memory cost is better if the hardware allows it. 64 MiB with 3 iterations is a common choice for a server.
The output encodes the parameters, so a hash can be verified after the parameters change.
$argon2id$v=19$m=19456,t=2,p=1$c29tZXNhbHQ$...
The comparison
| Concern | bcrypt | Argon2id |
|---|---|---|
| Memory cost | no | yes |
| GPU resistance | moderate | strong |
| Availability | everywhere | most languages |
| Analysis | 20+ years | since 2015 |
| Input limit | 72 bytes | none |
| Recommendation | acceptable | preferred |
Choosing
A new application: Argon2id, with the parameters tuned to the hardware.
An existing application on bcrypt: keep it. A migration is a real cost for a marginal gain, and bcrypt is not broken.
A language without a good Argon2 library: bcrypt with a cost of 12 and a pre-hash for the length limit.
A system that must be FIPS-compliant: PBKDF2, which is the approved option.
Never: SHA-256, MD5, SHA-1, or a plain hash with a salt.
Tuning the cost
The target is 100 to 250 milliseconds on the production hardware.
Measure it. A cost that is right on a laptop is wrong on a small server.
const start = Date.now();
await bcrypt.hash('test', 12);
console.log(Date.now() - start, 'ms');
Re-tune as the hardware changes. A cost that was right three years ago is cheap on a new CPU.
Store the parameters with the hash. The output already encodes them, which is what allows a rehash on login.
Migrating
Store the algorithm with the hash, which both formats do.
On a successful login, rehash if the algorithm or the parameters are old.
const valid = await verify(password, user.passwordHash);
if (valid && needsRehash(user.passwordHash)) {
user.passwordHash = await hash(password);
await user.save();
}
The migration completes as users log in. No reset is required.
A forced reset is the alternative, and it is a worse experience for the same result.
A pepper
A pepper is a secret added to the password before hashing, stored outside the database.
const peppered = password + process.env.PASSWORD_PEPPER;
const hash = await argon2.hash(peppered);
The benefit: a stolen database alone is not enough to start an attack.
The cost: the pepper must be available at login, so it lives in a secret manager, and a rotation requires a rehash.
It is a defence in depth, not a replacement for a slow hash.
Common mistakes
A general-purpose hash. SHA-256 is fast, which is the wrong property.
A cost that is too low. A cost of 4 is fast and weak.
A cost that is too high. A cost of 20 makes a login take seconds and is a denial of service.
Ignoring the bcrypt 72-byte limit. A long passphrase is truncated.
A salt that is not random. A fixed salt allows a rainbow table.
A salt that is stored separately and lost. The salt is in the hash output; do not store it twice.
Comparing hashes with ==. Use a constant-time comparison, which the library provides.
Logging the password. It appears in a log, a crash report or an error tracker.
A pepper in the source code. It is not a secret if it is in the repository.
A quotable summary
A password hash is deliberately slow, and the slowness is the security property: a hash that takes 100 milliseconds instead of a microsecond reduces an attacker’s rate by five orders of magnitude. Argon2id is the current recommendation because it adds a memory cost that limits a GPU attack, and bcrypt is still acceptable and more widely available. Tune the cost to the production hardware, store the parameters with the hash, and never use a general-purpose hash such as SHA-256 for a password.
Related reading
For the storage side, see password-storage-best-practices and password-hashing-guide. For the broader context, hashing-vs-encryption and two-factor-authentication-guide.