3. Other Hash Sizes and XOR Folding
Cette section conserve le texte RFC relatif a FNV, y compris FNV primes, offset_basis, endianism, XOR folding, constants, non-cryptographic security guidance, source code, test code, Makefile et les comparaisons avec SHA-1 and SHA-256.
Texte RFC original
3. Other Hash Sizes and XOR Folding
Many hash uses require a hash that is not one of the FNV sizes for
which constants are provided in Section 5. If a larger hash size is
needed, please contact the authors of this document.
For scenarios where a fixed-size binary field of k bits is desired
with k < 1024 but not among the constants provided in Section 5, the
recommended approach involves using the smallest FNV hash of size S
where S > k and employing XOR folding, as shown below. The final
bit-masking operation is logically unnecessary if the size of the
variable k-bit-hash is exactly k bits.
temp = FNV_S ( data-to-be-hashed )
k-bit-hash = ( temp XOR temp>>k ) bitwise-and ( 2**k - 1 )
A somewhat stronger hash may be obtained for exact FNV sizes by
calculating an FNV twice as long as the desired output ( S = 2*k )
and performing such XOR data folding using a k equal to the size of
the desired output. However, if a much stronger hash is desired,
cryptographic algorithms, such as those specified in [FIPS202] or
[RFC6234], should be used.
If it is desired to obtain a hash result that is a value between 0
and max, where max+1 is not a power of 2, simply choose an FNV hash
size S such that 2^S > max. Then, calculate the following:
FNV_S mod ( max+1 )
The resulting remainder will be in the range desired but will suffer
from a bias against large values, with the bias being larger if 2^S
is only slightly larger than max. If this bias is acceptable, no
further processing is needed. If this bias is unacceptable, it can
be avoided by retrying for certain high values of hash, as follows,
before applying the mod operation above:
X = ( int( ( 2**S - 1 ) / ( max+1 ) ) ) * ( max+1 )
while ( hash >= X )
hash = ( hash * FNV_Prime ) + offset_basis