Murmurhash2
Murmurhash2 is a Python library providing fast, non-cryptographic hash functions, specifically bindings for the MurmurHash2 and MurmurHash3 algorithms. It is designed for high performance in applications such as hash tables, caching, and data distribution, rather than for security-sensitive use cases. The current version, 0.2.10, was last released in February 2022 and supports Python 3.6+ environments. The project appears to be in a maintenance status with infrequent updates.
Warnings
- breaking MurmurHash2 and MurmurHash3 are non-cryptographic hash functions. They are explicitly vulnerable to collision attacks (HashDoS) and must NOT be used for security-sensitive applications such as password hashing, digital signatures, or any context where an attacker can control input data.
- gotcha The hash functions expect byte input. Passing a standard Python string (`str`) directly will result in a `TypeError` or unexpected behavior if the library attempts implicit conversion, which may not be platform-consistent.
- gotcha Due to its low-level C implementation, MurmurHash2 can be sensitive to endianness and data alignment across different platforms or when byte ordering is not strictly controlled, potentially leading to inconsistent hash values for the same logical input if the byte representation varies.
- deprecated MurmurHash2 is an older algorithm. MurmurHash3, which is also provided by this library, is the more current version and often recommended over MurmurHash2 for improved distribution and performance characteristics.
Install
-
pip install murmurhash2
Imports
- murmurhash2
from murmurhash2 import murmurhash2
- murmurhash3
from murmurhash2 import murmurhash3
Quickstart
from murmurhash2 import murmurhash2, murmurhash3
SEED = 3242157231 # A common seed value, can be any integer
data_to_hash = b'Hello, MurmurHash!' # Input must be bytes
hash_value_2 = murmurhash2(data_to_hash, SEED)
hash_value_3 = murmurhash3(data_to_hash, SEED)
print(f"MurmurHash2 of '{data_to_hash.decode()}' with seed {SEED}: {hash_value_2}")
print(f"MurmurHash3 of '{data_to_hash.decode()}' with seed {SEED}: {hash_value_3}")