fast-blake3
v1.0.1
Published
Fast BLAKE3 cryptographic hash function with TypeScript support
Maintainers
Readme
fast-blake3
High-performance BLAKE3 cryptographic hash implementation for JavaScript/TypeScript
Features
- Basic hashing
- Streaming/incremental hashing
- Keyed hashing (MAC)
- Key derivation (KDF)
- ESM and CommonJS support
- Full TypeScript types
Installation
npm install fast-blake3Quick Start
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');API Reference
hash(data, outputLength?)
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
hash('hello'); // 32-byte hash (default)
hash('hello', 64); // 64-byte hash
hash(new Uint8Array([1,2,3])); // Binary inputcreateHasher(options?)
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
const hasher = createHasher();
hasher.update('chunk1');
hasher.update('chunk2');
const digest = hasher.finalize(); // 32 bytes
const long = hasher.reset().update('data').finalize(64); // 64 byteskeyedHash(key, message, outputLength?)
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
const key = new Uint8Array(32).fill(0x42);
const mac = keyedHash(key, 'message'); // 32-byte MAC
const longMac = keyedHash(key, 'message', 64); // 64-byte MACderiveKey(context, keyMaterial, outputLength?)
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
const key = deriveKey('myapp v1 encryption', 'password');
const longKey = deriveKey('myapp v1', 'password', 64);rawHash(data, outputLength?)
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
import { rawHash } from 'fast-blake3';
const input = new Uint8Array([1, 2, 3]);
const output = rawHash(input); // 32 bytes
const long = rawHash(input, 64); // 64 bytesBlake3 Class
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
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' });Utility Functions
import { toHex, fromHex } from 'fast-blake3';
toHex(new Uint8Array([1, 2, 3])); // '010203'
fromHex('010203'); // Uint8Array([1, 2, 3])CommonJS Usage
(async () => {
const { hash, keyedHash, deriveKey } = await import('fast-blake3');
console.log(hash('hello'));
})();Browser Usage
<script type="module">
import { hash } from './dist/esm/index.js';
console.log(hash('hello'));
</script>TypeScript
Full type definitions included.
import {
hash,
createHasher,
keyedHash,
deriveKey,
Blake3,
Blake3Options,
Blake3Hasher
} from 'fast-blake3';
const digest: Uint8Array = hash('test');
const hasher: Blake3Hasher = createHasher();