MurmurHash
MurmurHash is a fast, non-cryptographic hash function library, primarily used for tasks like hash table indexing, data deduplication, and probabilistic data structures. This Python library provides efficient Cython bindings for MurmurHash, offering various 32-bit and 128-bit hash variants. It is actively maintained with frequent updates to support new Python versions and architectures.
Warnings
- breaking Be aware that `murmurhash` (from `explosion`) is a distinct package from the widely used `mmh3` library for MurmurHash3. They have different import paths (`import murmurhash.mrmr` vs `import mmh3`) and potentially different API behaviors. Installing one and attempting to use the other's API will lead to `ImportError` or unexpected hash results.
- gotcha Starting from version 1.0.7, MurmurHash2 and MurmurHash3 implementations within this library were made endian-neutral. While this ensures consistent hash values across diverse system architectures (e.g., big-endian vs. little-endian), it may produce different hash outputs compared to older `murmurhash` versions or other MurmurHash implementations that are endian-sensitive.
- gotcha MurmurHash is a non-cryptographic hash function optimized for speed and good distribution, not for security. It should not be used for cryptographic purposes such as password hashing, digital signatures, or data integrity where collision resistance and unpredictability are critical. Always use a consistent `seed` value to ensure reproducible hash outputs, especially when relying on hashes for data partitioning, caching, or lookups across different systems or application runs.
Install
-
pip install murmurhash
Imports
- hash
import murmurhash.mrmr murmurhash.mrmr.hash('data') - hash64
import murmurhash.mrmr murmurhash.mrmr.hash64('data') - hash128
import murmurhash.mrmr murmurhash.mrmr.hash128('data')
Quickstart
import murmurhash.mrmr
# Hash a string (UTF-8 encoded by default)
text_hash = murmurhash.mrmr.hash('hello world', seed=42)
print(f"32-bit hash for 'hello world': {text_hash}")
# Hash bytes
bytes_hash = murmurhash.mrmr.hash(b'hello world', seed=42)
print(f"32-bit hash for b'hello world': {bytes_hash}")
# Get a 64-bit hash
hash_64 = murmurhash.mrmr.hash64('another example', seed=0)
print(f"64-bit hash: {hash_64}")
# Get a 128-bit hash (returns a tuple of two integers)
hash_128 = murmurhash.mrmr.hash128('long string data', seed=12345)
print(f"128-bit hash: {hash_128}")
# Example with environment variable for seed (not typical for hash, but for auth template)
# import os
# seed_from_env = int(os.environ.get('MURMURHASH_SEED', '0'))
# env_hash = murmurhash.mrmr.hash('secure_data', seed=seed_from_env)
# print(f"Hash with env seed: {env_hash}")