FNV Hash Fast
FNV Hash Fast is a high-performance Python library providing FNV-1a hash functions (32-bit and 64-bit). It leverages C extensions for maximum speed, offering a significantly faster alternative to pure Python implementations. The current version is 2.0.2, with recent releases focusing on performance improvements and platform support.
Common errors
-
TypeError: a bytes-like object is required, not 'str'
cause Attempting to hash a Python string (`str`) instead of a bytes-like object.fixEnsure the input to `fnv1a_32()` or `fnv1a_64()` is a `bytes` object. Convert strings using `.encode('utf-8')` (or another appropriate encoding). -
ModuleNotFoundError: No module named 'fnvhash'
cause The `fnv-hash-fast` package is not installed in the current Python environment, or the import statement is incorrect.fixInstall the package using `pip install fnv-hash-fast`. Verify your Python environment is active and the import is `from fnvhash import ...`.
Warnings
- breaking Version 2.0.0 introduced significant internal changes, replacing the Cython wrapper with a direct METH_O C extension for performance. While the primary Python API (`fnv1a_32`, `fnv1a_64`) appears stable for most users, this major version bump could indicate subtle behavioral changes or affect environments with specific build toolchains.
- gotcha The library explicitly requires Python 3.10 or newer. Installing on older Python versions will result in an error or an older, incompatible version being installed.
- gotcha FNV hash functions, like most cryptographic and hashing functions, expect a `bytes-like object` as input, not a standard Python string (`str`). Passing a string will raise a `TypeError`.
Install
-
pip install fnv-hash-fast
Imports
- fnv1a_32
from fnvhash import fnv1a_32
- fnv1a_64
from fnvhash import fnv1a_64
Quickstart
from fnvhash import fnv1a_32, fnv1a_64
data_bytes = b"hello world"
hash_32_bit = fnv1a_32(data_bytes)
hash_64_bit = fnv1a_64(data_bytes)
print(f"FNV-1a 32-bit hash of '{data_bytes.decode()}': {hash_32_bit}")
print(f"FNV-1a 64-bit hash of '{data_bytes.decode()}': {hash_64_bit}")