JSPM

  • Created
  • Published
  • Downloads 468805
  • Score
    100M100P100Q177573F
  • License MIT

Fast hash functions compiled to WebAssembly

Package Exports

  • hash-wasm

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hash-wasm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

hash-wasm

Hash-WASM is a fast, portable hash function library. It's using WebAssembly to calculate the hash faster than other JavaScript-based implementations.

Features

  • Supported hash functions: MD4, MD5, CRC32 (more to come)
  • A lot faster than JS implementations (see benchmarks)
  • Supports all modern browsers and Node.js
  • Optimized for large files
  • Supports chunked input streams
  • WASM is bundled as base64 strings, so it can be easily deployed
  • Supports tree shaking (it only bundles the hash algorithms you need)
  • Includes TypeScript type definitions
  • Easy to use Promise-based async API

Install

npm i hash-wasm

Examples

Basic usage

import { md5 } from 'hash-wasm';

async function run(str) {
  console.log('MD5: ', await md5(str));
}

run();

Chunked input

import { createCRC32 } from 'hash-wasm';

async function run(str) {
  const crc32 = await createCRC32();
  crc32.init();
  crc32.update(new Uint8Array([0, 1, 2, 3]));
  crc32.update(new Uint32Array([4920, 8124]));
  crc32.update('abcd');
  const hash = crc32.digest();
  console.log('CRC32: ', hash);
}

run();

Browser support

Chrome Safari Firefox Edge IE
57+ 11+ 53+ 16+ Not supported

Benchmark

Benchmarks can be started with the npm run benchmark command.

MD5 ops/s throughput
node.js crypto module 213 852 MB/s
hash-wasm 137 548 MB/s
node-forge (npm library) 20 80 MB/s
md5 (npm library) 2 8 MB/s

MD4 ops/s throughput
node.js crypto module 351 1404 MB/s
hash-wasm 247 988 MB/s
js-md4 (npm library) 94 376 MB/s

CRC32 ops/s throughput
hash-wasm 471 1884 MB/s
crc (npm library) 131 524 MB/s

API

// simple usage
import { md4, md5, crc32 } from 'hash-wasm';

md4(data: string | typedArray | Buffer): Promise<string> // returns hash in hex format
md5(data: string | typedArray | Buffer): Promise<string> // returns hash in hex format
crc32(data: string | typedArray | Buffer): Promise<string> // returns hash in hex format

// usage with chunked data
import { createMD4, createMD5, createCRC32 } from 'hash-wasm';
createMD4(): Promise<IHasher>
createMD5(): Promise<IHasher>
createCRC32(): Promise<IHasher>

interface IHasher {
  init: () => void;
  update: (data: string | ArrayBuffer | Uint8Array | Uint16Array | Uint32Array | Buffer) => void;
  digest: () => string; // returns hash in hex format
}