1.0.23 • Published 30 days ago

argon2-wasm-edge v1.0.23

Weekly downloads
-
License
MIT
Repository
github
Last release
30 days ago

About

This library is heavily inspired by hash-wasm. It only includes the Argon2 algorithm and can run on Vercel Edge functions and Cloudflare Workers.

Hashing passwords with Argon2

Parameters

hasingParams.ts

The recommended process for choosing the parameters can be found here.

export default {
  parallelism: 1,
  iterations: 256,
  memorySize: 512, // use 512KB memory
  hashLength: 32, // output size = 32 bytes
  outputType: 'encoded', // return standard encoded string containing parameters needed to verify the key
};

Usage on standard Node and Bun environments

import params from './hasingParams';
import { argon2id, argon2Verify } from 'argon2-wasm-edge';


async function run() {
  // salt is a buffer containing random bytes
  const salt = new Uint8Array(16);
  window.crypto.getRandomValues(salt);

  const key = await argon2id({ ...params, password: 'pass', salt });

  console.log('Derived key:', key);

  const isValid = await argon2Verify({
    password: 'pass',
    hash: key,
  });

  console.log(isValid ? 'Valid password' : 'Invalid password');
}

run();

Usage on Vercel Edge

api/hash-password.ts

import params from './hasingParams';

import { argon2id, setWASMModules } from 'argon2-wasm-edge';
// @ts-expect-error
import argon2WASM from 'argon2-wasm-edge/wasm/argon2.wasm?module';
// @ts-expect-error
import blake2bWASM from 'argon2-wasm-edge/wasm/blake2b.wasm?module';

setWASMModules({ argon2WASM, blake2bWASM });

export const config = { runtime: 'edge' };

export default async function handler(req: Request) {
  const password = new URL(req.url).searchParams.get('password');
  if (!password) throw new Error('Missing password, try /api/hash-password?password=verysecure');
  const salt = new Uint8Array(16);
  crypto.getRandomValues(salt);
  const hashedPassword = await argon2id({ ...params, password, salt });
  return new Response(`${password} => ${hashedPassword}`);
}

Usage on Cloudflare Workers

worker.ts

import params from './hasingParams';

import { argon2id, setWASMModules } from 'argon2-wasm-edge';
// @ts-expect-error
import argon2WASM from 'argon2-wasm-edge/wasm/argon2.wasm'; // <-- imports of wasm modules works differently in CF
// @ts-expect-error
import blake2bWASM from 'argon2-wasm-edge/wasm/blake2b.wasm';

setWASMModules({ argon2WASM, blake2bWASM });

async function fetch(req: Request) {
  const password = new URL(req.url).searchParams.get('password');
  if (!password) throw new Error('Missing password, try /api/hash-password?password=verysecure');
  const salt = new Uint8Array(16);
  crypto.getRandomValues(salt);
  const hashedPassword = await argon2id({ ...params, password, salt });
  return new Response(`${password} => ${hashedPassword}`);
}

export default { fetch }
1.0.23

30 days ago

1.0.22

1 month ago

1.0.21

1 month ago

1.0.20

1 month ago

1.0.19

1 month ago

1.0.18

1 month ago

1.0.17

1 month ago

1.0.16

1 month ago

1.0.14

1 month ago

1.0.12

1 month ago

1.0.11

1 month ago

1.0.10

1 month ago

1.0.9

1 month ago

1.0.8

1 month ago

1.0.7

1 month ago

1.0.6

1 month ago

1.0.5

1 month ago

1.0.4

1 month ago

1.0.3

1 month ago

1.0.2

1 month ago

1.0.1

1 month ago

1.0.0

1 month ago