Skip to main content

Python bindings for the Rust blake3 crate

Project description

blake3-py Actions Status PyPI version

Python bindings for the official Rust implementation of BLAKE3, based on PyO3. These bindings expose all the features of BLAKE3, including extendable output, keying, and multithreading. The basic API matches that of Python's standard hashlib module.


from blake3 import blake3

# Hash some input all at once. The input can be bytes, a bytearray, or a memoryview.
hash1 = blake3(b"foobarbaz").digest()

# Hash the same input incrementally.
hasher = blake3()
hash2 = hasher.digest()
assert hash1 == hash2

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

# Use the keyed hashing mode, which takes a 32-byte key.
import secrets
random_key = secrets.token_bytes(32)
message = b"a message to authenticate"
mac = blake3(message, key=random_key).digest()

# Use the key derivation mode, which takes a context string. Context strings
# should be hardcoded, globally unique, and application-specific.
context = "blake3-py 2020-03-04 11:13:10 example context"
key_material = b"usually at least 32 random bytes, not a password"
derived_key = blake3(key_material, derive_key_context=context).digest()

# Extendable output. The default digest size is 32 bytes.
extended = blake3(b"foo").digest(length=100)
assert extended[:32] == blake3(b"foo").digest()
assert extended[75:100] == blake3(b"foo").digest(length=25, seek=75)

# Hash a large input using multiple threads. Note that this can be slower for
# inputs shorter than ~1 MB, and it's a good idea to benchmark it for your use
# case on your platform.
large_input = bytearray(1_000_000)
hash_single = blake3(large_input).digest()
hash_two = blake3(large_input, max_threads=2).digest()
hash_many = blake3(large_input, max_threads=blake3.AUTO).digest()
assert hash_single == hash_two == hash_many

# Copy a hasher that has already accepted some input.
hasher1 = blake3(b"foo")
hasher2 = hasher1.copy()
assert hasher1.digest() == blake3(b"foobar").digest()
assert hasher2.digest() == blake3(b"foobaz").digest()


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. But if you're building the source distribution, or if a binary wheel isn't available for your environment, you'll need to install the Rust toolchain.

C Bindings

Experimental bindings for the official BLAKE3 C implementation are available in the c_impl directory. These will probably not be published on PyPI, and most applications should prefer the Rust-based bindings. But if you can't depend on the Rust toolchain, and you're on some platform that this project doesn't provide binary wheels for, the C-based bindings might be an alternative.

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.3.1.tar.gz (131.1 kB view hashes)

Uploaded source

Built Distributions

blake3-0.3.1-cp310-none-win_amd64.whl (193.3 kB view hashes)

Uploaded cp310

blake3-0.3.1-cp310-none-win32.whl (209.9 kB view hashes)

Uploaded cp310

blake3-0.3.1-cp310-cp310-macosx_11_0_arm64.whl (295.2 kB view hashes)

Uploaded cp310

blake3-0.3.1-cp310-cp310-macosx_10_7_x86_64.whl (317.2 kB view hashes)

Uploaded cp310

blake3-0.3.1-cp39-none-win_amd64.whl (193.9 kB view hashes)

Uploaded cp39

blake3-0.3.1-cp39-none-win32.whl (210.2 kB view hashes)

Uploaded cp39

blake3-0.3.1-cp39-cp39-macosx_11_0_arm64.whl (295.2 kB view hashes)

Uploaded cp39

blake3-0.3.1-cp39-cp39-macosx_10_7_x86_64.whl (317.3 kB view hashes)

Uploaded cp39

blake3-0.3.1-cp38-none-win_amd64.whl (193.7 kB view hashes)

Uploaded cp38

blake3-0.3.1-cp38-none-win32.whl (210.4 kB view hashes)

Uploaded cp38

blake3-0.3.1-cp38-cp38-macosx_11_0_arm64.whl (295.8 kB view hashes)

Uploaded cp38

blake3-0.3.1-cp38-cp38-macosx_10_7_x86_64.whl (318.0 kB view hashes)

Uploaded cp38

blake3-0.3.1-cp37-none-win_amd64.whl (193.7 kB view hashes)

Uploaded cp37

blake3-0.3.1-cp37-none-win32.whl (209.8 kB view hashes)

Uploaded cp37

blake3-0.3.1-cp36-none-win_amd64.whl (191.2 kB view hashes)

Uploaded cp36

blake3-0.3.1-cp36-none-win32.whl (207.9 kB view hashes)

Uploaded cp36

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page