Skip to main content

Python bindings for the Rust blake3 crate

Project description

blake3-py Actions Status PyPI version

Python bindings for the Rust blake3 crate, based on PyO3. This a proof of concept, not yet fully-featured or production-ready. See also the soundness concerns below.

Example

import blake3

hash1 = blake3.blake3(b"foobarbaz").digest()

hasher = blake3.blake3()
hasher.update(b"foo")
hasher.update(b"bar")
hasher.update(b"baz")
hash2 = hasher.digest()

assert hash1 == hash2

print("The hash of 'hello world' is:",
      blake3.blake3(b"hello world").hexdigest())

If you've cloned the GitHub project, and you want to experiment with the scripts there, they work like this:

# Build the shared library first.
$ ./build.py

# Hash some input.
$ echo hello world | ./example.py
dc5a4edb8240b018124052c330270696f96771a63b45250a5c17d3000e823355

# Run the tests.
$ ./test.py

Installation

pip install blake3

As usual with Pip, you might need to use sudo or the --user flag with the command above, depending on how you installed Python on your system.

There are binary wheels available on PyPI for most environments, so most users do not need a Rust toolchain. If you're building the source distribution, or if a binary wheel isn't available for your environment, you'll need the nightly Rust toolchain (required by PyO3). This project includes a rust-toolchain file, so rustup will install and invoke the nightly toolchain automatically.

Features

Currently only basic hashing is supported, with the default 32-byte output size. Missing BLAKE3 features should be easy to add, though I'm not sure exactly what the API should look like. Missing features include:

  • variable-length output
  • an incremental output reader
  • the keyed hashing mode
  • the key derivation mode
  • optional multi-threading

Thread Safety and Soundness

This wrapper is not currently thread-safe. Like the hash implementations in the Python standard library, we release the GIL during update, to avoid blocking the entire process. However, that means that calling the update method from multiple threads at the same time is undefined behavior. We could solve this by putting a Mutex inside the wrapper type, but I'd like to get some expert advice about the best practice here first.

A deeper problem is that another thread might mutate a bytearray while we're hashing it, and while our Rust code is treating it as a &[u8]. That violates Rust's aliasing guarantees and is also technically undefined behavior. However, the only possible way to solve this while still supporting bytearray would be to retain the GIL. Again, I'm in need of expert advice.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blake3-0.1.2.tar.gz (13.8 kB view hashes)

Uploaded Source

Built Distributions

blake3-0.1.2-cp38-none-win_amd64.whl (119.2 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

blake3-0.1.2-cp38-none-win32.whl (117.6 kB view hashes)

Uploaded CPython 3.8 Windows x86

blake3-0.1.2-cp38-cp38-manylinux1_x86_64.whl (755.6 kB view hashes)

Uploaded CPython 3.8

blake3-0.1.2-cp38-cp38-macosx_10_7_x86_64.whl (160.2 kB view hashes)

Uploaded CPython 3.8 macOS 10.7+ x86-64

blake3-0.1.2-cp37-none-win_amd64.whl (119.2 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

blake3-0.1.2-cp37-none-win32.whl (117.6 kB view hashes)

Uploaded CPython 3.7 Windows x86

blake3-0.1.2-cp37-cp37m-manylinux1_x86_64.whl (755.5 kB view hashes)

Uploaded CPython 3.7m

blake3-0.1.2-cp37-cp37m-macosx_10_7_x86_64.whl (160.2 kB view hashes)

Uploaded CPython 3.7m macOS 10.7+ x86-64

blake3-0.1.2-cp36-none-win_amd64.whl (119.8 kB view hashes)

Uploaded CPython 3.6 Windows x86-64

blake3-0.1.2-cp36-none-win32.whl (118.2 kB view hashes)

Uploaded CPython 3.6 Windows x86

blake3-0.1.2-cp36-cp36m-manylinux1_x86_64.whl (756.3 kB view hashes)

Uploaded CPython 3.6m

blake3-0.1.2-cp36-cp36m-macosx_10_7_x86_64.whl (160.9 kB view hashes)

Uploaded CPython 3.6m macOS 10.7+ x86-64

blake3-0.1.2-cp35-none-win_amd64.whl (119.8 kB view hashes)

Uploaded CPython 3.5 Windows x86-64

blake3-0.1.2-cp35-none-win32.whl (118.2 kB view hashes)

Uploaded CPython 3.5 Windows x86

blake3-0.1.2-cp35-cp35m-manylinux1_x86_64.whl (756.2 kB view hashes)

Uploaded CPython 3.5m

blake3-0.1.2-cp35-cp35m-macosx_10_7_x86_64.whl (160.8 kB view hashes)

Uploaded CPython 3.5m macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page