FNV Hash Implementation
fnvhash is a pure Python implementation of the FNV (Fowler-Noll-Vo) hash function family, including FNV-1 and FNV-1a variants. It is known for its simplicity and speed in non-cryptographic hashing applications. The library is actively maintained, with its current version 0.2.1, and focuses on correctness with 100% test coverage.
Warnings
- gotcha FNV hashes are NOT cryptographically secure. They are designed for speed and distribution (e.g., hash tables, checksums), not for security-sensitive applications where resistance to malicious attacks or collision engineering is required. Use cryptographic hash functions like SHA-256 for security needs.
- gotcha The FNV hash functions (`fnv1a_32`, `fnv1_64`, etc.) expect `bytes` objects as input, not Python `str` objects. Passing a string directly will result in a TypeError.
- gotcha This `fnvhash` library is a pure Python implementation. While portable, it may be significantly slower than C-extension based FNV hash libraries (e.g., `pyhash`, `fnvhash-c`) for performance-critical applications or large data sets.
- breaking The library requires Python 3.9 or newer. Attempts to install or run on older Python versions (e.g., Python 3.8 or 2.x) will fail.
Install
-
pip install fnvhash
Imports
- fnv1a_32
from fnvhash import fnv1a_32
- fnv1_64
from fnvhash import fnv1_64
Quickstart
from fnvhash import fnv1a_32, fnv1_64
data_str = "hello world"
data_bytes = data_str.encode('utf-8') # FNV hash functions expect bytes
# Calculate 32-bit FNV-1a hash
hash_32 = fnv1a_32(data_bytes)
print(f"FNV-1a 32-bit hash of '{data_str}': {hex(hash_32)}")
# Calculate 64-bit FNV-1 hash
long_data_str = "this is a slightly longer string for 64-bit hashing"
long_data_bytes = long_data_str.encode('utf-8')
hash_64 = fnv1_64(long_data_bytes)
print(f"FNV-1 64-bit hash of '{long_data_str}': {hex(hash_64)}")