PHP 8.1: MurmurHash3 hash algorithm support
PHP 8.1 adds support for MurmurHash hashing algorithm.
MurmurHash is a non-cryptographic hashing algorithm, and PHP 8.1 supports the third version of MurmurHash,
MurmurHash3. PHP 8.1 supports the following variants:
murmur3a: 32-bit hash
murmur3c: 128-bit hash on x86 architecture
murmur3f: 128-bit hash on x64 architecture
hash('murmur3a', 'php.watch'); // "ac96fab7" hash('murmur3c', 'php.watch'); // "6d0fe9c3f960dc75cf42632f3e78ffda" hash('murmur3f', 'php.watch'); // "ba0189037daec822d973d304602d44f0"
MurmurHash is a streaming hash, which means the values can be updated in sequence without having to hash the input string as a whole.
$context = hash_init('murmur3f'); hash_update($context, 'php'); hash_update($context, '.'); hash_update($context, 'watch'); $hash = hash_final($context); // "ba0189037daec822d973d304602d44f0"
MurmurHash is not designed as a cryptographic hashing algorithm. For password hashing, use
password_hashand its friends. Furthermore, none of the MurmurHash variants are allowed in
MurmurHash algorithm is faster than most of the hashing algorithms. On the source page of MurmurHash, it mentions a benchmark on several other hashing algorithms.
Note that the PHP's implementation of MurmurHash can be different in performance. For a benchmark on PHP hash algorithms, see PHP Hash Algorithm Benchmarks.
Backwards Compatibility Impact
MurmurHash is newly added to PHP 8.1, and due to current lack of a hash registry that supports adding custom hashing algorithms, producing MurmurHash hashes using the
hash() function is not possible on older PHP versions.
On PHP 8.0, a ValueError exception will be thrown is attempted to
Fatal error: Uncaught ValueError: hash(): Argument #1 ($algo) must be a valid hashing algorithm in ...:...
On PHP versions prior to 8.0, a warning will be raised:
Warning: hash(): Unknown hashing algorithm: murmur3a in ... on line ...
- rryqszq4/php-murmurhash - A PHP extension that adds MurmurHash support.
- lastguest/murmurhash-php - A userland-PHP implementation of MurmurHash.