blake3_ffi 0.2.0 copy "blake3_ffi: ^0.2.0" to clipboard
blake3_ffi: ^0.2.0 copied to clipboard

BLAKE3 hashing for Dart over native FFI, up to 14x faster than SHA-256. One- shot, streaming, keyed (MAC), and key derivation. C auto-compiled at build, no binaries to ship.

example/blake3_ffi_example.dart

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:blake3_ffi/blake3_ffi.dart';

/// Two things this package is good at: hashing a byte buffer in one call,
/// and hashing a large file in a stream without holding it all in memory.
void main() {
  // One-shot: hash a small message and print it as hex.
  final message = utf8.encode('The quick brown fox jumps over the lazy dog');
  print('blake3("...dog") = ${blake3Hex(message)}');

  // Streaming: hash this source file chunk by chunk.
  final digest = hashFile(File(Platform.script.toFilePath()));
  print('this file        = $digest');

  // Keyed hashing (a MAC): both sides share a 32-byte key. The *Hex variants
  // return the digest as hex directly, so there is no formatting to write.
  final key = Uint8List.fromList(List<int>.generate(32, (i) => i));
  print('keyed("...dog")  = ${blake3KeyedHex(key, message)}');

  // Key derivation: turn a shared secret into a purpose-bound subkey.
  final subkey = blake3DeriveKeyHex(
    'example.com 2026 session cookie v1',
    utf8.encode('master secret'),
  );
  print('derived key      = $subkey');
}

/// Streams [file] through a [Blake3Hasher] 64 KiB at a time, so memory use
/// stays flat no matter how large the file is.
String hashFile(File file) {
  final hasher = Blake3Hasher();
  try {
    final handle = file.openSync();
    try {
      final buffer = Uint8List(64 * 1024);
      while (true) {
        final read = handle.readIntoSync(buffer);
        if (read == 0) break;
        hasher.update(Uint8List.sublistView(buffer, 0, read));
      }
    } finally {
      handle.closeSync();
    }
    return hasher.finalizeHex();
  } finally {
    hasher.dispose();
  }
}
0
likes
0
points
307
downloads

Publisher

verified publisherdeveloperyusuf.com

Weekly Downloads

BLAKE3 hashing for Dart over native FFI, up to 14x faster than SHA-256. One- shot, streaming, keyed (MAC), and key derivation. C auto-compiled at build, no binaries to ship.

Repository (GitHub)
View/report issues

Topics

#blake3 #hashing #cryptography #ffi #performance

License

unknown (license)

Dependencies

code_assets, ffi, hooks, native_toolchain_c

More

Packages that depend on blake3_ffi