百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
B

blake3

> 编程语言
开源

JavaScript 使用 BLAKE3 加密: 原生 Node 绑定 (如果可用) 和 WebAssembly

199 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

JavaScript 使用 BLAKE3 加密: 原生 Node 绑定 (如果可用) 和 WebAssembly

BLAKE3

BLAKE3 running in JavaScript (node.js and browsers) via native bindings, where available, or WebAssembly.

bash
npm install blake3

Additionally, there's a flavor of the package which is identical except that it will not download native Node.js bindings and use only WebAssembly:

bash
npm install blake3-wasm

Table of Contents

  • Quickstart
  • API
    • Node.js
      • hash(data: BinaryLike, options?: { length: number }): Buffer
      • keyedHash(key: BinaryLike, data: BinaryLike, options?: { length: number }): Buffer
      • deriveKey(context: BinaryLike, material: BinaryLike, options?: { length: number }): Buffer
      • Hasher
        • createHash(): Hasher
        • createKeyed(key: BinaryLike): Hasher
        • createDeriveKey(context: BinaryLike): Hasher
        • hasher.update(data: BinaryLike): this
        • hasher.digest(encoding?: string, options?: { length: number })): Buffer | string
        • hasher.reader(): HashReader
        • hasher.dispose()
      • HashReader
        • reader.position: bigint
        • reader.readInto(target: Uint8Array): number
        • reader.read(bytes: number): Buffer
        • reader.view(target: Buffer): Readonly
        • reader[Symbol.iterator]
    • Browser
      • hash(data: BinaryLike, options?: { length: number }): Hash
      • keyedHash(key: BinaryLike, data: BinaryLike, options?: { length: number }): Hash
      • deriveKey(context: BinaryLike, material: BinaryLike, options?: { length: number }): Hash
      • Hash
        • hash.equals(other: Uint8Array)
        • hash.toString(encoding: 'hex' | 'base64' | 'utf8'): string
      • Hasher
        • createHash(): Hasher
        • createKeyed(key: BinaryLike): Hasher
        • createDeriveKey(context: BinaryLike): Hasher
        • hasher.update(data: BinaryLike): this
        • hasher.digest(encoding?: 'hex' | 'base64' | 'utf8', options?: { length: number })): Hash | string
        • hasher.reader(): HashReader
        • hasher.dispose()
      • HashReader
        • reader.position: bigint
        • reader.readInto(target: Uint8Array): number
        • reader.read(bytes: number): Hash
        • reader.view(target: Buffer): Readonly
        • reader[Symbol.iterator]
  • Speed
  • Other (JS) Implementations
  • Contributing
    • Publishing

Quickstart

If you're on Node, import the module via

javascript
const blake3 = require('blake3');

blake3.hash('foo'); // => Buffer

If you're in the browser, import blake3/browser. This includes a WebAssembly binary, so you probably want to import it asynchronously, like so:

javascript
import('blake3/browser').then((blake3) => {
  blake3.hash('foo'); // => Uint8Array
});

The API is very similar in Node.js and browsers, but Node supports and returns Buffers and a wider range of input and output encoding.

More complete example:

javascript
const { hash, createHash } = require('blake3');

hash('some string'); // => hash a string to a uint8array

// Update incrementally (Node and Browsers):
const hash = createHash();
stream.on('data', (d) => hash.update(d));
stream.on('error', (err) => {
  // hashes use unmanaged memory in WebAssembly, always free them if you don't digest()!
  hash.dispose();
  throw err;
});
stream.on('end', () => finishedHash(hash.digest()));

// Or, in Node, it's also a transform stream:
createReadStream('file.txt')
  .pipe(createHash())
  .on('data', (hash) => console.log(hash.toString('hex')));

API

Node.js

The Node API can be imported via require('blake3').

hash(data: BinaryLike, options?: { length: number }): Buffer

Returns a hash for the given data. The data can be a string, buffer, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Buffer.

keyedHash(key: BinaryLike, data: BinaryLike, options?: { length: number }): Buffer

Returns keyed a hash for the given data. The key must be exactly 32 bytes. The data can be a string, buffer, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Buffer.

For more information, see the blake3 docs.

deriveKey(context: BinaryLike, material: BinaryLike, options?: { length: number }): Buffer

The key derivation function. The data can be a string, buffer, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Buffer.

For more information, see the blake3 docs.

Hasher

The hasher is a type that lets you incrementally build a hash. It's compatible with Node's crypto hash instance. For instance, it implements a transform stream, so you could do something like:

javascript
createReadStream('file.txt')
  .pipe(createHash())
  .on('data', (hash) => console.log(hash.toString('hex')));
createHash(): Hasher

Creates a new hasher instance using the standard hash function.

createKeyed(key: BinaryLike): Hasher

Creates a new hasher instance for a keyed hash. For more information, see the blake3 docs.

createDeriveKey(context: BinaryLike): Hasher

Creates a new hasher instance for the key derivation function. For more information, see the blake3 docs.

hasher.update(data: BinaryLike): this

Adds data to a hash. The data can be a string, buffer, typedarray, array buffer, or array.

hasher.digest(encoding?: string, options?: { length: number })): Buffer | string

Returns the hash of the data. If an encoding is given, a string will be returned. Otherwise, a Buffer is returned. Optionally, you can specify the requested byte length of the hash.

hasher.reader(): HashReader

Returns a HashReader for the current hash.

hasher.dispose()

This is a no-op for Node.js.

HashReader

The hash reader can be returned from hashing functions. Up to 264-1 bytes of data can be read from BLAKE3 hashes; this structure lets you read those. Note that, like hash, this is an object which needs to be manually disposed of.

reader.position: bigint

A property which gets or sets the position of the reader in the output stream. A RangeError is thrown if setting this to a value less than 0 or greater than 264-1. Note that this is a bigint, not a standard number.

javascript
reader.position += 32n; // advance the reader 32 bytes
reader.readInto(target: Uint8Array): number

Reads bytes into the target array, filling it up and advancing the reader's position. It returns the number of bytes written, which may be less then the size of the target buffer if position 264-1 is reached.

reader.read(bytes: number): Buffer

Reads and returns the given number of bytes from the reader, and advances the position. A RangeError is thrown if reading this data puts the reader past 264-1 bytes.

reader.view(target: Buffer): Readonly

Returns a view of the given number of bytes from the reader. The view can be used synchronously, but must not be reused later. This is more efficient when using the webassembly version of the module.

Fewer bytes may be returned than requested, if the number is large (>1MB).

reader[Symbol.iterator]

The reader is an Iterable of Readonlys. Like the view method, the iterated arrays will be reused internally on the next iteration, so if you need data, you should copy it out of the iterated array.

Browser

The browser API can be imported via import('blake3/browser'), which works well with Webpack.

Note that you must call the load() method before using any function in the module.

javascript
import * as blake3 from 'blake3/browser-async';

blake3.load().then(() => {
  console.log(blake3.hash('hello world'));
});

hash(data: BinaryLike, options?: { length: number }): Hash

Returns a hash for the given data. The data can be a string, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Hash instance.

keyedHash(key: BinaryLike, data: BinaryLike, options?: { length: number }): Hash

Returns keyed a hash for the given data. The key must be exactly 32 bytes. The data can be a string, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Hash instance.

For more information, see the blake3 docs.

deriveKey(context: BinaryLike, material: BinaryLike, options?: { length: number }): Hash

The key derivation function. The data can be a string, typedarray, array buffer, or array. By default, it generates the first 32 bytes of the hash for the data, but this is configurable. It returns a Hash instance.

For more information, see the blake3 docs.

Hash

A Hash is the type returned from hash functions and the hasher in the browser. It's a Uint8Array with a few additional helper methods.

hash.equals(other: Uint8Array)

Returns whether this hash equals the other hash, via a constant-time equality check.

hash.toString(encoding: 'hex' | 'base64' | 'utf8'): string

Hasher

The hasher is a type that lets you incrementally build a hash. For instance, you can hash a fetched page like:

javascript
const res = await fetch('https://example.com');
const body = await res.body;

const hasher = blake3.createHash();
const reader = body.getReader();

while (true) {
  const { done, value } = await reader.read();
  if (done) {
    break;
  }

  hasher.update(value);
}

console.log('Hash of', res.url, 'is', hasher.digest('hex'));

Converts the hash to a string with the given encoding.

createHash(): Hasher

Creates a new hasher instance using the standard hash function.

createKeyed(key: BinaryLike): Hasher

Creates a new hasher instance for a keyed hash. For more information, see the blake3 docs.

createDeriveKey(context: BinaryLike): Hasher

Issues· 16 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptblake3hashneonnode-addon

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言