Skip to content
Velloreth
Cybersecurity 5 min read

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

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:

VariantProperty
Argon2dGPU-resistant, side-channel vulnerable
Argon2iside-channel resistant, GPU vulnerable
Argon2idboth, and the one to use

The parameters:

ParameterMeaning
memorythe memory cost in KiB
iterationsthe time cost
parallelismthe number of lanes
salta random value
hashLengththe 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

ConcernbcryptArgon2id
Memory costnoyes
GPU resistancemoderatestrong
Availabilityeverywheremost languages
Analysis20+ yearssince 2015
Input limit72 bytesnone
Recommendationacceptablepreferred

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.

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.

Frequently asked questions

Which is better, bcrypt or Argon2?
Argon2id is the current recommendation from OWASP and the winner of the Password Hashing Competition, because it adds a memory cost that makes a GPU attack expensive. bcrypt is still acceptable and is more widely available. For a new application, Argon2id is the better default; for an existing one, bcrypt is not a reason to migrate urgently.
Why is a password hash deliberately slow?
A slow hash makes each guess expensive. An attacker with a stolen database tries billions of guesses, and a hash that takes 100 milliseconds instead of a microsecond reduces the rate by five orders of magnitude. The slowness is the security property, not a defect.
What is the bcrypt cost factor?
It is the base-2 logarithm of the number of rounds. A cost of 10 means 2^10 rounds. Each increment doubles the time. A cost of 10 to 12 is a common range, and it should be tuned so a hash takes about 100 to 250 milliseconds on the production hardware.
What is the difference between Argon2i, Argon2d and Argon2id?
Argon2d is resistant to a GPU attack and vulnerable to a side-channel attack. Argon2i is the reverse. Argon2id is a hybrid that is resistant to both, and it is the variant to use for a password.
What is a pepper and should I use one?
A pepper is a secret value added to the password before hashing, stored outside the database. It means a stolen database alone is not enough to start an attack. It is a defence in depth, and it must be stored in a secret manager rather than in the code.
Can I migrate from bcrypt to Argon2?
Yes, without forcing a reset. Store the algorithm with the hash, and on a successful login with the old algorithm, rehash the password with the new one and update the record. The migration completes as users log in.