xxHash Python Binding

raw JSON →
3.6.0 verified Tue May 12 auth: no python install: verified quickstart: verified

xxhash is a Python binding for the high-performance, non-cryptographic xxHash library by Yann Collet. It is widely favored for its speed and efficiency in applications requiring fast hashing of large volumes of data, such as data integrity checks, file deduplication, and checksum generation. The library is actively maintained, with version 3.6.0 currently available, and new releases occurring a few times a year, often to support newer Python versions and xxHash upstream updates.

pip install xxhash
error ModuleNotFoundError: No module named 'xxhash'
cause The xxhash library is not installed in the current Python environment.
fix
pip install xxhash
error TypeError: a bytes-like object is required, not 'str'
cause The xxhash hashing functions expect byte-like objects (e.g., `bytes`), but a Python string was provided directly.
fix
Encode the string to bytes before hashing, for example: xxhash.xxh64('hello world'.encode('utf-8'))
error error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
cause Installing the `xxhash` Python package on Windows requires a C++ compiler to build its C extension, but the necessary Visual C++ Build Tools are not installed.
fix
Download and install the "Build Tools for Visual Studio" from the provided link, ensuring the "Desktop development with C++" workload or just the C++ build tools are selected during installation.
error AttributeError: type object 'xxhash.xxh64' has no attribute 'hexdigest'
cause The `hexdigest` or `intdigest` method was called on the `xxh64` class itself, rather than on an instance of the hasher object returned by calling `xxh64()` with data.
fix
Call xxh64() with the data to get a hasher instance, then call hexdigest() on that instance: xxhash.xxh64(b'data').hexdigest()
breaking Python 3.6 support was dropped in `xxhash` v3.3.0. Users on Python 3.6 or older must use `xxhash` v3.2.0 or earlier.
fix Upgrade to Python 3.7+ or pin `xxhash<3.3.0`.
breaking `xxhash` v2.0.0 introduced XXH3 hashes (`xxh3_64`, `xxh3_128`) and required the underlying xxHash C library version 0.8.0 or newer.
fix Ensure your environment or bundled C library meets the version requirement if building from source. For new code, consider using XXH3 algorithms for better performance.
deprecated `xxhash.VERSION_TUPLE` has been deprecated in v3.6.0 and will be removed in the next major release.
fix Avoid using `xxhash.VERSION_TUPLE`. Use `xxhash.VERSION` for the module version string.
gotcha xxHash is a non-cryptographic hash function designed for speed and is NOT suitable for security-critical applications like password hashing, digital signatures, or HMAC where collision resistance is paramount.
fix Use cryptographic hash functions (e.g., from Python's `hashlib` module) for security-sensitive contexts.
gotcha The `seed` argument for `xxh32` and `xxh64` functions expects an unsigned 32-bit or 64-bit integer, respectively. Providing a negative value or an overly large integer can lead to unexpected behavior.
fix Ensure the `seed` value is a non-negative integer within the valid range for the chosen hash function (0 to 2^32-1 for xxh32, 0 to 2^64-1 for xxh64).
breaking As of `xxhash` v0.3.0, the `digest()` method returns bytes in big-endian representation of the integer hash. Prior versions returned little-endian, which is a breaking change for compatibility if upgrading from very old versions.
fix If migrating from a pre-0.3.0 version and relying on `digest()` byte order, update logic to expect big-endian. For new code, this is the default and expected behavior.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.00s 18.3M
3.10 alpine (musl) - - 0.00s 18.3M
3.10 slim (glibc) wheel 1.6s 0.00s 19M
3.10 slim (glibc) - - 0.00s 19M
3.11 alpine (musl) wheel - 0.00s 20.2M
3.11 alpine (musl) - - 0.00s 20.2M
3.11 slim (glibc) wheel 1.7s 0.00s 21M
3.11 slim (glibc) - - 0.00s 21M
3.12 alpine (musl) wheel - 0.00s 12.0M
3.12 alpine (musl) - - 0.00s 12.0M
3.12 slim (glibc) wheel 1.5s 0.00s 13M
3.12 slim (glibc) - - 0.00s 13M
3.13 alpine (musl) wheel - 0.00s 11.8M
3.13 alpine (musl) - - 0.00s 11.7M
3.13 slim (glibc) wheel 1.5s 0.00s 13M
3.13 slim (glibc) - - 0.00s 12M
3.9 alpine (musl) wheel - 0.00s 17.8M
3.9 alpine (musl) - - 0.00s 17.8M
3.9 slim (glibc) wheel 1.9s 0.00s 19M
3.9 slim (glibc) - - 0.00s 19M

This example demonstrates both one-shot and incremental hashing using the `xxh64` and `xxh3_128` algorithms. Data must be provided as bytes. An optional seed can be used to generate distinct hash outputs for identical input data.

import xxhash

data_to_hash = b"Hello, xxHash! This is some data to be hashed."

# One-shot hashing
hash_value_64 = xxhash.xxh64(data_to_hash, seed=0).hexdigest()
hash_value_128 = xxhash.xxh3_128(data_to_hash).hexdigest()

print(f"XXH64 Hash: {hash_value_64}")
print(f"XXH3_128 Hash: {hash_value_128}")

# Incremental hashing
hasher = xxhash.xxh64(seed=42)
hasher.update(b"First chunk ")
hasher.update(b"of data.")
incremental_hash = hasher.hexdigest()
print(f"Incremental XXH64 Hash: {incremental_hash}")