跳转到主要内容

Rust blake3 crate的Python绑定

项目描述

blake3-py Actions Status PyPI version

Python绑定BLAKE3的官方Rust实现,基于PyO3。这些绑定公开了BLAKE3的所有功能,包括可扩展输出、密钥和多线程。基本API与Python标准hashlib模块匹配。

示例

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

# Hash the same input fluently.
assert hash1 == blake3(b"foo").update(b"bar").update(b"baz").digest()

# 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

# Hash a file with multiple threads using memory mapping. This is what b3sum
# does by default.
file_hasher = blake3(max_threads=blake3.AUTO)
file_hasher.update_mmap("/big/file.txt")
file_hash = file_hasher.digest()

# Copy a hasher that's 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()

安装

pip install blake3

与Pip一样,您可能需要使用sudo或上述命令中的--user标志,具体取决于您如何在系统上安装Python。

大多数环境在PyPI上提供了二进制轮。但是,如果您正在构建源分发,或者如果为您的环境不可用二进制轮,您需要安装Rust工具链

C绑定

c_impl目录中提供了官方BLAKE3 C实现的实验性绑定。这些可能不会在PyPI上发布,并且大多数应用程序应首选基于Rust的绑定。但是,如果您无法依赖于Rust工具链,并且您在项目不提供二进制轮的平台之一上,基于C的绑定可能是一个替代方案。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

blake3-0.4.1.tar.gz (117.7 kB 查看哈希值)

上传时间 源代码

构建发行版

blake3-0.4.1-cp312-none-win_amd64.whl (210.1 kB 查看哈希值)

上传时间 CPython 3.12 Windows x86-64

blake3-0.4.1-cp312-none-win32.whl (226.8 kB 查看哈希值)

上传时间 CPython 3.12 Windows x86

blake3-0.4.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB 查看哈希值)

上传时间 CPython 3.12 manylinux: glibc 2.17+ x86-64

blake3-0.4.1-cp312-cp312-macosx_11_0_arm64.whl (329.1 kB 查看哈希值)

上传时间 CPython 3.12 macOS 11.0+ ARM64

blake3-0.4.1-cp312-cp312-macosx_10_12_x86_64.whl (343.9 kB 查看哈希值)

上传时间 CPython 3.12 macOS 10.12+ x86-64

blake3-0.4.1-cp311-none-win_amd64.whl (210.1 kB 查看哈希值)

上传时间 CPython 3.11 Windows x86-64

blake3-0.4.1-cp311-none-win32.whl (226.9 kB 查看哈希值)

上传时间 CPython 3.11 Windows x86

blake3-0.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ x86-64

blake3-0.4.1-cp311-cp311-macosx_11_0_arm64.whl (329.3 kB 查看哈希值)

上传于 CPython 3.11 macOS 11.0+ ARM64

blake3-0.4.1-cp311-cp311-macosx_10_12_x86_64.whl (344.7 kB 查看哈希值)

上传于 CPython 3.11 macOS 10.12+ x86-64

blake3-0.4.1-cp310-none-win_amd64.whl (210.1 kB 查看哈希值)

上传于 CPython 3.10 Windows x86-64

blake3-0.4.1-cp310-none-win32.whl (226.9 kB 查看哈希值)

上传于 CPython 3.10 Windows x86

blake3-0.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB 查看哈希值)

上传于 CPython 3.10 manylinux: glibc 2.17+ x86-64

blake3-0.4.1-cp310-cp310-macosx_11_0_arm64.whl (329.1 kB 查看哈希值)

上传于 CPython 3.10 macOS 11.0+ ARM64

blake3-0.4.1-cp310-cp310-macosx_10_12_x86_64.whl (344.7 kB 查看哈希值)

上传于 CPython 3.10 macOS 10.12+ x86-64

blake3-0.4.1-cp39-none-win_amd64.whl (210.3 kB 查看哈希值)

上传于 CPython 3.9 Windows x86-64

blake3-0.4.1-cp39-none-win32.whl (227.0 kB 查看哈希值)

上传于 CPython 3.9 Windows x86

blake3-0.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB 查看哈希值)

上传于 CPython 3.9 manylinux: glibc 2.17+ x86-64

blake3-0.4.1-cp39-cp39-macosx_11_0_arm64.whl (330.0 kB 查看哈希值)

上传于 CPython 3.9 macOS 11.0+ ARM64

blake3-0.4.1-cp39-cp39-macosx_10_12_x86_64.whl (345.5 kB 查看哈希值)

上传时间: CPython 3.9 macOS 10.12+ x86-64

blake3-0.4.1-cp38-none-win_amd64.whl (210.0 kB 查看哈希值)

上传时间: CPython 3.8 Windows x86-64

blake3-0.4.1-cp38-none-win32.whl (227.0 kB 查看哈希值)

上传时间: CPython 3.8 Windows x86

blake3-0.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB 查看哈希值)

上传时间: CPython 3.8 manylinux: glibc 2.17+ x86-64

blake3-0.4.1-cp38-cp38-macosx_11_0_arm64.whl (329.9 kB 查看哈希值)

上传时间: CPython 3.8 macOS 11.0+ ARM64

blake3-0.4.1-cp38-cp38-macosx_10_12_x86_64.whl (345.0 kB 查看哈希值)

上传时间: CPython 3.8 macOS 10.12+ x86-64

blake3-0.4.1-cp37-none-win_amd64.whl (210.1 kB 查看哈希值)

上传时间: CPython 3.7 Windows x86-64

blake3-0.4.1-cp37-none-win32.whl (226.5 kB 查看哈希值)

上传时间: CPython 3.7 Windows x86

blake3-0.4.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB 查看哈希值)

上传时间: CPython 3.7m manylinux: glibc 2.17+ x86-64

blake3-0.4.1-cp37-cp37m-macosx_10_12_x86_64.whl (345.3 kB 查看哈希值)

上传时间: CPython 3.7m macOS 10.12+ x86-64