PHP 8.1: MurmurHash3 hash algorithm support

TypeNew Feature

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', ''); // "ac96fab7"
hash('murmur3c', ''); // "6d0fe9c3f960dc75cf42632f3e78ffda"
hash('murmur3f', ''); // "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_hash and its friends. Furthermore, none of the MurmurHash variants are allowed in hash_hmac function.

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.

Speed (MiB/sec)
murmur3a 2413.88
murmur3c 3197.63
murmur3f 5226.40
sha2-256 147.80
sha1-160 364.95
md5-128 353.76

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 hash('murmur...', '...'):

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 ...

Alternatives include: