Fast BLAKE3 cryptographic hash function with TypeScript support
npm install fast-blake3High-performance BLAKE3 cryptographic hash implementation for JavaScript/TypeScript
- Basic hashing
- Streaming/incremental hashing
- Keyed hashing (MAC)
- Key derivation (KDF)
- ESM and CommonJS support
- Full TypeScript types
``bash`
npm install fast-blake3
`javascript
import { hash, createHasher, keyedHash, deriveKey } from 'fast-blake3';
// Basic hash
const digest = hash('hello world'); // Uint8Array(32)
const longHash = hash('hello world', 64); // Uint8Array(64)
// Streaming hash
const hasher = createHasher();
hasher.update('hello').update(' world');
const result = hasher.finalize();
// Keyed hash (MAC)
const key = new Uint8Array(32).fill(0x42);
const mac = keyedHash(key, 'message');
// Key derivation (KDF)
const derivedKey = deriveKey('myapp context', 'password');
`
Computes the BLAKE3 hash of the provided input.
Parameters:
| Name | Type | Description |
|------|------|-------------|
| data | Uint8Array \| string | Input data to hash |outputLength
| | number | Output length in bytes (default: 32) |
Returns: Uint8Array
`javascript`
hash('hello'); // 32-byte hash (default)
hash('hello', 64); // 64-byte hash
hash(new Uint8Array([1,2,3])); // Binary input
Creates a streaming hasher for incremental hashing.
Parameters:
| Name | Type | Description |
|------|------|-------------|
| options.key | Uint8Array | 32-byte key for keyed hashing mode |options.context
| | string | Context string for key derivation mode |
Returns: Blake3Hasher with methods:update(data)
- - Add data, returns hasher for chainingfinalize(outputLength?)
- - Finalize and return hashreset()
- - Reset hasher to initial state
`javascript`
const hasher = createHasher();
hasher.update('chunk1');
hasher.update('chunk2');
const digest = hasher.finalize(); // 32 bytes
const long = hasher.reset().update('data').finalize(64); // 64 bytes
Computes a keyed hash (MAC) using BLAKE3.
Parameters:
| Name | Type | Description |
|------|------|-------------|
| key | Uint8Array | 32-byte secret key |message
| | Uint8Array \| string | Message to authenticate |outputLength
| | number | Output length in bytes (default: 32) |
Returns: Uint8Array
`javascript`
const key = new Uint8Array(32).fill(0x42);
const mac = keyedHash(key, 'message'); // 32-byte MAC
const longMac = keyedHash(key, 'message', 64); // 64-byte MAC
Derives a key using BLAKE3's KDF mode.
Parameters:
| Name | Type | Description |
|------|------|-------------|
| context | string | Context string (unique to your application) |keyMaterial
| | Uint8Array \| string | Input key material |outputLength
| | number | Output length in bytes (default: 32) |
Returns: Uint8Array
`javascript`
const key = deriveKey('myapp v1 encryption', 'password');
const longKey = deriveKey('myapp v1', 'password', 64);
Low-level function that operates directly on Uint8Array and returns the raw hash.
Parameters:
| Name | Type | Description |
|------|------|-------------|
| data | Uint8Array | Input data |outputLength
| | number | Output length in bytes (default: 32) |
Returns: Uint8Array
`javascript
import { rawHash } from 'fast-blake3';
const input = new Uint8Array([1, 2, 3]);
const output = rawHash(input); // 32 bytes
const long = rawHash(input, 64); // 64 bytes
`
Static class providing an object-oriented interface.
| Method | Description |
|--------|-------------|
| Blake3.hash(data, options?) | Hash data with optional encoding |Blake3.createHasher(options?)
| | Create streaming hasher |Blake3.keyedHash(key, message, options?)
| | Keyed hash (MAC) |Blake3.deriveKey(context, material, options?)
| | Key derivation (KDF) |
Options:
| Name | Type | Description |
|------|------|-------------|
| encoding | 'hex' \| 'base64' \| 'buffer' | Output format (default: 'hex') |outputLength
| | number | Output length in bytes (default: 32) |
Returns: string | Uint8Array
`javascript
import { Blake3 } from 'fast-blake3';
// Hash with encoding
Blake3.hash('test', { encoding: 'hex' }); // hex string
Blake3.hash('test', { encoding: 'base64' }); // base64 string
Blake3.hash('test', { encoding: 'buffer' }); // Uint8Array
Blake3.hash('test', { encoding: 'hex', outputLength: 64 });
// Keyed hash with encoding
Blake3.keyedHash(key, 'msg', { encoding: 'hex' });
// Key derivation (defaults to buffer for security)
Blake3.deriveKey('ctx', 'material', { encoding: 'buffer' });
`
`javascript
import { toHex, fromHex } from 'fast-blake3';
toHex(new Uint8Array([1, 2, 3])); // '010203'
fromHex('010203'); // Uint8Array([1, 2, 3])
`
`javascript`
(async () => {
const { hash, keyedHash, deriveKey } = await import('fast-blake3');
console.log(hash('hello'));
})();
`html`
Full type definitions included.
`typescript
import {
hash,
createHasher,
keyedHash,
deriveKey,
Blake3,
Blake3Options,
Blake3Hasher
} from 'fast-blake3';
const digest: Uint8Array = hash('test');
const hasher: Blake3Hasher = createHasher();
``