The basis of the FNV hash algorithm was taken from an idea sent as reviewer comments to the IEEE POSIX P1003.2 committee by Glenn Fowler and Phong Vo in 1991. In a subsequent ballot round, Landon Curt Noll improved on their algorithm. In an email message to Noll, they named it the Fowler/Noll/Vo or FNV hash.[1]
Overview
The current versions are FNV-1 and FNV-1a, which supply a means of creating non-zero FNV offset basis. FNV currently[as of?] comes in 32-, 64-, 128-, 256-, 512-, and 1024-bit variants. For pure FNV implementations, this is determined solely by the availability of FNV primes for the desired bit length; however, the FNV webpage discusses methods of adapting one of the above versions to a smaller length that may or may not be a power of two.[2][3]
One of FNV's key advantages is that it is very simple to implement.[8] Start with an initial hash value of FNV offset basis. For each byte in the input, multiplyhash by the FNV prime, then XOR it with the byte from the input. The alternate algorithm, FNV-1a, reverses the multiply and XOR steps.
algorithm fnv-1 ishash := FNV_offset_basisfor eachbyte_of_data to be hashed dohash := hash × FNV_primehash := hashXORbyte_of_datareturnhash
In the above pseudocode, all variables are unsignedintegers. All variables, except for byte_of_data, have the same number of bits as the FNV hash. The variable, byte_of_data, is an 8-bit unsigned integer.
The FNV-1a hash differs from the FNV-1 hash only by the order in which the multiply and XOR is performed:[9][11]
algorithm fnv-1a ishash := FNV_offset_basisfor eachbyte_of_data to be hashed dohash := hashXORbyte_of_datahash := hash × FNV_primereturnhash
The above pseudocode has the same assumptions that were noted for the FNV-1 pseudocode. The change in order leads to slightly better avalanche characteristics.[9][12]
FNV-0 hash (deprecated)
The FNV-0 hash differs from the FNV-1 hash only by the initialisation value of the hash variable:[9][13]
algorithm fnv-0 ishash := 0
for eachbyte_of_data to be hashed dohash := hash × FNV_primehash := hashXORbyte_of_datareturnhash
The above pseudocode has the same assumptions that were noted for the FNV-1 pseudocode.
A consequence of the initialisation of the hash to 0 is that empty messages and all messages consisting of only the byte 0, regardless of their length, hash to 0.[13]
Use of the FNV-0 hash is deprecated except for the computing of the FNV offset basis for use as the FNV-1 and FNV-1a hash parameters.[9][13]
FNV offset basis
There are several different FNV offset bases for various bit lengths. These offset bases are computed by computing the FNV-0 from the following 32 octets when expressed in ASCII:
For a given integer s such that 4 < s < 11, let n = 2s and t = ⌊(5 + n) / 12⌋; then the n-bit FNV prime is the smallest prime numberp that is of the form
such that:
0 < b < 28,
the number of one-bits in the binary representation of b is either 4 or 5, and
p mod (240 − 224 − 1) > 224 + 28 + 7.
Experimentally, FNV primes matching the above constraints tend to have better dispersion properties. They improve the polynomial feedback characteristic when an FNV prime multiplies an intermediate hash value. As such, the hash values produced are more scattered throughout the n-bit hash space.[4][14]
FNV hash parameters
The above FNV prime constraints and the definition of the FNV offset basis yield the following table of FNV hash parameters:
Speed of computation – As a hash designed primarily for hashtable and checksum use, FNV-1 and FNV-1a were designed to be fast to compute. However, this same speed makes finding specific hash values (collisions) by brute force faster.
Sticky state – Being an iterative hash based primarily on multiplication and XOR, the algorithm is sensitive to the number zero. Specifically, if the hash value were to become zero at any point during calculation, and the next byte hashed were also all zeroes, then the hash would not change. This makes colliding messages trivial to create given a message that results in a hash value of zero at some point in its calculation. Additional operations, such as the addition of a third constant prime on each step, can mitigate this but may have detrimental effects on avalanche effect or random distribution of hash values.
Diffusion – The ideal secure hash function is one in which each byte of input has an equally-complex effect on every bit of the hash. In the FNV hash, the ones place (the rightmost bit) is always the XOR of the rightmost bit of every input byte. This can be mitigated by XOR-folding (computing a hash twice the desired length, and then XORing the bits in the "upper half" with the bits in the "lower half").[4]