BLAKE2 Hash Functions (JavaScript)
blakejs provides a pure JavaScript implementation of the BLAKE2b and BLAKE2s cryptographic hash functions. It is currently at version 1.2.1 and appears to be in an active maintenance state, receiving updates as needed for bug fixes or minor enhancements rather than following a strict time-based release cadence. Its primary differentiator is its full compatibility with browser environments without requiring WebAssembly or native Node.js modules, making it an easy choice for client-side hashing. While it offers solid cryptographic security similar to SHA2 and SHA3, its pure JavaScript nature means it sacrifices performance compared to native or WASM alternatives, especially in server-side (Node.js) contexts. It's designed to be compact, with less than 500 lines of code, and easy to integrate, particularly for browser-based applications where native wrappers are not an option.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` in an ECMAScript Module (ESM) context (e.g., in a modern Node.js project with `"type": "module"` in package.json or a browser environment).fixUse `import * as blake from 'blakejs';` for ESM environments, or ensure your file is treated as a CommonJS module. -
TypeError: blake.blake2bHex is not a function
cause Incorrectly importing `blakejs` or attempting to destructure named exports when the package primarily exports a single object.fixEnsure you are using `import * as blake from 'blakejs';` (ESM) or `const blake = require('blakejs');` (CJS), and then access functions via `blake.blake2bHex`. -
TypeError: Input must be a string, Buffer, or Uint8Array
cause The input provided to a hashing function (e.g., `blake2bHex`) is not a JavaScript string, Node.js Buffer, or a standard `Uint8Array`.fixConvert the input data to an accepted type before passing it to `blakejs` functions. For example, use `new TextEncoder().encode('your string')` to convert a string to a `Uint8Array`. -
Error: blake2b: output length too large
cause The specified `outlen` parameter for a BLAKE2b hash function exceeds its maximum allowed value of 64 bytes (or 32 bytes for BLAKE2s).fixAdjust the `outlen` parameter to be less than or equal to 64 bytes for BLAKE2b functions, or less than or equal to 32 bytes for BLAKE2s functions.
Warnings
- gotcha This pure JavaScript implementation is significantly slower than native or WebAssembly alternatives (e.g., node-blake2 or blake2.wasm). For performance-critical applications, especially on Node.js servers, consider those alternatives.
- gotcha Pure JavaScript cryptographic implementations, while generally secure algorithmically, can be more susceptible to timing side-channel attacks compared to native implementations. Avoid using `blakejs` for hashing highly sensitive keys in environments where an attacker can precisely measure execution time.
- gotcha The library has an internal limitation of handling input up to 2^53 bytes (approximately 8 petabytes). While extremely large, applications processing truly massive datasets should be aware of this theoretical limit.
- gotcha When using BLAKE2b, the maximum output length and key length is 64 bytes. For BLAKE2s, it's 32 bytes. Exceeding these limits will result in an error.
Install
-
npm install blakejs -
yarn add blakejs -
pnpm add blakejs
Imports
- blake
import blake from 'blakejs';
import * as blake from 'blakejs';
- blake
const blake = require('blakejs'); - blake2bHex
import { blake2bHex } from 'blakejs';blake.blake2bHex(data);
Quickstart
import * as blake from 'blakejs';
const inputString = 'hello blakejs world';
const inputBytes = new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]); // 'abcde'
const key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); // Optional key for MAC
// Compute BLAKE2b hash of a string, returning a hex string
const blake2bHexString = blake.blake2bHex(inputString, null, 64);
console.log('BLAKE2b (string) hex:', blake2bHexString);
// Compute BLAKE2s hash of a Uint8Array, returning a Uint8Array
const blake2sByteArray = blake.blake2s(inputBytes, key, 32);
console.log('BLAKE2s (bytes) array:', blake2sByteArray);
// Streaming BLAKE2b hash example
const OUTPUT_LENGTH = 64;
const streamingKey = null; // Optional key
const context = blake.blake2bInit(OUTPUT_LENGTH, streamingKey);
blake.blake2bUpdate(context, new TextEncoder().encode('first part '));
blake.blake2bUpdate(context, new TextEncoder().encode('second part of data'));
const finalHash = blake.blake2bFinal(context);
console.log('Streaming BLAKE2b hex:', Array.from(finalHash).map(b => b.toString(16).padStart(2, '0')).join(''));