webbuf_blake3
Library
webbuf_blake3
is a Rust library that provides an easy-to-use interface for
computing BLAKE3 hashes, double hashes, and Message Authentication Codes (MAC)
using the BLAKE3 algorithm. This library is designed to work with both Rust and
JavaScript/TypeScript by enabling WASM support with an optional feature.
Features
- BLAKE3 Hashing: Compute hashes with the BLAKE3 algorithm for data integrity and verification.
- Double Hashing: Hash a message twice to provide enhanced security (e.g., for blockchain applications).
- MAC Generation: Create a keyed MAC using BLAKE3 to verify data integrity and authenticity.
- WASM Support: Build the library for WebAssembly with the
wasm
feature, enabling usage in JavaScript environments.
Installation
Add the following dependency to your Cargo.toml
:
[]
= { = "0.8.0", = ["wasm"] } # Enable WASM if needed
To build the library for Rust-only usage, skip the wasm
feature:
[]
= "0.8.0"
Usage
Rust Example
use ;
// Compute a BLAKE3 hash
let data = b"hello world";
let hash = blake3_hash.unwrap;
println!;
// Compute a double BLAKE3 hash
let double_hash = double_blake3_hash.unwrap;
println!;
// Generate a keyed MAC
let key = ; // 32-byte key
let mac = blake3_mac.unwrap;
println!;
JavaScript/TypeScript Example (with WASM)
When building with wasm-pack
, the functions are exposed to JavaScript.
Example:
import init from "./your-wasm-package";
;
Building with WASM
To build the library for WebAssembly, use:
To build the library for native Rust usage (without WASM):
API
blake3_hash(data: &[u8]) -> Result<Vec<u8>, String>
Computes a BLAKE3 hash for the given data.
double_blake3_hash(data: &[u8]) -> Result<Vec<u8>, String>
Computes a double BLAKE3 hash by hashing the input twice.
blake3_mac(key: &[u8], data: &[u8]) -> Result<Vec<u8>, String>
Generates a keyed MAC using the BLAKE3 algorithm. The key must be exactly 32 bytes.
Tests
The library includes unit tests to ensure correctness. Run tests with:
Example Test Case:
License
This project is licensed under the MIT License. See the LICENSE
file for
details.
Contributing
Feel free to open issues and submit pull requests. Contributions are welcome!