Choosing Argon2 parameters
Three numbers that look equal and are not. Why memory is the real lever, and how to measure it on your own hardware.
The goal of a password hash is not to hide the password. The password is kept secret elsewhere. The goal is to make each guess expensive enough that guessing in bulk stops being worthwhile.
Why SHA-256 does not work
SHA-256 is designed to be fast. A modern GPU computes billions of them per second — which is exactly the right direction if you are the attacker.
If a database is read and the password column holds SHA-256 values, the attacker does not "decrypt" anything. They hash each candidate and compare — the same operation the server did at sign-up, but run on specialised hardware.
What is needed is a function that is deliberately slow, and slow in a way parallel hardware cannot buy back cheaply.
What Argon2id is
Argon2 has three variants:
- Argon2d — memory access depends on the data. Fast to crack, but opens the door to side-channel attacks.
- Argon2i — memory access is independent of the data. Resists side channels well, but slower on GPUs.
- Argon2id — the first half behaves like Argon2i, the second like Argon2d.
Argon2id is the default choice, and what this site uses. It keeps side-channel resistance for the first pass — where data is most easily observed — then switches to the memory-dependent mode for the rest.
Three parameters, and which one matters
import { hash } from "@node-rs/argon2";
const digest = await hash(password, {
algorithm: 2, // Argon2id
memoryCost: 65536, // 64 MiB
timeCost: 3, // passes
parallelism: 1, // lanes
});These three numbers look like three equal dials. They are not.
memoryCost is the real lever. A GPU has thousands of cores but bounded memory bandwidth. If every hash needs 64 MiB, the number of simultaneous hashes is limited by how many MiB you have — not by how many cores. That is why memory is more expensive than time.
timeCost slows both sides equally. Double it and the attacker slows by two, but so does the user's wait.
parallelism splits the work across lanes within one hash. On a single-core CPU, 1 is correct — raising it only weakens the hash without making it faster.
Proving the parameters
Do not trust someone else's numbers. Measure on the hardware you actually run:
node -e '
const { hash } = require("@node-rs/argon2");
const t = process.hrtime.bigint();
hash("benchmark-password", { algorithm: 2, memoryCost: 65536, timeCost: 3, parallelism: 1 })
.then(() => console.log(Number(process.hrtime.bigint() - t) / 1e6, "ms"));
'A practical target: hashing should take a few hundred milliseconds. Much faster and the attacker is fast too; much slower and users start to notice.
A second ticket if the database is read
Hashing protects the password. It does not protect you from the database file being copied. With a "pepper" — a secret string kept outside the database — an attacker holding the whole hash table still cannot crack anything until they also obtain the pepper.
If you remember one thing from this post: the parameter worth raising is memoryCost, because it is the one an attacker cannot buy back with more cores.
Comments
Get new posts by email
No ads, no tracking. Just new posts, unsubscribe any time.