Git Product home page Git Product logo

argon2-wasi's Introduction

argon2-wasi

This wraps the Rust crate argon2 in a very simple WASI (or commandline) compatible program.

Compatible with workerd / CloudFlare Workers' WASI support. You need to run the worker in unbounded mode for it to not time out.

bcrypt-wasi provides an identical API for bcrypt.

Usage

Simple example:

import { WASI } from "@cloudflare/workers-wasi";
// @ts-ignore TS2307: Cannot find module './argon2-wasi.wasm'
import argon2 from "./argon2-wasi.wasm";

export async function invoke(args: string[]) {
  const stdout = new TransformStream();
  const stderr = new TransformStream();
  const wasi = new WASI({
    args: ["argon2-wasi.wasm", ...args],
    stdout: stdout.writable,
    stderr: stderr.writable,
  });
  const instance = new WebAssembly.Instance(argon2, {
    wasi_snapshot_preview1: wasi.wasiImport,
  });
  const promise = wasi.start(instance);
  const errors = stderr.readable.getReader().read();
  const ret = stdout.readable.getReader().read();
  const [errorsStream, resultStream, _] = await Promise.all([errors, ret, promise]);
  const errorsValue = new TextDecoder().decode(errorsStream.value);
  if (errorsValue) {
    throw new Error(errorsValue);
  }
  const retValue = new TextDecoder().decode(resultStream.value);
  return retValue.trim();
}

export async function argon2Hash(password: string): Promise<string> {
  return await invoke(["hash", password]);
}

export async function argon2Verify(password: string, hash: string): Promise<boolean> {
  return (await invoke(["verify", password, hash])) === "true";
}

Then just use await argon2Hash('somepwd'); or await argon2Verify('somepwd', '$argon2id$v..')

License

Same license as for the argon2 crate; licensed under either of:

at your option.

argon2-wasi's People

Contributors

miunau avatar rmarscher avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.