{"id":12715,"library":"blakejs","title":"BLAKE2 Hash Functions (JavaScript)","description":"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.","status":"active","version":"1.2.1","language":"javascript","source_language":"en","source_url":"https://github.com/dcposch/blakejs","tags":["javascript","typescript"],"install":[{"cmd":"npm install blakejs","lang":"bash","label":"npm"},{"cmd":"yarn add blakejs","lang":"bash","label":"yarn"},{"cmd":"pnpm add blakejs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For ESM, use a namespace import (`* as blake`) to reliably get an object containing all functions, mirroring how it's used in CommonJS. A direct default import (`import blake`) might work in some transpilation setups but can be less robust for CJS interop.","wrong":"import blake from 'blakejs';","symbol":"blake","correct":"import * as blake from 'blakejs';"},{"note":"Standard CommonJS import for Node.js and older browser environments. The 'blake' constant will be an object containing all hashing functions (e.g., `blake.blake2bHex`).","symbol":"blake","correct":"const blake = require('blakejs');"},{"note":"Individual hashing functions like `blake2bHex` are properties of the imported 'blake' object, not direct named exports. They should be accessed via dot notation on the imported object.","wrong":"import { blake2bHex } from 'blakejs';","symbol":"blake2bHex","correct":"blake.blake2bHex(data);"}],"quickstart":{"code":"import * as blake from 'blakejs';\n\nconst inputString = 'hello blakejs world';\nconst inputBytes = new Uint8Array([0x61, 0x62, 0x63, 0x64, 0x65]); // 'abcde'\nconst key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]); // Optional key for MAC\n\n// Compute BLAKE2b hash of a string, returning a hex string\nconst blake2bHexString = blake.blake2bHex(inputString, null, 64);\nconsole.log('BLAKE2b (string) hex:', blake2bHexString);\n\n// Compute BLAKE2s hash of a Uint8Array, returning a Uint8Array\nconst blake2sByteArray = blake.blake2s(inputBytes, key, 32);\nconsole.log('BLAKE2s (bytes) array:', blake2sByteArray);\n\n// Streaming BLAKE2b hash example\nconst OUTPUT_LENGTH = 64;\nconst streamingKey = null; // Optional key\nconst context = blake.blake2bInit(OUTPUT_LENGTH, streamingKey);\nblake.blake2bUpdate(context, new TextEncoder().encode('first part '));\nblake.blake2bUpdate(context, new TextEncoder().encode('second part of data'));\nconst finalHash = blake.blake2bFinal(context);\nconsole.log('Streaming BLAKE2b hex:', Array.from(finalHash).map(b => b.toString(16).padStart(2, '0')).join(''));","lang":"typescript","description":"Demonstrates both synchronous (hex string and Uint8Array) and streaming BLAKE2b/BLAKE2s hashing with optional keys, using an ESM import pattern."},"warnings":[{"fix":"For Node.js, use `node-blake2`. For browsers with WASM support, consider `blake2.wasm` for better performance.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use hardware-accelerated or native cryptographic libraries for operations involving highly sensitive cryptographic keys in untrusted environments.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For inputs exceeding 2^53 bytes, an alternative hashing solution or custom streaming logic capable of handling such scale would be required, though this is an extreme edge case.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure `outlen` and `key` array lengths passed to `blake2b*` functions are <= 64 bytes, and for `blake2s*` functions, <= 32 bytes.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `import * as blake from 'blakejs';` for ESM environments, or ensure your file is treated as a CommonJS module.","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).","error":"ReferenceError: require is not defined"},{"fix":"Ensure you are using `import * as blake from 'blakejs';` (ESM) or `const blake = require('blakejs');` (CJS), and then access functions via `blake.blake2bHex`.","cause":"Incorrectly importing `blakejs` or attempting to destructure named exports when the package primarily exports a single object.","error":"TypeError: blake.blake2bHex is not a function"},{"fix":"Convert 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`.","cause":"The input provided to a hashing function (e.g., `blake2bHex`) is not a JavaScript string, Node.js Buffer, or a standard `Uint8Array`.","error":"TypeError: Input must be a string, Buffer, or Uint8Array"},{"fix":"Adjust 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.","cause":"The specified `outlen` parameter for a BLAKE2b hash function exceeds its maximum allowed value of 64 bytes (or 32 bytes for BLAKE2s).","error":"Error: blake2b: output length too large"}],"ecosystem":"npm"}