@bitgo/blake2b v3.2.4
@bitgo/blake2b
Blake2b (64-bit version) in pure JavaScript
This module is based on @dcposch implementation of BLAKE2b, with some changes:
- This module requires you to pass in a
outbuffer, saving an allocation - This module allows you to set the
saltandpersonalparameters - This module exports constants for the parameters in libsodium style
- Uses a WASM version (where it is supported) for massive performance boosts
All credit goes to @dcposch for doing the hard work of porting the implementation from C to JavaScript.
Usage
var blake2b = require('@bitgo/blake2b');
var output = new Uint8Array(64);
var input = Buffer.from('hello world');
console.log('hash:', blake2b(output.length).update(input).digest('hex'));API
var hash = blake2b(outLength, [key], [salt], [personal], [noAssert = false])
Create a new hash instance, optionally with key, salt and
personal. Bypass input assertions by setting noAssert to true.
All parameters must be Uint8Array, Buffer or another object with a compatible
API. All parameters must also fulfill the following constraints, or an
AssertionError will be thrown (unless noAssert = true):
outLengthmust within the byte ranges defined by the constants below.keyis optional, but must within the byte ranges defined by the constants below, if given. This value must be kept secret, and can be used to create prefix-MACs.saltis optional, but must be exactlySALTBYTES, if given. You can use this parameter as a kind of per user id, or local versioning scheme. This value is not required to be secret.personalis optional, but must be exactlyPERSONALBYTES, if given. You can use this parameter as a kind of app id, or global versioning scheme. This value is not required to be secret.
var hash = hash.update(input)
Update the hash with new input. Calling this method after .digest will throw
an error.
var out = hash.digest(out)
Finalise the the hash and write the digest to out. out must be exactly equal
to outLength given in the blake2b method.
Optionally you can pass hex to get the hash as a hex string or no arguments
to have the hash return a new Uint8Array with the hash.
Constants
blake2b.BYTES_MINMinimum length ofoutblake2b.BYTES_MAXMaximum length ofoutblake2b.BYTESRecommended default length ofoutblake2b.KEYBYTES_MINMinimum length ofkeyblake2b.KEYBYTES_MAXMaximum length ofkeyblake2b.KEYBYTESRecommended default length ofkeyblake2b.SALTBYTESRequired length ofsaltblake2b.PERSONALBYTESRequired length ofpersonal
Install
npm install @bitgo/blake2bTest vectors
This repository includes test vectors with
{outlen, out, input, key, salt, personal} objects for testing conformance
against the spec and other implementations:
- Lines 2 - 257 are tests for hashing with no key, taken from BLAKE2 test vectors
- Lines 258 - 513 are tests for hashing with keys, taken from BLAKE2 test vectors
- Lines 514- 577 are tests for hashing with key, salt and personalisation, derived from the libsodium tests
License
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago