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

argon2id

v1.0.1

Published

Argon2id implementation in pure Javascript

Downloads

310

Readme

Argon2id

Fast, lightweight Argon2id implementation for both browser and Node:

  • optimized for bundle size (< 7KB minified and gizipped, with wasm inlined as base64)
  • SIMD support, with automatic fallback to non-SIMD binary if not supported (e.g. in Safari)
  • performance is comparable to or better than argon2-browser.

We initially tried implementing a solution in pure JS (no Wasm) but the running time was unacceptable. We resorted to implement part of the module in Wasm, to take advantage of 64-bit multiplications and SIMD instructions. The Wasm binary remains small thanks to the fact that the memory is fully managed by the JS side, hence no memory management function gets included in the Wasm binary.

Install

Install from npm (compiled wasm files included):

npm i argon2id

Usage

With bundlers like Rollup (through plugin-wasm) or Webpack (through wasm-loader), that automatically translate import statements like import wasmModule from '*.wasm' to a loader of type wasmModule: (instanceOptions) => WebAssembly.WebAssemblyInstantiatedSource (either sync or async), you can simply use the default export like so:

import loadArgon2idWasm from 'argon2id';

const argon2id = await loadArgon2idWasm();
const hash = argon2id({
  password: new Uint8Array(...),
  salt: crypto.getRandomValues(new Uint8Array(32)),
  parallelism: 4,
  passes: 3,
  memorySize: 2**16
});

Refer to the Argon2 RFC for details about how to pick the parameters.

Note about memory usage: every call to loadArgon2idWasm will instantiate and run a separate Wasm instance, with separate memory. The used Wasm memory is cleared after each call to argon2id, but it isn't deallocated (this is due to Wasm limitations). Re-loading the Wasm module is thus recommended in order to free the memory if multiple argon2id hashes are computed and some of them require considerably more memory than the rest.

Custom Wasm loaders

The library does not require a particular toolchain. If the aforementioned bundlers are not an option, you can manually take care of setting up the Wasm modules.

For instance, in Node, the library can be used without bundlers. You will need to pass two functions that instantiate the Wasm modules to setupWasm (first function is expected to take care of the SIMD binary, second one the non-SIMD one):

import fs from 'fs';
import setupWasm from 'argon2id/lib/setup.js';

// point to compiled binaries
const SIMD_FILENAME = 'argon2id/dist/simd.wasm';
const NON_SIMD_FILENAME = 'argon2id/dist/no-simd.wasm';

const argon2id = await setupWasm(
  (importObject) => WebAssembly.instantiate(fs.readFileSync(SIMD_FILENAME), importObject),
  (importObject) => WebAssembly.instantiate(fs.readFileSync(NON_SIMD_FILENAME), importObject),
);

Using the same principle, for browsers you can use bundlers with simple base-64 file loaders.

Compiling

The npm package already includes the compiled binaries. If you fork the repo, you'll have to manually compile wasm (Docker required):

npm run build

The resulting binaries will be under dist/. If you do not want to use docker, you can look into installing emscripten; you'll find the compilation commands to use in build_wasm.sh.