• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Python
  • License
    Other
  • Created over 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python bindings for the BLAKE3 cryptographic hash function

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.

Examples

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()
hasher.update(b"foo")
hasher.update(b"bar")
hasher.update(b"baz")
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()
hasher1.update(b"bar")
hasher2.update(b"baz")
assert hasher1.digest() == blake3(b"foobar").digest()
assert hasher2.digest() == blake3(b"foobaz").digest()

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. 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.

More Repositories

1

duct.rs

a Rust library for running child processes
Rust
807
star
2

sha256_project

The SHA-256 Project, developed for NYU Tandon's Applied Cryptography course
Python
533
star
3

bao

an implementation of BLAKE3 verified streaming
Rust
465
star
4

clinacl

a command line tool for playing with NaCl
Python
253
star
5

blake2_simd

high-performance implementations of BLAKE2b/s/bp/sp in pure Rust with dynamic SIMD
C
126
star
6

duct.py

a Python library for running child processes
Python
113
star
7

fbmessenger

[deprecated] a PyQt clone of Facebook Messenger for Windows
Python
102
star
8

os_pipe.rs

a cross-platform library for opening OS pipes in Rust
Rust
97
star
9

blake3-6502

the BLAKE3 hash function implemented in 6502 assembly
Assembly
56
star
10

shared_child.rs

a wrapper around std::process::Child that lets multiple threads wait or kill at once
Rust
39
star
11

applied_crypto_2021_fall

problem sets for CS-GY 6903 Applied Cryptography
Python
39
star
12

bessie

an authenticated, chunked cipher based on BLAKE3
Rust
21
star
13

pure_python_blake3

a pure Python implementation of BLAKE3
Python
15
star
14

dotfiles

Jack's config files
Shell
12
star
15

blake3_reference_impl_c

a C port of the BLAKE3 Rust reference implementation
C
10
star
16

blake2_c.rs

a safe wrapper around the BLAKE2 C implementation (deprecated in favor of blake2b_simd and blake2s_simd)
Rust
7
star
17

riddance

a reservable, retiring, recyclable slotmap/arena (WIP)
Rust
6
star
18

kangarootwelve_xkcp.rs

A Rust wrapper around the XKCP implementation of the KangarooTwelve hash function
C
6
star
19

unsafe_rust_is_not_c_talk

6
star
20

founder

A wrapper around fzf and fd, which keeps track of files you've opened before
Rust
6
star
21

arch

Jack's install scripts for Arch Linux.
Shell
5
star
22

bao_presentation

HTML
5
star
23

avx512_test

Rust
4
star
24

bao_experiments

benchmarks for various design changes to github.com/oconnor663/bao
HTML
4
star
25

rust-examples

scary things that Rust keeps you safe from
Rust
4
star
26

mersenne_breaker

Rust
4
star
27

copy_in_place

[deprecated] a single-function Rust crate providing a safe wrapper around ptr::copy for efficient copying within slices
Rust
4
star
28

jacko.io

source files for my personal website
Rust
4
star
29

blake3_aead

experimental
Rust
4
star
30

chacha20_simd

experimental
Rust
4
star
31

pure_python_salsa_chacha

pure Python implementations of Salsa20, XSalsa20, ChaCha20 (IETF), and XChaCha20
Python
4
star
32

simd_examples

Rust
3
star
33

happy_eyeballs

A demo comparison between futures in Rust and in Python's Trio
Rust
3
star
34

thread_tester

dummy project
Rust
3
star
35

blake2s_simd

DEPRECATED
Rust
3
star
36

keybase_validator

Rust
3
star
37

zsh-sensible

zsh defaults that everyone can agree on
Shell
3
star
38

atomic_examples

Rust
3
star
39

async_demo

teaching examples for async Rust
Rust
3
star
40

baokeshed

Rust
3
star
41

easy_steps_chinese_vocab

Python
3
star
42

test

funny filenames
3
star
43

cpp_rust_talk

https://youtu.be/IPmRDS0OSxM
JavaScript
3
star
44

tosqlite

Python
3
star
45

cooper_pair_box

Jack's physics senior project. A model of the inductively-shunted Cooper pair box.
Python
2
star
46

cell_talk

JavaScript
2
star
47

dungeon_of_despair

a great text-based dungeon crawler from our undergraduate years
Scheme
2
star
48

qol-armor

Lua
2
star
49

supercop

Jack's working copy of the SUPERCOP source tree, for BLAKE3 submissions (see crypto_hash/blake3/)
C
2
star
50

rust_blake3_c_bindings

C
2
star
51

scratch

2
star
52

unsound_helpers

Rust
2
star
53

incremental_messagepack

Example implementation of an incremental MessagePack decoder.
Python
2
star
54

appveyor-exec-py

reproducing a bug where python scripts don't execute on AppVeyor
Python
2
star
55

cryptopals-rust

HTML
2
star
56

pooter

for Mi
Objective-C
2
star
57

basex_gmp

C
2
star
58

clap_unicode_test

exercising a bug in the Rust Clap library
Rust
2
star
59

palindrompit

A palindromic tip calculator for Android
Scala
2
star
60

pyo3_pybuffer_repro

repro code for a memory corruption issue with PyO3 + PyPy
Rust
1
star
61

broken-make

a demo of a broken Make build
C
1
star
62

blog_os

Rust
1
star
63

actions-test

messing around with GitHub Actions
Rust
1
star
64

curve25519

code from an interview, not interesting :)
Python
1
star
65

rust-practice

Rust
1
star
66

fsanitize_example

Rust
1
star
67

ttt

Tic-Tac-Toe
Python
1
star
68

codingexamples

Python
1
star
69

yalie

Yet Another Lisp Interpreting Experiment: Jack's comp sci senior project. An interpreter for an object-oriented Lisp.
Python
1
star
70

cell_utils

EXPERIMENTAL CODE, not published on crates.io
Rust
1
star
71

go_calling_rust_example

Go
1
star
72

find_xor_collisions

Rust
1
star
73

peru-server

a Flask web site that can host peru modules and generate a peru.yaml for you
Python
1
star
74

peru-pygit2-example

an example project using peru and make to build against pygit2
Makefile
1
star
75

cc_hello_world

minimal demo project for testing `cc` changes
Rust
1
star