BLAKE2 Hash Functions (JavaScript)

1.2.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates both synchronous (hex string and Uint8Array) and streaming BLAKE2b/BLAKE2s hashing with optional keys, using an ESM import pattern.

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(''));

view raw JSON →