blakets
BlakeTS is a pure TypeScript implementation of the BLAKE2b and BLAKE2s hash functions.
Original JavaScript-only implementation by dcposch
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 blakets
var blake = require('blakets');
console.log(blake.blake2bHex('abc'));
console.log(blake.blake2sHex('abc'));
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.
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
var OUTPUT_LENGTH = 64
var context = blake2bInit(OUTPUT_LENGTH, KEY)
...
blake2bUpdate(context, bytes)
...
var hash = blake2bFinal(context)
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
Licensed under the GNU Lesser General Public License. Ported from the reference C implementation in RFC 7693.