• Stars
    star
    178
  • Rank 214,989 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Pure Javascript implementation of the BLAKE2b and BLAKE2s hash functions

blakejs

travis ci npm version

blakejs is a pure Javascript implementation of the BLAKE2b and BLAKE2s hash functions.

blake1


RFC 7693: The BLAKE Cryptographic Hash and MAC

BLAKE is the default family of hash functions in the venerable NaCl crypto library. Like SHA2 and SHA3 but unlike MD5 and SHA1, BLAKE offers solid security. With an optimized assembly implementation, BLAKE can be faster than all of those other hash functions.

Of course, this implementation is in Javascript, so it won't be winning any speed records. More under Performance below. It's short and sweet, less than 500 LOC.

As far as I know, this package is the easiest way to compute Blake2 in the browser.

Other options to consider:

  • @nazar-pc has WebAssembly implementation for higher performance where supported: blake2.wasm
  • @emilbayes has a Blake2b-only implementation with salt support; WASM with automatic JS fallback: blake2b
  • On node, you probably want the native wrapper node-blake2

Quick Start

$ npm install --save blakejs
var blake = require('blakejs')
console.log(blake.blake2bHex('abc'))
// prints ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923
console.log(blake.blake2sHex('abc'))
// prints 508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982

API

1. Use blake2b to compute a BLAKE2b hash

Pass it a string, Buffer, or Uint8Array containing bytes to hash, and it will return a Uint8Array containing the hash.

// Computes the BLAKE2B hash of a string or byte array, and returns a Uint8Array
//
// Returns a n-byte Uint8Array
//
// Parameters:
// - input - the input bytes, as a string, Buffer, or Uint8Array
//           Strings are converted to UTF8 bytes
// - key - optional key Uint8Array, up to 64 bytes
// - outlen - optional output length in bytes, default 64
function blake2b(input, key, outlen) {
    [...]
}

For convenience, blake2bHex takes the same arguments and works the same way, but returns a hex string.

2. Use blake2b[Init,Update,Final] to compute a streaming hash

var KEY = null // optional key
var OUTPUT_LENGTH = 64 // bytes
var context = blake2bInit(OUTPUT_LENGTH, KEY)
...
// each time you get a byte array from the stream:
blake2bUpdate(context, bytes)
...
// finally, once the stream has been exhausted
var hash = blake2bFinal(context)
// returns a 64-byte hash, as a Uint8Array

3. All blake2b* functions have blake2s* equivalents

BLAKE2b: blake2b, blake2bHex, blake2bInit, blake2bUpdate, and blake2bFinal

BLAKE2s: blake2s, blake2sHex, blake2sInit, blake2sUpdate, and blake2sFinal

The inputs are identical except that maximum key size and maximum output size are 32 bytes instead of 64.

Limitations

  • Can only handle up to 2**53 bytes of input

    If your webapp is hashing more than 8 petabytes, you may have other problems :)

Testing

Performance

BLAKE2b: 15.2 MB / second on a 2.2GHz i7-4770HQ
BLAKE2s: 20.4 MB / second

¯\_(ツ)_/¯

If you're using BLAKE2b in server side node.js code, you probably want the native wrapper which should be able to do several hundred MB / second on the same processor.

If you're using BLAKE2b in a web app, 15 MB/sec might be fine.

Javascript doesn't have 64-bit integers, and BLAKE2b is a 64-bit integer algorithm. Writing it withUint32Array is not that fast. BLAKE2s is a 32-bit algorithm, so it's a bit faster.

If we want better machine code at the expense of gross-looking Javascript, we could use asm.js

License

Creative Commons CC0. Ported from the reference C implementation in RFC 7693.

More Repositories

1

scramble

Secure email for everyone
JavaScript
226
star
2

btcmirror

Bitcoin Mirror is a bitcoin light client that runs on ethereum.
Solidity
63
star
3

webtorrent-remote

Run WebTorrent in one process, control it from another process or even another machine
JavaScript
39
star
4

silverportal

Trustless BTC-ETH exchange.
TypeScript
28
star
5

datpedia

dat wikipedia
JavaScript
25
star
6

sidekick

Convert anything to anything using GPT3
TypeScript
17
star
7

yimby

yes in my back yard
JavaScript
12
star
8

eth-pcd

TypeScript
10
star
9

webtorrent-electron

WebTorrent Electron Process
JavaScript
8
star
10

hello-foundry

Foundry hello world
4
star
11

snk

a snake game in ~100 lines of go
Go
4
star
12

scramble-mail-repo

Scramble.io module for locally storing and indexing email. Uses sqlite3.
JavaScript
4
star
13

bootloaderjs

Bootloader.js loads your static resources securely. It checks for cryptographic signatures on everything it loads. It caches itself permanently, so that an adversary serve existing clients a bad version of bootloader.js
4
star
14

zucast

A zero-knowledge forum, private to Zuzalu
TypeScript
3
star
15

simple_spheres

Tiny raytracer written to compare programming languages
C
3
star
16

ld37

ludum dare 37
JavaScript
3
star
17

hello-react

TypeScript
2
star
18

sumrz

Summarize a CSV file. Get quick stats about each column.
Go
2
star
19

canshark

C#
1
star
20

hello-eth

TypeScript
1
star
21

chess

JavaScript
1
star
22

scramble-ui

Scramble UI components. Made with React, packaged with Browserify.
JavaScript
1
star
23

react-hello-world

hack lodge react workshop
JavaScript
1
star
24

dash

TypeScript
1
star
25

hello-p2p

JavaScript
1
star
26

react-tsx-hello-world

JavaScript
1
star
27

p9

planet 9
JavaScript
1
star
28

escrow

An escrow contract to settle a ridiculous bet, written almost entirely by GPT4.
Solidity
1
star
29

d3school

Learn D3.js
JavaScript
1
star
30

github-deploy

Deploy or test automatically whenever you push to Github
JavaScript
1
star
31

ethmonitor

Monitor your Ethereum validators
TypeScript
1
star
32

art

Plotter art
Python
1
star
33

ethexp

ethereum explorer
TypeScript
1
star
34

astro

CS448B Assignment 3
JavaScript
1
star
35

unsuckifier

JavaScript
1
star
36

peerio-client

Messages and files, simple and secure.
JavaScript
1
star