Fnv Class
Definition
Provides a base class for the Fowler-Noll-Vo (FNV) hash family, supporting both the FNV-1 and FNV-1a variants at 32-bit and 64-bit widths.
public abstract class Fnv : NonCryptographicHashAlgorithm, IResumableHashAlgorithm
- Inheritance
-
Fnv
- 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
FNV maintains a running hash initialized from an offset basis and processes each input byte by combining
multiplication with a large FNV prime and a bitwise XOR. Derived types select the width and variant:
- FNV-1multiplication followed by XOR.
- FNV-1aXOR followed by multiplication.
When to choose FNV. FNV is byte-at-a-time, allocation-free, and trivially fast on small inputs - a common choice for hashing identifiers, dictionary keys, and cache lookups in hot paths. For most new code prefer Fnv1a32 or Fnv1a64: the FNV-1a ordering has measurably better avalanche than the original FNV-1. For inputs longer than a few hundred bytes, MurmurHash3 or CityHash generally distribute better and are faster on modern CPUs; FNV's strength is its simplicity and predictable performance on short keys.
Output size and lifecycle. The digest length is fixed by the constructor's hashSize argument
(32 or 64 bits) and emitted in big-endian byte order.
GetCurrentHash() is non-destructive and may be called
any number of times during a running hash; Reset()
returns the running state to its initial offset basis.
Thread safety. Instances are not thread-safe; share behind explicit synchronization, or allocate one per consumer.
important
This algorithm is not cryptographically secure and should not be used for password hashing, digital signatures, or integrity validation in security-sensitive applications.
using Bodu.IO.Hashing;
using Bodu.IO.Hashing.Extensions;
// Pick a width and variant; FNV-1a is the recommended default.
var fnv = new Fnv1a64();
byte[] digest = fnv.ComputeHash(System.Text.Encoding.UTF8.GetBytes("user@example.com"));
Constructors
Fnv(int, ulong, ulong, bool)
Initializes a new instance of the Fnv class using the specified configuration parameters.
protected Fnv(int hashSize, ulong prime, ulong offsetBasis, bool useFnv1a)
Parameters
hashSizeintThe size, in bits, of the resulting hash value. Supported values are 32 and 64.
primeulongThe FNV prime multiplier used during hash computation.
offsetBasisulongThe initial offset basis used to seed the running hash.
useFnv1abooltrue to use the FNV-1a variant (XOR followed by multiplication); otherwise false to use the FNV-1 variant (multiplication followed by XOR).
Exceptions
- ArgumentException
hashSizeis not a supported value (32 or 64).
Properties
AlgorithmName
Gets the algorithm name in the form FNV-{variant}-{bits}, e.g. FNV-1-32 or FNV-1a-64.
public string AlgorithmName { get; }
Property Value
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.
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()
TryComputeHashFrom(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, out int)
Resumes a hash computation from a previously finalized hash value, processes additional input, and writes the new finalized hash to the specified destination span.
public bool TryComputeHashFrom(ReadOnlySpan<byte> previousHash, ReadOnlySpan<byte> newData, Span<byte> destination, out int bytesWritten)
Parameters
previousHashReadOnlySpan<byte>The previously finalized hash value to resume from.
newDataReadOnlySpan<byte>The additional input data to include in the resumed hash calculation.
destinationSpan<byte>The destination buffer to write the finalized hash value to.
bytesWrittenintOutputs the number of bytes written to the destination buffer.
Returns
- bool
true if the resumed and finalized hash was written successfully; otherwise, false if the destination span was too small.
Remarks
FNV applies no finalization - the digest is the big-endian running accumulator - so resuming requires no reversal: the digest seeds the accumulator directly. The computation runs against saved-and-restored instance state, so any in-progress incremental state on the instance survives the call unchanged.
Exceptions
- ArgumentException
Thrown if the
previousHashlength does not match HashLengthInBytes.
Explicit Interface Implementations
IResumableHashAlgorithm.get_HashLengthInBytes()
int IResumableHashAlgorithm.get_HashLengthInBytes()
Returns
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |