npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

argon2-wasm-edge

v1.0.23

Published

Lightning fast Argon2 implementation runnable on Vercel Edge and CF Workers

Downloads

204

Readme

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 }