Authentication

Password Hashing in PHP, Python and Node: Same Algorithms, Three Different Ways to Get It Wrong

I timed Argon2id, bcrypt and scrypt in PHP 8.6, Python 3.15 and Node 26 and tested each one's edge cases. PHP and bcryptjs still accept two different passwords that share their first 72 bytes, Python and Node both reject OWASP's own top scrypt setting at their default memory limit, and Node's...

a close up of a padlock on a door
Photo by Kaffeebart / Unsplash

Password storage is one of the few areas of security where the right answer has been settled for years. Use a slow, salted, memory-hard function, store its parameters with the hash, and compare in constant time. Argon2id if you can, scrypt if you can't, and bcrypt if you're stuck on something old. That's the entire OWASP Password Storage Cheat Sheet in two sentences.

And yet every language wraps those same three algorithms differently: different defaults, different limits, different output formats, and different ways of failing quietly. If you maintain a PHP app, a Python service and a Node API that share one users table, which describes a lot of real companies, those differences decide whether your logins work, how many per second you can handle, and whether a password that shouldn't match does. So I took PHP 8.6.0, Python 3.15.2 and Node 26.13.1 (with 24.22.3 for comparison) and checked each one.

Password Hashing Defaults in PHP, Python and Node

Here's what each one does if you don't pass any options, next to what OWASP recommends:

Default algorithm Default parameters Output
PHP password_hash($pw, PASSWORD_DEFAULT) bcrypt cost 12 (was 10 before 8.4) $2y$12$...
PHP PASSWORD_ARGON2ID Argon2id m=65536 KiB, t=4, p=1 $argon2id$v=19$m=65536,t=4,p=1$...
Python stdlib none for Argon2 or bcrypt; hashlib.scrypt has no defaults for n, r, p you choose everything raw bytes
Python argon2-cffi PasswordHasher() Argon2id t=3, m=65536 KiB, p=4 (RFC 9106 low-memory profile) PHC string
Python bcrypt 5.0 bcrypt 12 rounds $2b$12$...
Node crypto.argon2Sync() (since 24.7.0) Argon2 (you pick the variant) none, all parameters are required raw bytes
Node crypto.scryptSync() scrypt N=16384, r=8, p=1 raw bytes
OWASP minimum Argon2id m=19456 KiB (19 MiB), t=2, p=1 —

The PHP defaults come from its own constants, and the Python ones from the libraries' attributes, both printed by my test scripts. The Node and OWASP rows come from the Node crypto docs and the cheat sheet. All three languages default to something reasonable. They just don't agree with each other.

What Each Hash Costs

Median of seven hashes each, on a 2-vCPU Xeon VM. PHP:

PHP 8.6.0 | PASSWORD_DEFAULT=2y | argon2 defaults: memory=65536 KiB time=4 threads=1
PASSWORD_DEFAULT                     206.2 ms  $2y$12$Wtd9IU3XFiZP75OCNnPEFuA...
bcrypt cost 10 (pre-8.4 default)      46.5 ms  $2y$10$9Sc/GKhxYHLGEP0k63Bpae8...
bcrypt cost 13                       412.3 ms  $2y$13$imMwzti9gjYhbvphCiWZJeh...
argon2id defaults                    200.0 ms  $argon2id$v=19$m=65536,t=4,p=1...
argon2id OWASP m=19456 t=2 p=1        23.1 ms  $argon2id$v=19$m=19456,t=2,p=1...

Python:

Python 3.15.2 | argon2-cffi 25.1.0 PasswordHasher(): t=3 m=65536 KiB p=4 | bcrypt 5.0.0
argon2-cffi PasswordHasher() defaults   123.5 ms  $argon2id$v=19$m=65536,t=3,p=4...
argon2-cffi OWASP m=19456 t=2 p=1        20.8 ms  $argon2id$v=19$m=19456,t=2,p=1...
bcrypt.hashpw rounds=12 (default)       248.0 ms  b'$2b$12$PLcOnkgeLYv0Mfn3wW9VG...
hashlib.scrypt n=2**14 r=8 p=5          149.7 ms  5a41ed6b4f50f5452ae684b9c3aa4d...

Node 26, then Node 24:

Node v26.13.1 | bcryptjs 3.0.3
argon2Sync OWASP m=19456 t=2 p=1         31.2 ms  be0bed52c51443f1a43c75b15cacb1...
argon2Sync docs example m=65536 t=3 p=4  147.3 ms  6a4911dcf83c5b29528c8a13e3ca8a...
scryptSync defaults (N=16384 r=8 p=1)    36.4 ms  9b05a2adb604ce9a20f8aa769f9e32...
scrypt N=2**17 with default maxmem: ERR_CRYPTO_INVALID_SCRYPT_PARAMS: Invalid scrypt params: error:030000AC:digital envelope routines::memory limit exceeded
scryptSync N=2**17 maxmem=256 MiB       342.7 ms  776a1d9057eb1047b8617a15f1f728...
bcryptjs hashSync cost 12               305.1 ms  $2b$12$UPKastqAHMUtUDyG6q2o8OZ...
...
Node v24.22.3 | bcryptjs 3.0.3
argon2Sync OWASP m=19456 t=2 p=1         23.1 ms  399ebee06cb93502ff11a8287f1173...
argon2Sync docs example m=65536 t=3 p=4  124.6 ms  e01d3d1cccbbbd7a0dbba5e4edf2de...
scryptSync defaults (N=16384 r=8 p=1)    33.6 ms  69500c5ea7d3dcbc809d2aa58e41d7...
scrypt N=2**17 with default maxmem: ERR_CRYPTO_INVALID_SCRYPT_PARAMS: Invalid scrypt params: error:030000AC:digital envelope routines::memory limit exceeded
scryptSync N=2**17 maxmem=256 MiB       344.5 ms  1d4bf572d5277dc5d440727eaee339...
bcryptjs hashSync cost 12               305.1 ms  $2b$12$fa04WBhG1r0IKMJ/M63d.uY...

Pulled into one table (Node 26 numbers):

Configuration PHP 8.6 Python 3.15 Node 26
Argon2id, OWASP minimum (19 MiB, t=2, p=1) 23.1 ms 20.8 ms 31.2 ms
Argon2id, library default 200.0 ms (64 MiB, t=4, p=1) 123.5 ms (64 MiB, t=3, p=4) no default
bcrypt cost 12 206.2 ms 248.0 ms 305.1 ms (bcryptjs, pure JS)
scrypt N=2^17, r=8, p=1 (OWASP's first row) — 337.9 ms 342.7 ms

Two takeaways. First, the OWASP Argon2id floor is cheap, around 20–30 ms. PHP's own Argon2id default is nearly ten times more expensive than that, and Python's runs four lanes in parallel to land at 124 ms. Second, bcrypt at cost 12 costs 200–300 ms everywhere. That's fine for one login and brutal at scale.

That's the number to plan capacity around. With a 200 ms hash and two cores, you max out at about ten logins per second, and the CPU work is identical whether the login is real or a bot stuffing credentials. Hash cost is your defense against offline cracking and also a denial-of-service lever you're handing to anyone who can reach your login form. Rate limiting the login endpoint isn't optional at these costs.

We recommend reading Passkeys Are Replacing Passwords to continue reading our selection of content. The way out of all of this is not storing a password at all, and that's where passkeys come in.

Passkeys Are Replacing Passwords Passkeys bind auth to the device — nothing to phish, nothing to stuff. How the WebAuthn ceremony works, where recovery breaks, and the fallback trap. SudoSecurity · Santhdium

Trap 1: bcrypt Stops Reading at Byte 72

bcrypt only uses the first 72 bytes of its input. That's in the algorithm, not a bug in any one library. What differs is what each library does about it. I hashed one 80-byte password and verified a different 80-byte password that shares the same first 72 bytes:

Library Result
PHP 8.6 password_hash(PASSWORD_BCRYPT) Hashed. The other password verifies: true
Python bcrypt 5.0.0 ValueError: password cannot be longer than 72 bytes, truncate manually if necessary (e.g. my_password[:72])
Node bcryptjs 3.0.3 Hashed. The other password verifies: true
PHP Argon2id (same two passwords) false, as it should be
bcrypt, 80-byte password: hashed; other password verifies too? true
argon2id, 80-byte password: other password verifies too? false

The PHP manual does document it: passwords are "truncated to a maximum length of 72 bytes." It's still a silent truncation in the default algorithm of the most widely deployed web language. 72 bytes sounds like plenty until you remember it's bytes, not characters. A passphrase in emoji, Cyrillic or CJK runs out of room several times faster. And any scheme that glues a pepper or username in front of the password eats into those 72 bytes before the user's input even starts.

Python's bcrypt 5.0 made the right call by raising an error. If you're on PHP or bcryptjs, enforce a 72-byte maximum yourself, measured with strlen() or Buffer.byteLength(), not character count. Or skip the problem and use Argon2id. OWASP describes pre-hashing with a fast hash and base64 as an alternative, then spends three paragraphs on how to get that wrong, including the null-byte and password-shucking problems. I'd switch algorithms first.

Trap 2: OWASP's Own Top scrypt Setting Fails by Default

OWASP's first recommended scrypt configuration is N=2^17, r=8, p=1, using 128 MiB. In both Python and Node it throws:

scrypt n=2**17 with default maxmem: ValueError: [digital envelope routines] memory limit exceeded
scrypt N=2**17 with default maxmem: ERR_CRYPTO_INVALID_SCRYPT_PARAMS: Invalid scrypt params: error:030000AC:digital envelope routines::memory limit exceeded

Both call into OpenSSL, and OpenSSL caps scrypt memory by default. The Python docs say so: maxmem "limits memory (OpenSSL 1.1.0 defaults to 32 MiB)." Raise maxmem (I used 256 MiB) and it works, at about 340 ms per hash in both languages. This is the good kind of failure, because it's loud. The bad version is a developer who hits the error, drops N to 2^14 to make it go away, and ships that. OWASP's lowest row at N=2^14 still requires p=5 to be acceptable.

Trap 3: Node's Argon2 Has No Hash Format

Node 24.7 added crypto.argon2() and crypto.argon2Sync(), which means Argon2 with no native npm module. Read the signature, though: you pass message, nonce, parallelism, tagLength, memory and passes, all required, and you get back a Buffer. There's no salt generation, no encoded output, and no verify function. It's a key-derivation primitive, not a password API.

So you store all of that yourself. The standard way to do it is the PHC string format PHP and Python already produce, which packs the algorithm, version, parameters, salt and hash into one column. Here's a small encoder:

// Node's argon2 returns raw bytes. Encode them as a PHC string that PHP and Python can verify.
import { argon2Sync, randomBytes } from "node:crypto";

const b64 = (buf) => buf.toString("base64").replace(/=+$/, "");
export function hashPassword(password, { memory = 19456, passes = 2, parallelism = 1 } = {}) {
  const nonce = randomBytes(16);
  const tag = argon2Sync("argon2id", { message: password, nonce, memory, passes, parallelism, tagLength: 32 });
  return `$argon2id$v=19$m=${memory},t=${passes},p=${parallelism}$${b64(nonce)}$${b64(tag)}`;
}
console.log(hashPassword(process.argv[2]));

Since everything ends up in the same format, I checked whether a hash from each language verifies in the others:

node argon2id: $argon2id$v=19$m=19456,t=2,p=1$l41QT/HVzxHY4iwJ4CMVuA$j8Gd2O0LtW3sbBjtfRW0sVZQB7hy3ZM6TqYRpUYd0Fc
php  argon2id: $argon2id$v=19$m=19456,t=2,p=1$VkMuS1M2WXBBTmNBSU9DQQ$92wGpB6ghadolW7hJPF7QYyt6ilu1Udvijp+NQFz1IA
php  bcrypt:   $2y$12$qEgUt8sOGewoGfnCruGTl.ALFjDgleZwK8kTZFoaIktD7xBdz0G4m
PHP verifies node argon2id:     true
Python verifies node argon2id:  True
Python verifies php argon2id:   True
Python check_needs_rehash (php): True
Python verifies php bcrypt $2y$: True
bcryptjs verifies php bcrypt:   true

All of them interoperate. A Node-encoded Argon2id hash verifies in PHP's password_verify() and Python's argon2-cffi. PHP's Argon2id verifies in Python. PHP's $2y$ bcrypt verifies in Python's bcrypt and in bcryptjs. If your services share a users table, standardize on PHC strings and you can move logins between stacks without forcing password resets.

The check_needs_rehash ... True line matters too. Python flagged PHP's hash because its parameters differ from PasswordHasher()'s defaults. That's the hook you use to migrate.

On verification in Node: if you write your own verify, compare the derived bytes with crypto.timingSafeEqual(), never === on hex strings.

Trap 4: The Sync Hash Freezes Your Event Loop

PHP and Python block a worker while hashing, but those runtimes have many workers. Node has one event loop. I ran 20 Argon2id hashes back to back with a 1 ms timer ticking, sync and then async:

20 hashes, sync : 517 ms, 1 ms timer fired 0 times
20 hashes, async: 602 ms, 1 ms timer fired 537 times

The sync version froze the process for half a second. The timer didn't fire once, so no other request got served in that time. The async version took slightly longer in total but let the timer fire 537 times. Use crypto.argon2() with a callback or promisify, never argon2Sync in a request handler. Async crypto runs on libuv's threadpool, which has 4 threads by default (UV_THREADPOOL_SIZE). On a login-heavy service, that pool is your hashing concurrency limit.

Sizing for Login Bursts: The Threadpool Is Your Throttle

Async hashing keeps the event loop free, but the work still happens somewhere: libuv's threadpool, and each Argon2 call holds its full memory cost for as long as it runs. Here are 32 logins arriving at once, at the OWASP minimum parameters, with different UV_THREADPOOL_SIZE values on this 2-vCPU box:

// 32 logins arriving at once, hashed with async Argon2id (OWASP minimum: 19 MiB, t=2, p=1).
import { argon2, randomBytes } from "node:crypto";
import { promisify } from "node:util";

const hash = promisify(argon2);
const params = () => ({ message: "correct horse battery staple", nonce: randomBytes(16), parallelism: 1, tagLength: 32, memory: 19456, passes: 2 });

let worstLag = 0, last = performance.now();
const probe = setInterval(() => { const now = performance.now(); worstLag = Math.max(worstLag, now - last - 10); last = now; }, 10);

const t = performance.now();
await Promise.all(Array.from({ length: 32 }, () => hash("argon2id", params())));
const total = performance.now() - t;
clearInterval(probe);
console.log(`UV_THREADPOOL_SIZE=${(process.env.UV_THREADPOOL_SIZE ?? "4 (default)").padEnd(11)} 32 hashes in ${total.toFixed(0).padStart(5)} ms | worst event-loop lag ${worstLag.toFixed(1).padStart(5)} ms | peak RSS ${(process.resourceUsage().maxRSS / 1024).toFixed(0)} MB`);
UV_THREADPOOL_SIZE=4 (default) 32 hashes in   441 ms | worst event-loop lag   9.3 ms | peak RSS 123 MB
UV_THREADPOOL_SIZE=4 (default) 32 hashes in   482 ms | worst event-loop lag   6.7 ms | peak RSS 123 MB
UV_THREADPOOL_SIZE=1           32 hashes in   809 ms | worst event-loop lag   0.8 ms | peak RSS 66 MB
UV_THREADPOOL_SIZE=1           32 hashes in   685 ms | worst event-loop lag   1.0 ms | peak RSS 66 MB
UV_THREADPOOL_SIZE=2           32 hashes in   396 ms | worst event-loop lag   1.3 ms | peak RSS 85 MB
UV_THREADPOOL_SIZE=2           32 hashes in   412 ms | worst event-loop lag   1.0 ms | peak RSS 85 MB
UV_THREADPOOL_SIZE=8           32 hashes in   464 ms | worst event-loop lag  10.6 ms | peak RSS 199 MB
UV_THREADPOOL_SIZE=8           32 hashes in   491 ms | worst event-loop lag  15.8 ms | peak RSS 199 MB
UV_THREADPOOL_SIZE=32          32 hashes in   650 ms | worst event-loop lag  42.3 ms | peak RSS 628 MB
UV_THREADPOOL_SIZE=32          32 hashes in   562 ms | worst event-loop lag 104.5 ms | peak RSS 618 MB
UV_THREADPOOL_SIZE 32 hashes Worst event-loop lag Peak RSS
1 685–809 ms 0.8–1.0 ms 66 MB
2 (= cores) 396–412 ms 1.0–1.3 ms 85 MB
4 (default) 441–482 ms 6.7–9.3 ms 123 MB
8 464–491 ms 10.6–15.8 ms 199 MB
32 562–650 ms 42.3–104.5 ms 618–628 MB

A pool the size of the core count was fastest. Every thread beyond that added no throughput, because there were only two cores to run on. It added about 19 MB of Argon2 memory per extra concurrent hash, and it made the event loop itself laggier, because the hashing threads competed with the main thread for CPU. At 32 threads, a burst of 32 logins took 620 MB and stalled the loop by up to 104 ms.

The PHP equivalent is pm.max_children, and the arithmetic is the same: concurrent hashes × memory cost has to fit in RAM, and concurrent hashes beyond your core count only add queueing. Size the pool (or the workers) to the cores, set the memory cost with the burst in mind, and rate-limit the login route so a credential-stuffing run can't fill the pool. The libuv threadpool is shared with fs, DNS lookups and zlib, so a hashing burst also slows down every file read on the process.

We recommend reading Timing Attacks on RSA in Python to continue reading our selection of content. Constant-time comparison is one piece of a bigger topic, and timing leaks are covered in depth here.

Timing Attacks on RSA in Python Your RSA leaks key bits through how long it takes. Walkthrough of a timing attack on square-and-multiply, measured code, and real defenses. SudoSecurity · Santhdium

Upgrading Old Hashes Without a Password Reset

Every stack has a 2015-era $2y$10$ or unsalted-SHA hash somewhere. The fix is the same in all three languages: at login, when you have the plaintext anyway, verify against whatever's stored, then re-hash under the current policy if it's outdated. PHP has this built in:

<?php
// Upgrade old hashes at login: verify against whatever is stored, then rehash if the policy changed.
const POLICY_ALGO = PASSWORD_ARGON2ID;
const POLICY_OPTS = ["memory_cost" => 19456, "time_cost" => 2, "threads" => 1];

function login(string $password, string &$storedHash): bool {
    if (!password_verify($password, $storedHash)) {
        return false;
    }
    if (password_needs_rehash($storedHash, POLICY_ALGO, POLICY_OPTS)) {
        $storedHash = password_hash($password, POLICY_ALGO, POLICY_OPTS); // save this back to the DB
    }
    return true;
}

$stored = password_hash("hunter2-but-longer", PASSWORD_BCRYPT, ["cost" => 10]); // a 2015-era hash
echo "before: ", substr($stored, 0, 7), "\n";
var_dump(login("hunter2-but-longer", $stored));
echo "after:  ", substr($stored, 0, 31), "\n";
var_dump(password_needs_rehash($stored, POLICY_ALGO, POLICY_OPTS));
before: $2y$10$
bool(true)
after:  $argon2id$v=19$m=19456,t=2,p=1$
bool(false)

One successful login, and the old bcrypt cost-10 hash becomes Argon2id at OWASP parameters. argon2-cffi has the same hook as PasswordHasher.check_needs_rehash(), as the interop test showed. In Node you write it yourself: parse the m=, t= and p= values out of the PHC string and compare them with your policy.

What I'd Standardize On

  • Algorithm: Argon2id everywhere, PHC string format, parameters tuned per box. OWASP's m=19456, t=2, p=1 is the floor. Benchmark on your own hardware and go as high as login latency and concurrency allow.
  • PHP: password_hash($pw, PASSWORD_ARGON2ID, [...]) with explicit options, plus password_needs_rehash() at login.
  • Python: argon2-cffi PasswordHasher with explicit parameters, and check_needs_rehash().
  • Node: async crypto.argon2(), your own PHC encoder, and timingSafeEqual. Or a maintained library that does all three.
  • If you must keep bcrypt: enforce a 72-byte limit yourself in PHP and Node.
  • Everywhere: rate-limit the login endpoint, because your hash cost is an attacker's amplifier.

What This Doesn't Cover

  • Peppers. Node's secret parameter and HMAC-before-hash schemes change your key-management story. That's a topic of its own.
  • PBKDF2 and FIPS. OWASP's FIPS-140 recommendation is PBKDF2-HMAC-SHA256 at 600,000 iterations. I didn't benchmark it here.
  • Native npm modules. I tested the pure-JS bcryptjs, not the native bcrypt or argon2 packages, which are faster.
  • GPU cracking economics. Why memory-hardness matters is well covered elsewhere. These numbers are about the defender's side.
  • Passkeys and passwordless login. The real long-term fix is not storing passwords at all.

The Actual Point

The algorithms are settled, but the wrappers aren't. PHP and bcryptjs will accept two different long passwords as the same one. Python and Node reject OWASP's own top scrypt setting until you raise a memory limit. And Node's Argon2 gives you raw bytes, with storing the parameters left to you. The good news is that the PHC string format makes all three interoperate. Standardize on Argon2id in PHC form, set the parameters explicitly instead of trusting each library's defaults, re-hash at login, and run your own benchmark on your production hardware before you pick numbers.

Sources: the OWASP Password Storage Cheat Sheet, php.net's password_hash() manual, the Node.js v26 crypto documentation, the Python hashlib documentation, the argon2-cffi API documentation, and RFC 9106. All timings and edge cases were run by me on php:8.6-cli, Python 3.15.2 with argon2-cffi 25.1.0 and bcrypt 5.0.0, and node:26-slim and node:24-slim with bcryptjs 3.0.3. Every code and output block is reproduced verbatim from those scripts and runs; the scripts are original code using each language's documented API.

$ tail -f /forums

Discussion

Comments live on the community forums — sign in there to reply.