• Stars
    star
    154
  • Rank 242,095 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

BLAKE3 hashing for JavaScript: native Node bindings (where available) and WebAssembly

BLAKE3

BLAKE3 running in JavaScript (node.js and browsers) via native bindings, where available, or WebAssembly.

npm install blake3

Additionally, there's a flavor of the package which is identical except that it will not download native Node.js bindings and use only WebAssembly:

npm install blake3-wasm

Table of Contents

Quickstart

If you're on Node, import the module via

const blake3 = require('blake3');

blake3.hash('foo'); // => Buffer

If you're in the browser, import blake3/browser. This includes a WebAssembly binary, so you probably want to import it asynchronously, like so:

import('blake3/browser').then((blake3) => {
  blake3.hash('foo'); // => Uint8Array
});

The API is very similar in Node.js and browsers, but Node supports and returns Buffers and a wider range of input and output encoding.

More complete example:

const { hash, createHash } = require('blake3');

hash('some string'); // => hash a string to a uint8array

// Update incrementally (Node and Browsers):
const hash = createHash();
stream.on('data', (d) => hash.update(d));
stream.on('error', (err) => {
  // hashes use unmanaged memory in WebAssembly, always free them if you don't digest()!
  hash.dispose();
  throw err;
});
stream.on('end', () => finishedHash(hash.digest()));

// Or, in Node, it's also a transform stream:
createReadStream('file.txt')
  .pipe(createHash())
  .on('data', (hash) => console.log(hash.toString('hex')));

API

Node.js

The Node API can be imported via require('blake3').

hash(data: BinaryLike, options?: { length: number }): Buffer

Returns a hash for the given data. The data can be a string, buffer, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Buffer.

keyedHash(key: BinaryLike, data: BinaryLike, options?: { length: number }): Buffer

Returns keyed a hash for the given data. The key must be exactly 32 bytes. The data can be a string, buffer, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Buffer.

For more information, see the blake3 docs.

deriveKey(context: BinaryLike, material: BinaryLike, options?: { length: number }): Buffer

The key derivation function. The data can be a string, buffer, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Buffer.

For more information, see the blake3 docs.

Hasher

The hasher is a type that lets you incrementally build a hash. It's compatible with Node's crypto hash instance. For instance, it implements a transform stream, so you could do something like:

createReadStream('file.txt')
  .pipe(createHash())
  .on('data', (hash) => console.log(hash.toString('hex')));
createHash(): Hasher

Creates a new hasher instance using the standard hash function.

createKeyed(key: BinaryLike): Hasher

Creates a new hasher instance for a keyed hash. For more information, see the blake3 docs.

createDeriveKey(context: BinaryLike): Hasher

Creates a new hasher instance for the key derivation function. For more information, see the blake3 docs.

hasher.update(data: BinaryLike): this

Adds data to a hash. The data can be a string, buffer, typedarray, array buffer, or array.

hasher.digest(encoding?: string, options?: { length: number })): Buffer | string

Returns the hash of the data. If an encoding is given, a string will be returned. Otherwise, a Buffer is returned. Optionally, you can specify the requested byte length of the hash.

hasher.reader(): HashReader

Returns a HashReader for the current hash.

hasher.dispose()

This is a no-op for Node.js.

HashReader

The hash reader can be returned from hashing functions. Up to 264-1 bytes of data can be read from BLAKE3 hashes; this structure lets you read those. Note that, like hash, this is an object which needs to be manually disposed of.

reader.position: bigint

A property which gets or sets the position of the reader in the output stream. A RangeError is thrown if setting this to a value less than 0 or greater than 264-1. Note that this is a bigint, not a standard number.

reader.position += 32n; // advance the reader 32 bytes
reader.readInto(target: Uint8Array): number

Reads bytes into the target array, filling it up and advancing the reader's position. It returns the number of bytes written, which may be less then the size of the target buffer if position 264-1 is reached.

reader.read(bytes: number): Buffer

Reads and returns the given number of bytes from the reader, and advances the position. A RangeError is thrown if reading this data puts the reader past 264-1 bytes.

reader.view(target: Buffer): Readonly<Uint8Array>

Returns a view of the given number of bytes from the reader. The view can be used synchronously, but must not be reused later. This is more efficient when using the webassembly version of the module.

Fewer bytes may be returned than requested, if the number is large (>1MB).

reader[Symbol.iterator]

The reader is an Iterable of Readonly<Uint8Array>s. Like the view method, the iterated arrays will be reused internally on the next iteration, so if you need data, you should copy it out of the iterated array.

Browser

The browser API can be imported via import('blake3/browser'), which works well with Webpack.

Note that you must call the load() method before using any function in the module.

import * as blake3 from 'blake3/browser-async';

blake3.load().then(() => {
  console.log(blake3.hash('hello world'));
});

hash(data: BinaryLike, options?: { length: number }): Hash

Returns a hash for the given data. The data can be a string, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Hash instance.

keyedHash(key: BinaryLike, data: BinaryLike, options?: { length: number }): Hash

Returns keyed a hash for the given data. The key must be exactly 32 bytes. The data can be a string, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Hash instance.

For more information, see the blake3 docs.

deriveKey(context: BinaryLike, material: BinaryLike, options?: { length: number }): Hash

The key derivation function. The data can be a string, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Hash instance.

For more information, see the blake3 docs.

Hash

A Hash is the type returned from hash functions and the hasher in the browser. It's a Uint8Array with a few additional helper methods.

hash.equals(other: Uint8Array)

Returns whether this hash equals the other hash, via a constant-time equality check.

hash.toString(encoding: 'hex' | 'base64' | 'utf8'): string

Hasher

The hasher is a type that lets you incrementally build a hash. For instance, you can hash a fetched page like:

const res = await fetch('https://example.com');
const body = await res.body;

const hasher = blake3.createHash();
const reader = body.getReader();

while (true) {
  const { done, value } = await reader.read();
  if (done) {
    break;
  }

  hasher.update(value);
}

console.log('Hash of', res.url, 'is', hasher.digest('hex'));

Converts the hash to a string with the given encoding.

createHash(): Hasher

Creates a new hasher instance using the standard hash function.

createKeyed(key: BinaryLike): Hasher

Creates a new hasher instance for a keyed hash. For more information, see the blake3 docs.

createDeriveKey(context: BinaryLike): Hasher

Creates a new hasher instance for the key derivation function. For more information, see the blake3 docs.

hasher.update(data: BinaryLike): this

Adds data to a hash. The data can be a string, buffer, typedarray, array buffer, or array. This will throw if called after digest() or dispose().

hasher.digest(encoding?: 'hex' | 'base64' | 'utf8', options?: { length: number })): Hash | string

Returns the hash of the data. If an encoding is given, a string will be returned. Otherwise, a Hash is returned. Optionally, you can specify the requested byte length of the hash.

hasher.reader(): HashReader

Returns a HashReader for the current hash.

hasher.dispose()

Disposes of webassembly-allocated resources. Resources are free automatically via a FinalizationRegistry for hashers, but you may call this manually if you run into resource-constraint issues.

HashReader

The hash reader can be returned from hashing functions. Up to 264-1 bytes of data can be read from BLAKE3 hashes; this structure lets you read those. Note that, like hash, this is an object which needs to be manually disposed of.

reader.position: bigint

A property which gets or sets the position of the reader in the output stream. A RangeError is thrown if setting this to a value less than 0 or greater than 264-1. Note that this is a bigint, not a standard number.

reader.position += 32n; // advance the reader 32 bytes
reader.readInto(target: Uint8Array): number

Reads bytes into the target array, filling it up and advancing the reader's position. It returns the number of bytes written, which may be less then the size of the target buffer if position 264-1 is reached.

reader.read(bytes: number): Hash

Reads and returns the given number of bytes from the reader, and advances the position. A RangeError is thrown if reading this data puts the reader past 264-1 bytes.

reader.view(target: Buffer): Readonly<Uint8Array>

Returns a view of the given number of bytes from the reader. The view can be used synchronously, but must not be reused later. This is more efficient when using the webassembly version of the module.

Fewer bytes may be returned than requested, if the number is large (>1MB).

reader[Symbol.iterator]

The reader is an Iterable of Readonly<Uint8Array>s. Like the view method, the iterated arrays will be reused internally on the next iteration, so if you need data, you should copy it out of the iterated array.

Speed

Native Node.js bindings are a work in progress.

You can run benchmarks by installing npm install -g @c4312/matcha, then running matcha benchmark.js. These are the results running on Node 12 on my MacBook. Blake3 is significantly faster than Node's built-in hashing.

    276,000 ops/sec > 64B#md5 (4,240x)
    263,000 ops/sec > 64B#sha1 (4,040x)
    271,000 ops/sec > 64B#sha256 (4,160x)
  1,040,000 ops/sec > 64B#blake3 wasm (15,900x)
    625,000 ops/sec > 64B#blake3 native (9,590x)

      9,900 ops/sec > 64KB#md5 (152x)
     13,900 ops/sec > 64KB#sha1 (214x)
      6,470 ops/sec > 64KB#sha256 (99.2x)
      6,410 ops/sec > 64KB#blake3 wasm (98.4x)
     48,900 ops/sec > 64KB#blake3 native (750x)

        106 ops/sec > 6MB#md5 (1.63x)
        150 ops/sec > 6MB#sha1 (2.3x)
       69.2 ops/sec > 6MB#sha256 (1.06x)
       65.2 ops/sec > 6MB#blake3 wasm (1x)
        502 ops/sec > 6MB#blake3 native (7.7x)

Other (JS) Implementations

Contributing

This build is a little esoteric due to the mixing of languages. We use a Makefile to coodinate things.

To get set up, you'll want to open the repository in VS Code. Make sure you have Remote Containers installed, and then accept the "Reopen in Container" prompt when opening the folder. This will get the environment set up with everything you need. Then, run make prepare to install local dependencies.

Finally, make will create a build for you; you can run make MODE=release for a production release, and certainly should if you want to benchmark it.

  • Rust code is compiled from src/lib.rs to pkg/browser and pkg/node
  • TypeScript code is compiled from ts/*.ts into dist

Publishing

In case I get hit by a bus or get other contributors, these are the steps for publishing:

  1. Get all your code ready to go in master, pushed up to Github.
  2. Run make prepare-binaries. This will update the branch generate-binary, which kicks off a build via Github actions to create .node binaries for every relevant Node.js version.
  3. When the build completes, it'll generate a zip file of artifacts. Download those.
  4. Back on master, run npm version <type> to update the version in git. git push --tags.
  5. On Github, upload the contents of the artifacts folder to the release for the newly tagged version.
  6. Run npm publish.

More Repositories

1

cockatiel

๐Ÿฆ A resilience and transient-fault-handling library that allows developers to express policies such as Backoff, Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback. Inspired by .NET Polly.
TypeScript
1,355
star
2

js-fuzz

An AFL-inspired genetic fuzz tester for JavaScript
TypeScript
127
star
3

validity

Golang package for beautiful data validation
Go
39
star
4

sio

Socket.io CLI client and debugging tool.
JavaScript
26
star
5

matcha

A caffeine driven, simple command line for benchmarking
TypeScript
21
star
6

laravel-dbsim

Query builder sim for L4
JavaScript
20
star
7

multicraft

Revised UI for the next Multicraft panel
PHP
14
star
8

codesong

Fork of Dayle Rees's Earthsong theme with improvements for VS Code
13
star
9

msft-rewards-modeller

Modeller tool for Microsoft employees to figure out their compension
Jupyter Notebook
11
star
10

nodejs-testing

VS Code integration for node:test native tests
TypeScript
8
star
11

vscode-css-theme-completions

TypeScript
7
star
12

kap-azure

Azure Storage plugin for Kap
TypeScript
7
star
13

social-colors

LESS file with social media colors. Tired of looking them up. Every. Single. Time.
CSS
7
star
14

esbuild-problem-matchers

Problem matchers for esbuild tasks in VS Code
5
star
15

bump-module

JavaScript
4
star
16

vsix-viewer

TypeScript
4
star
17

vscode-webview-tools

Miscellaneous tools for building things in VS Code webviews
TypeScript
4
star
18

chromehash

A Rust/WebAssembly implementation of the Chrome content hashing algorithm
JavaScript
3
star
19

hosting-sim-2014

IT'S SO INTENSE BRAH
Ruby
3
star
20

-sv

๐Ÿ‘ separated values
JavaScript
3
star
21

cosmonaut

Azure Cosmos DB ODM
TypeScript
3
star
22

gulp-breakout

Better breakout build environment for web development
JavaScript
3
star
23

peet-io

My new personal site, now with 100% more blog!
TypeScript
2
star
24

solarized-slack

CSS for making the Slack interface solarized!
CSS
2
star
25

recipes

Food I make
Go
2
star
26

bin-vcs

Experimental + WIP: version control for binary files with a git-like CLI
Go
2
star
27

protobill

PHP
2
star
28

ketama

Node.js hash ring implementation using libketama-like hashing
TypeScript
2
star
29

mchostexposed

CSS
2
star
30

fsu-entrance

Entrance for FSU
Java
2
star
31

grunt-breakout

Breakout build environment for web development
JavaScript
2
star
32

peet.io

Simple site for peet.io
JavaScript
2
star
33

typed-vscode-contributions

[WIP] Strongly-typed runtime and generators for VS Code extensions
TypeScript
1
star
34

dfa

Simple renderer for deterministic finite automatons
Go
1
star
35

CSC148-assignment-1

Python
1
star
36

centroid-api

API Wrapper for the CentroidMedia API
JavaScript
1
star
37

WHMCS-Dwolla-Gateway

Gateway for WHMCS into Dwolla.
PHP
1
star
38

kiln-monitor

A nice UI for monitoring Bartell kilns
C#
1
star
39

sixgood

Image sharing system for s.ix.gd
Python
1
star
40

rpt

Helper to measure how often things happen.
Go
1
star
41

backer

GPL Node.js-powered backup daemon.
CoffeeScript
1
star
42

test-controller-migration-example

An example of a migration from the Test Explorer UI to native VS Code testing
TypeScript
1
star
43

faster-import-cost

Fast import code display for Javascript code
TypeScript
1
star
44

HubSub

Java
1
star
45

csc209

C
1
star
46

mopidy-azure

Azure storage extension for Mopidy
Python
1
star
47

OpenMCD

Open source, drop-in replacement for the Multicraft daemon.
Java
1
star