API Reference

argon2

Native Node.js bindings for the Argon2 password hashing winner of the Password Hashing Competition.

Official Documentation ↗
CompatibilityThis documentation covers version v0.41. Check the official docs for the latest API surface.

Installation

npmnpm install argon2
pnpmpnpm add argon2

Methods

Native Node.js bindings for the Argon2 password hashing winner of the Password Hashing Competition.

Overview

The argon2 package wraps reference Argon2 implementations as prebuilt native bindings. hash produces a self-describing encoded string, algorithm version, memory, iterations, parallelism, salt, digest, and verify replays exactly those parameters.

Because parameters live inside the stored hash, they can be raised for new passwords while old hashes still verify, enabling gradual rehash-on-login migrations. The package exposes the three variants, with argon2id as the sensible default for credential storage.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • argon2.hash — Hashes a password with tuned cost parameters and returns the encoded string carrying all parameters.
  • argon2.verify — Recomputes the hash with the parameters embedded in the stored string and reports a match boolean.
  • type constants — Selects the algorithm variant: argon2id balances side-channel resistance and GPU resistance for passwords.

Usage Notes

  • Budget hashing latency: it is intentional work, so run it off the hot path for bulk imports with a queue.
  • Rehash silently on successful login when stored parameters are below your current policy.
  • Prebuild binaries cover common platforms; verify installation in CI images before deploying.

Common Pitfalls

  • Dropping memoryCost to speed tests also removes the property that makes Argon2 strong; use low costs only in fixtures.
  • Storing parameters separately from the hash string creates drift; the encoded string already carries them.
  • verify rejects on malformed hash strings; treat malformed records as compromised and force reset.

Security notes

Quoted from the official documentation (https://github.com/ranisalt/node-argon2); this is not a complete threat model.

FAQ

Why argon2id over bcrypt?
Argon2 is memory-hard: memoryCost forces attackers to spend RAM per guess, which dramatically raises the cost of GPU and ASIC cracking. bcrypt has no meaningful memory requirement, and Argon2id additionally resists side-channel trade-offs.
How do I tune the cost parameters?
Target a hash time around 100-250ms on production hardware: raise memoryCost first because memory is the strongest lever, then timeCost. Store parameters in the hash string so verification always matches what was used at signup.
What should I return when verification fails?
The same generic response you return for unknown users, with comparable latency. Avoid revealing whether the account exists, and pair verification failures with consistent logging and rate limiting on the login route.