Argon2 Hashing for Node.js

0.44.0 · active · verified Sun Apr 19

node-argon2 is a Node.js library providing native bindings to the reference Argon2 hashing algorithm, which is a key derivation function designed to be memory-hard and suitable for password hashing. The library is actively maintained with frequent minor and patch releases, currently stable at version 0.44.0. It aims to simplify the use of Argon2 in Node.js applications by offering prebuilt binaries for common platforms (since v0.26.0, expanded significantly in v0.40.0), reducing the need for local compilation. Key differentiators include robust TypeScript support, flexibility in configuring Argon2 parameters (e.g., time cost, memory cost, parallelism), and the ability to choose between Argon2i, Argon2d, or Argon2id variants, making it a secure choice for password storage. It requires Node.js >= 16.17.0, with Node 18 or 20 being recommended.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to hash a password using default settings, verify a password against a hash, and hash with custom Argon2 parameters.

import { hash, verify } from 'argon2';

async function main() {
  const password = process.env.USER_PASSWORD ?? 'mySecurePassword';
  let hashedPassword: string;

  try {
    // Hash a password with default options (Argon2id)
    hashedPassword = await hash(password);
    console.log('Hashed password:', hashedPassword);

    // Verify a password against a hash
    if (await verify(hashedPassword, password)) {
      console.log('Password matched!');
    } else {
      console.error('Password did not match.');
    }

    // Example of hashing with custom options (e.g., Argon2i, higher memory cost)
    const customHashedPassword = await hash(password, {
      type: 1, // argon2.ArgonType.Argon2i
      memoryCost: 1024,
      timeCost: 4,
      parallelism: 2
    });
    console.log('Custom hashed password:', customHashedPassword);

  } catch (err) {
    console.error('An error occurred:', err);
  }
}

main();

view raw JSON →