Password Hashing: The Right Algorithm, Cost Parameter, and Common Mistakes

If you're stuck between bcrypt, Argon2, and PBKDF2, or you don't know what to set the cost parameter to, this guide gives you exact numbers, real configurations, and common mistakes.

7 min Updated 16 Nov 2026

Your database is full of password hashes and now you have to decide: keep the same bcrypt, migrate to Argon2id, or just raise the cost parameter? If your answer to this question is "it depends," this text is for you. Here we bring you the numbers, configurations, and real limits of each algorithm so you can decide.

Which password hashing algorithm should we choose today

The recommended order in 2024 is clear: Argon2id first, then scrypt, then bcrypt, and PBKDF2-HMAC-SHA256 only when you have to (for example FIPS or a library that doesn't have Argon2). This order comes from the OWASP Password Storage Cheat Sheet, and the reason is simple: Argon2id has both memory resistance and performs well against side-channel attacks on CPU and GPU.

bcrypt still isn't bad. Its problem is that it has a 72-byte input limit and silently ignores anything beyond that. If you feed long passwords to bcrypt with a SHA-256 pre-hash, you have to watch out for length extension attacks; the correct approach is to use HMAC-SHA384 or SHA-512 as the pre-hash, not plain SHA-256.

Algorithms you should no longer see

MD5, SHA-1, plain SHA-256, and anything without a salt. If in the users table you have a column named password whose value is 32 hex characters, it's probably MD5. A quick test: take the hash of a specific password with md5sum and compare it with the database value. If they match, you need to write a migration program that very moment.

Plain SHA-256 is just as bad. Its speed on an ordinary graphics card reaches billions of hashes per second. It also has no cost parameter to slow it down.

What should we set the cost parameter to

Simple rule: hashing a password should take between 100 and 500 milliseconds on your production hardware. Less than 100 milliseconds means the attacker brute-forces faster; more than 500 milliseconds means the user waits behind the login form and your server chokes under concurrent login load.

For bcrypt, the cost parameter is the base-two logarithm. cost=12 means 2^12 rounds, about 250 milliseconds on an ordinary core. cost=10 is about 60 milliseconds and isn't enough for most projects. For Argon2id, the OWASP recommended configuration is:

m=19456 (19 MiB), t=2, p=1

That means 19 megabytes of memory, two iterations, and one thread. If your server has enough RAM, m=65536 and t=3 is also a good option. Important point: add up the memory parameter with the number of concurrent users. 19 megabytes times 200 concurrent logins is about 3.8 gigabytes. If you don't account for this, the server hits swap under login load and your TTFB goes from 200 milliseconds to several seconds.

Why cost=14 isn't right for everyone

In one project I saw the security team set cost to 14, and after deployment the login page timed out during busy hours. The problem wasn't the algorithm; the problem was that each login took 1.2 seconds of CPU and 50 concurrent logins created a queue. The solution: they brought cost back to 12 and instead put a rate limit on the login endpoint. This is where they make a mistake: they think raising cost is always more secure. In reality, high cost without a rate limit only creates a new DoS vector.

Migrating from bcrypt to Argon2id without downtime

You can't change the password hashing algorithm overnight, because you don't have the user's raw password. The standard approach is this: add an algo column to the user table that takes the value bcrypt or argon2id. At login, verify the hash based on this column. If it was bcrypt and it was correct, re-hash it with Argon2id that very moment and update the column. This is called "rehash on login" and migrates most active users within a few weeks.

For users who no longer log in, set up a background script that after, say, 90 days, replaces the remaining hashes with a random, unrecoverable password and asks the user to recover the password via email. This raises security, but the cost is that you lose some inactive users. If your business depends on low-activity users, make this decision with the product team, not just the security team.

Salt, pepper, and common implementation mistakes

The salt must be random per user and at least 16 bytes. bcrypt and Argon2 store the salt inside the output themselves, so you don't need a separate column. But pepper (a fixed string kept separate from the database) must be added manually. If you put the pepper in the same database, it's worthless.

Another common mistake: comparing hashes with == instead of hash_equals or constant_time_compare. This opens a timing attack window. In PHP use password_verify, which performs the secure comparison itself. In Python, argon2-cffi and passlib do the same.

A practical note: if you work on Linux servers, make sure the Argon2 library is installed. On Debian and Ubuntu install the libargon2-1 package and enable the sodium extension in PHP. Without these, your code silently falls back to a weak fallback instead of Argon2. This kind of error doesn't show up in the logs and you only find out when a penetration test is performed.

When a password leaks: the role of infrastructure

A good hash isn't enough. If an attacker gains access to the database, an Argon2id hash only buys time, not absolute security. You must make sure that database access is restricted, backups are encrypted, and access logs to the users table are reviewed. For configuring the access level of sensitive files like the database configuration file, the guide to configuring secure file permissions in Linux is a good starting point.

Also, if your login endpoint is exposed to automated attacks, rate limiting and IP blocking are necessary. The guide to blocking malicious IPs covers control panel, htaccess, and firewall methods. For the network layer, DDoS protection is also part of the picture, but it isn't a substitute for correct hashing.

If you work on cloud infrastructure and want to know what security service is available for the higher layers, ServerNet security services is an option you can check out. But the decision about the password hashing algorithm is one you have to make yourself; no external service takes the place of this decision.

Frequently asked questions

Is bcrypt still secure or should I migrate to Argon2?

bcrypt with an appropriate cost (12 or higher) is still secure for most applications. But if you're starting a new project or can do a gradual migration, Argon2id is the better choice because it has memory resistance and performs better against GPU and ASIC attacks.

Migration isn't mandatory, but if your passwords have been stored with bcrypt for more than five years, it's a good time to review.

What should we set the cost parameter in bcrypt to?

The number 12 is a reasonable starting point and takes about 250 milliseconds on most servers. If your server is powerful and login load is low, 13 is also acceptable. Above 14 without a rate limit isn't recommended.

More important than the number is real measurement on your own hardware. Take a simple benchmark with password_hash in PHP or bcrypt.hashpw in Python and look at the time.

Should I combine the password with SHA-256 before hashing?

Only in one case: when you use bcrypt and passwords may be longer than 72 bytes. In this case use HMAC-SHA384 or SHA-512, not plain SHA-256. For Argon2id this isn't necessary because it has no input length limit.

If you do this pre-hash wrong, security decreases rather than increases. So if you're not sure, use Argon2id and skip this step.

A password hash has leaked in the database; what should I do?

First, invalidate all active sessions and ask users to change their passwords. Second, investigate which path the attacker came through and close that path. Third, if the hashes were with a weak algorithm like MD5 or SHA-1, assume the passwords have been cracked and start an immediate migration.

A strong hash only buys time. If passwords are short and guessable, even Argon2id doesn't hold up well against a dictionary attack. Enforcing a minimum length of 12 characters and checking leaked passwords in services like Have I Been Pwned is part of that same decision.

Next step: take a benchmark on your own server today and see how many milliseconds hashing a password takes. If it's under 100, raise the cost parameter; if it's above 500, set up rate limiting before anything else.

Was this page helpful?