MurmurHash3 Class
Definition
Base class for the MurmurHash3 family of non-cryptographic hash algorithms by Austin Appleby. See the
SMHasher reference repository for the specification.
public abstract class MurmurHash3 : NonCryptographicHashAlgorithm, IDisposable
- Inheritance
-
MurmurHash3
- Implements
- Derived
- Inherited Members
- Extension Methods
-
NonCryptographicHashAlgorithmExtensions.ComputeHash(NonCryptographicHashAlgorithm, byte[], int, int)NonCryptographicHashAlgorithmExtensions.TryVerifyHash(NonCryptographicHashAlgorithm, byte[], byte[])NonCryptographicHashAlgorithmExtensions.TryVerifyHash(NonCryptographicHashAlgorithm, byte[], string)NonCryptographicHashAlgorithmExtensions.TryVerifyHash(NonCryptographicHashAlgorithm, Stream, byte[])
Remarks
The algorithm is block-streamable: input delivered through Append(ReadOnlySpan<byte>) is mixed into the running accumulators one block at a time, with only a partial trailing block buffered between calls. Memory use is constant regardless of input length, and reading the current hash applies the tail and finalization mix to a copy of the accumulators, so digest reads are non-destructive.
A 32-bit seed can be supplied at construction time to vary the output for the same input, which is useful for building distributed hash tables and bloom filters. The seed does not affect the algorithm's security posture.
Shared mixing primitives (FMix32(uint), FMix64(ulong)) and algorithm constants are defined here and are available to all derived variants. Supported output sizes are 32 and 128 bits.
When to choose MurmurHash3. MurmurHash3 has excellent avalanche, strong distribution under SMHasher's full battery, and is faster than the FNV family on inputs longer than a few dozen bytes - making it the default choice for non-distributed in-memory hash tables, bloom filters, and content-based sharding. Pick MurmurHash3_32 when 32 bits is sufficient and the host is 32-bit-friendly; pick MurmurHash3_128 when collision pressure (large key spaces, fingerprinting) calls for more bits. CityHash typically edges MurmurHash3 on long inputs on 64-bit CPUs; Fnv is preferable only for very small fixed-length keys.
Instances are not thread-safe; share behind explicit synchronization.
important
MurmurHash3 is not cryptographically secure. It must not be used for password hashing, digital signatures, or any application that requires collision resistance under adversarial conditions.
using Bodu.IO.Hashing;
using Bodu.IO.Hashing.Extensions;
// 32-bit hash, default seed, suitable for in-memory hash tables.
var m32 = new MurmurHash3_32();
uint h32 = BinaryPrimitives.ReadUInt32LittleEndian(m32.ComputeHash(keyBytes));
// 128-bit hash, custom seed for shard isolation across services.
var m128 = new MurmurHash3_128(seed: 0xC2B2AE35u);
byte[] fingerprint = m128.ComputeHash(payload);
Properties
Seed
Gets the 32-bit seed used to initialize the hash computation.
public uint Seed { get; }
Property Value
- uint
The seed value supplied at construction time, or zero if no seed was specified.
Methods
Append(ReadOnlySpan<byte>)
When overridden in a derived class, appends the contents of source to the data already processed for the current hash computation.
public override void Append(ReadOnlySpan<byte> source)
Parameters
sourceReadOnlySpan<byte>The data to process.
Dispose()
Releases all resources used by the current instance and clears its buffered input state.
public void Dispose()
Remarks
After disposal, subsequent calls to Append(ReadOnlySpan<byte>), Reset(), or GetCurrentHashCore(Span<byte>) throw ObjectDisposedException. Calling Dispose() multiple times is safe and has no effect after the first invocation.
Dispose(bool)
Releases the resources used by the current instance, optionally clearing managed state.
protected virtual void Dispose(bool disposing)
Parameters
disposingbooltrue when called from Dispose(); false when called from a finalizer. Managed resources are released only when
disposingis true.
Remarks
Override in a derived class to release additional resources owned by the subclass. Always invoke
base.Dispose(disposing) from the override so that the buffered state is cleared.
FMix32(uint)
Applies the MurmurHash3 32-bit finalization mix to thoroughly diffuse the bits of a 32-bit value.
protected static uint FMix32(uint h)
Parameters
huintThe 32-bit value to mix.
Returns
- uint
The finalized 32-bit value with strong avalanche properties.
FMix64(ulong)
Applies the MurmurHash3 64-bit finalization mix to thoroughly diffuse the bits of a 64-bit value.
protected static ulong FMix64(ulong k)
Parameters
kulongThe 64-bit value to mix.
Returns
- ulong
The finalized 64-bit value with strong avalanche properties.
GetCurrentHashCore(Span<byte>)
When overridden in a derived class, writes the computed hash value to destination without modifying accumulated state.
protected override void GetCurrentHashCore(Span<byte> destination)
Parameters
Reset()
When overridden in a derived class, resets the hash computation to the initial state.
public override void Reset()
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |