You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why BLAKE2 for hashing? "BLAKE2 is a cryptographic hash function faster than MD5, SHA-1, SHA-2, and SHA-3, yet is at least as secure as the latest standard SHA-3. BLAKE2 has been adopted by many projects due to its high speed, security, and simplicity." https://blake2.net/
node-blake2 provides a stream-compatible
blake2b, blake2bp, blake2s, and blake2sp Hash and KeyedHash for Node.js.
node-blake2 has been tested to work with the following compilers and platforms:
varblake2=require('blake2');varh=blake2.createKeyedHash('blake2b',Buffer.from('key - up to 64 bytes for blake2b, 32 for blake2s'));h.update(Buffer.from("test"));console.log(h.digest("hex"));
blake2.createKeyedHash takes a key argument like
crypto.createHmac.
Although it is not an HMAC, a keyed hash serves the same purpose.
Important notes
blake2.create{Hash,KeyedHash} support algorithms blake2b, blake2bp, blake2s, and blake2sp.
Data passed to .update on blake2.{Hash,KeyedHash} must be a Buffer.
Keys passed to blake2.createKeyedHash(algo, key) must be a Buffer.
Just as with crypto.Hash, .digest() can only be called once.
BLAKE2 can generate digests between 1-64 bytes for BLAKE2b and 1-32 bytes for
BLAKE2s. Pass digestLength as an option to use a digest shorter than the
default (maximum length):
varblake2=require('blake2');varh=blake2.createHash('blake2b',{digestLength: 16});h.update(Buffer.from("test"));h.digest();// Returns a Buffer with 16 bytes
or with a key:
varblake2=require('blake2');varh=blake2.createKeyedHash('blake2b',Buffer.from('my key'),{digestLength: 16});h.update(Buffer.from("test"));h.digest();// Returns a Buffer with 16 bytes
Note that BLAKE2 will generate completely different digests for shorter digest
lengths; they are not simply a slice of the default digest.
Copying a hash object
You can call .copy() on a Hash or KeyedHash, which will return a new object with all of the internal BLAKE2 state copied from the source object.
varblake2=require('blake2');varh=blake2.createHash('blake2b');h.update(Buffer.from("test"));// Call .copy() before .digest(), because .digest() finalizes internal statevarj=h.copy();// h is unaffected by updates to jj.update(Buffer.from("more"));console.log(h.digest());console.log(j.digest());
Known issues
On Windows, node-blake2 requires enabling AVX instructions as a workaround for the way the upstream build preprocessor detects support for SSE2.
About
All four BLAKE2 variants (blake2b, blake2bp, blake2s, blake2sp) with stream support for Node.js