← blog · November 24, 2026

Storing Passwords Properly: Argon2id or bcrypt, Measuring Parameters, Pepper and Upgrading Old Hashes

When the database leaks, the cost of the hash function is the only thing protecting passwords. Measuring Argon2id parameters on your hardware, concurrent logins exhausting memory, bcrypt's 72 byte limit, pepper rotation and upgrading old hashes without users noticing.

Password storage is one of those jobs most teams set up once and forget. Sign-up works, login works, the database holds an unreadable string, and the topic is considered closed. The real question is what happens when the database leaks. After a breach, the attacker can test guesses against their copy of the table on their own hardware, as fast and for as long as they like. The only purpose of password hashing is to make that offline guessing expensive. This post covers which algorithm to pick and why, how to choose parameters, and how to upgrade old hashes without bothering users.

Why a fast hash is the wrong tool

General purpose hash functions such as SHA-256 or MD5 are built to be fast. A single GPU computes billions of SHA-256 hashes per second. A salt defeats precomputed tables but does nothing to slow down individual guesses, so most common passwords fall to dictionary and rule-based attacks within minutes.

Password hashing functions are deliberately slow and tunable. Four options are taken seriously today:

  • Argon2id lets you tune both time and memory. The memory requirement makes massively parallel guessing on GPUs and custom hardware costly. This is the default choice for a new system.
  • scrypt is also memory-hard and was the best option before Argon2. It is a sensible fallback where you cannot get an Argon2id library.
  • bcrypt is roughly thirty years old, has a mature library in every language and only has a time cost. Still acceptable, with known limits covered below.
  • PBKDF2 may be the only option in environments that require FIPS compliance. It has no memory hardness and is the weakest against GPUs, so use it only when compliance demands it, and with a high iteration count.

My position is simple: pick Argon2id for new projects. If you already run bcrypt, there is no emergency, but you can start producing Argon2id hashes using the gradual upgrade approach below.

Choosing parameters

The OWASP Password Storage Cheat Sheet gives 19 MiB of memory, 2 iterations and a parallelism of 1 as the minimum configuration for Argon2id. That is a floor, not a target. The right values are measured on your own hardware: decide how long a login may take (a few hundred milliseconds is typical for web applications), raise memory as far as your servers can afford, then raise the iteration count until you hit that time.

The calculation most teams skip is concurrency. A setting that uses 64 MiB per login is fine for a single request. When 50 logins arrive at once, the process wants more than 3 GiB. In a memory-limited container the kernel kills the process, and from the outside all anyone sees is a login endpoint that occasionally falls over. After picking parameters, cap concurrency on the login endpoint or run verification in a separate worker pool of known size.

For bcrypt, use a cost factor of at least 10; each increment doubles the time. If you are stuck with PBKDF2-HMAC-SHA256, the current OWASP recommendation is 600,000 iterations.

An example in Python and PHP

In Python, the argon2-cffi library bundles verification, parameter encoding and the rehash decision into one object:

from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError

ph = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)

def register(password: str) -> str:
    return ph.hash(password)

def login(stored: str, password: str) -> tuple[bool, str | None]:
    try:
        ph.verify(stored, password)
    except VerifyMismatchError:
        return False, None
    if ph.check_needs_rehash(stored):
        return True, ph.hash(password)
    return True, None

memory_cost is in KiB, so 19456 KiB is 19 MiB. The output looks like $argon2id$v=19$m=19456,t=2,p=1$...: the algorithm, parameters, salt and hash are stored together. You do not need a separate salt column, and when you change parameters, old records keep verifying with their own.

PHP has the same flow built in:

$opts = ['memory_cost' => 19456, 'time_cost' => 2, 'threads' => 1];
$hash = password_hash($password, PASSWORD_ARGON2ID, $opts);

if (password_verify($password, $stored)) {
    if (password_needs_rehash($stored, PASSWORD_ARGON2ID, $opts)) {
        $new = password_hash($password, PASSWORD_ARGON2ID, $opts);
    }
}

PASSWORD_ARGON2ID is only defined when PHP was built with Argon2 support. Distribution packages usually have it; check your own images.

Upgrading existing hashes

When you want stronger parameters or a different algorithm, you do not have the plaintext passwords. There are two techniques.

Rehash on login. As in the example above: when a user logs in with the correct password, the plaintext is in memory, so you hash it with the new parameters and update the record. Simple and safe, but it only covers users who log in. Accounts that have been idle for a year keep the old hash.

Wrapping. Feed the old hash into the new algorithm: store argon2id(old_hash) and mark the record as wrapped. One batch job migrates the whole table and users notice nothing. At login you compute the old function first, then Argon2id; once verification succeeds you replace the record with a plain Argon2id hash. This is the right way to get rid of genuinely weak hashes such as unsalted MD5, because the weak hash does not sit in the table for even one more day.

Use both: wrap to protect the whole table overnight, rehash on login to move records to the clean format over time.

Pepper: what it buys and what it costs

A pepper is a key kept in the application's secret store rather than in the database. The usual approach is to run the password, or the resulting hash, through HMAC with that key. The benefit: an attacker who only obtained the database, for example through SQL injection or a leaked backup, cannot start guessing at all without the key.

The cost is rotation. If the pepper changes, none of the existing hashes verify. Store a pepper version with each record and keep the old key until migration finishes. If the pepper ends up in the same backup as the database, you have gained nothing.

Pitfalls

bcrypt's 72 byte limit. bcrypt uses the first 72 bytes of input and silently drops the rest. Long passphrases and multi-byte UTF-8 characters reach the limit sooner than expected. A common workaround is to hash the password with SHA-256 first and pass that to bcrypt, but a raw digest can contain zero bytes, and some bcrypt implementations stop reading at a zero byte. If you prehash, base64-encode the digest first. The cleaner fix is to move to Argon2id.

Username enumeration. A login endpoint that returns immediately for unknown users and spends 300 milliseconds hashing for known ones tells anyone measuring response times which email addresses are registered. Verify against a dummy hash when the user does not exist.

Unbounded password length. Argon2id compresses long input internally, so cost barely grows with length, but still cap request body size and the password field. A few hundred characters is a reasonable ceiling; accept at least 64 so that password manager users are not penalised.

Unicode differences. The same-looking password can arrive as different byte sequences from different devices. If registration and login do not apply the same normalization (usually NFC), users with the correct password cannot log in.

Test settings leaking into production. Using cheap parameters to speed up tests is normal. If those parameters are switched by an environment variable, they can leak into production. Add a startup check that refuses any setting below the floor in the production environment.

When not to store passwords at all

The best password table is the one you do not have. For an internal application where an identity provider exists, delegate authentication to it over OIDC or SAML, so password policy, lockout, multi-factor authentication and breach monitoring live in one place. In a consumer product, passkey support moves passwords out of the primary role. When you do have to store them, the list above is enough: Argon2id, measured parameters, bounded concurrency, gradual upgrades and a properly stored pepper.