Argon2 Password Hash With Params

Hash passwords with Argon2id using explicit memory time and parallelism parameters and verify with constant-time comparison.

JavascriptAuth Jwt

Code

javascriptsnippet
1 line
import{argon2id,verify}from'argon2';export async function hashPassword(pw){return argon2id(pw,{memoryCost:19456,timeCost:2,parallelism:1,hashLength:32});}export async function verifyPassword(hash,pw){try{return await verify(hash,pw);}catch{return false;}}export function needsRehash(hash,{memoryCost=19456,timeCost=2}={}){return!hash.includes('m='+memoryCost)||!hash.includes('t='+timeCost);}

From first line to last

Argon2 Password Hash With Params keeps everything in the functions hashPassword, verifyPassword and needsRehash.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. Everything borrowed from argon2 is visible in one line: argon2id, verify.

What to watch for

  • Dependency check: argon2 — not built-in, must be present in the project.

Applying it

The main signature reads export async function hashPassword(pw)

hashPassword is the public entry point; everything else in the file exists to support it.

Interactive Tools for This Topic

FAQ

What does this need from the package registry?
These are real dependencies: `argon2`. Without it the snippet will not load.