Nearly everyone knows you "should not store passwords in plaintext" and should "hash with bcrypt." That is almost never where the real problem is. It is in the implementation details around the hash: an uncalibrated cost factor, a botched algorithm migration, a silently truncated input, or confusing a password hash with a generic cryptographic hash. A good algorithm, badly implemented, barely protects more than a bad one.

The concrete problem

A database leak is a "when," not an "if," over a long enough horizon. The question that actually matters is not "did I hash the password" but "if this table leaks tomorrow, how much time and budget would an attacker need to recover plaintext passwords from the hashes?" With SHA-256(password), the answer is: a few hours on a consumer GPU, because SHA-256 is designed to compute fast, which is exactly the opposite of what you want for a password.

Why a fast hash is the actual problem

A generic cryptographic hash (SHA-256, SHA-512) is optimized to verify data integrity at high speed. That speed, a virtue for a file checksum, becomes a weakness for a password: an attacker who gets the hash table can test billions of candidate passwords per second on GPU hardware, far beyond what any human could guess manually.

bcrypt and argon2 fix this by being deliberately slow, and for argon2, memory-hungry as well:

  • bcrypt is built on the Blowfish cipher with a configurable number of rounds (the "cost factor"), which makes the computation tunably slow as hardware gets faster over time.
  • argon2 (winner of the 2015 Password Hashing Competition, recommended by OWASP) adds a memory constraint on top of the CPU constraint, which makes GPU and specialized ASIC attacks noticeably less effective than they are against bcrypt.

Both also bake in an automatically generated random salt, which defeats precomputed rainbow-table attacks and guarantees that two users with the same password never end up with the same stored hash.

Diagnose the existing system first

Before fixing anything, audit what is actually stored in the database:

-- Find suspicious hashes: too short to be bcrypt/argon2,
-- or missing a recognizable algorithm prefix
SELECT id, email, LEFT(password_hash, 10), LENGTH(password_hash)
FROM users
WHERE password_hash NOT LIKE '$2y$%'   -- bcrypt
  AND password_hash NOT LIKE '$2b$%'
  AND password_hash NOT LIKE '$argon2%'
LIMIT 50;

A bcrypt hash always starts with $2y$ or $2b$ followed by the cost factor (e.g. $2y$12$...); an argon2 hash starts with $argon2id$v=19$.... If the table holds plain 32- or 64-character hex strings with no prefix, that is almost certainly bare MD5 or SHA: treat it as the top priority.

The fix, and the implementation traps

Pick a realistic cost factor, not a copy-pasted default

// Node.js, with bcrypt
const bcrypt = require('bcrypt');

// The cost factor must be calibrated against PRODUCTION hardware,
// not left at the library default. Usual target: ~250ms per hash.
const SALT_ROUNDS = 12;

async function hashPassword(plain) {
  return bcrypt.hash(plain, SALT_ROUNDS);
}

async function verifyPassword(plain, stored) {
  return bcrypt.compare(plain, stored);
}

A cost factor left too low (often stuck at 10 out of habit) weakens the protection; set too high, it degrades login UX or saturates CPU under load. The right setting is measured on real infrastructure, not a development laptop.

Argon2: watch all three parameters, not just time

const argon2 = require('argon2');

async function hashPassword(plain) {
  return argon2.hash(plain, {
    type: argon2.argon2id, // always argon2id, not argon2i or argon2d alone
    memoryCost: 19456, // ~19 MiB, current OWASP recommendation
    timeCost: 2,
    parallelism: 1,
  });
}

argon2id is the hybrid mode OWASP recommends for most use cases (resistant to both side-channel attacks AND GPU attacks), unlike argon2i or argon2d alone, each of which only covers part of the threat model. Leaving memoryCost at too low a default cancels out much of argon2's advantage over bcrypt.

bcrypt's silent truncation

A less well-known but real trap: bcrypt silently truncates input at 72 bytes. A 100-character password gets hashed as if it were only 72 characters, with no error or warning depending on the implementation. If the application accepts long passphrases (good UX practice), you need to either document that limit, pre-hash with SHA-256 before bcrypt (only to work around the length limit, never as a substitute for bcrypt itself), or migrate to argon2, which does not have this constraint.

Migrating a legacy algorithm without forcing a mass reset

// Laravel: verify against the legacy algorithm, rehash on the fly
public function attempt(string $email, string $plain): bool
{
    $user = User::where('email', $email)->first();
    if (!$user) {
        return false;
    }

    if (str_starts_with($user->password_hash, 'md5:')) {
        // Legacy path: verify with the old algorithm, migrate on match
        if (md5($plain) === substr($user->password_hash, 4)) {
            $user->password_hash = Hash::make($plain); // bcrypt by default
            $user->save();
            return true;
        }
        return false;
    }

    return Hash::check($plain, $user->password_hash);
}

This "migrate on login" approach avoids forcing every user to reset their password at once, which would be both a bad signal (user panic) and a support load spike. In exchange, it requires keeping the legacy verification path active during the transition, with visibility into what percentage of accounts are still unmigrated.

The tradeoffs worth naming

  • bcrypt vs argon2 for a new project: argon2id is OWASP's current recommendation, but bcrypt remains perfectly acceptable and benefits from a more mature ecosystem in some older languages/frameworks. The real risk is not the choice between the two, it is a miscalibrated cost factor or memory parameter, regardless of which algorithm is used.
  • High cost factor vs response time: an overly aggressive cost factor protects better against offline brute-force but can open a denial-of-service vector if an attacker spams the login endpoint with deliberately invalid passwords, burning the expensive hash computation for nothing. Rate-limiting the authentication endpoint is still necessary alongside this, not instead of it.

When bcrypt/argon2 alone are not enough

Hashing protects against a database leak, not against a weak or reused password. For an exposed application (user accounts with access to sensitive or financial data), this needs to be paired with a check against known compromised password lists (an API like "Have I Been Pwned - Pwned Passwords," queried via k-anonymity so the plaintext password is never sent) and, ideally, with MFA. A strong hash is a necessary condition for account security, not a sufficient one.

Password changes and session invalidation

A detail that often gets missed even in an otherwise correct implementation: changing the hashing parameters (bumping bcrypt's cost factor, switching from bcrypt to argon2) does nothing for accounts that never log in again. A sensible migration policy re-hashes on successful login (shown above), but it should also be paired with monitoring that tracks what fraction of the user base is still on the old algorithm months later, so a forced reset for the remaining stragglers can be a deliberate decision rather than something nobody tracked.

// Periodic check: how many accounts are still on a legacy hash
async function legacyHashRatio(db) {
  const [{ total }] = await db.query('SELECT COUNT(*) AS total FROM users');
  const [{ legacy }] = await db.query(
    "SELECT COUNT(*) AS legacy FROM users WHERE password_hash LIKE 'md5:%'",
  );
  return legacy / total;
}

This is also where password hashing intersects with session design: rehashing a password does not invalidate existing sessions or API tokens issued before the change. If the migration is prompted by a suspected compromise rather than routine hardening, rotating session tokens and forcing re-authentication has to be a conscious, separate step, not an assumed side effect of updating the hash.

Takeaway

The choice of algorithm (bcrypt or argon2id) matters less than how it is implemented: a cost factor calibrated against real hardware, explicit memory parameters for argon2, deliberate handling of bcrypt's 72-byte limit, and a gradual rather than forced migration for any legacy MD5/SHA still lingering in the database.