Digest::FNV::XS - Fowler/Noll/Vo (FNV) hashes
use Digest::FNV::XS; # nothing exported by default
This module is more or less a faster version of Digest::FNV, that additionally supports binary data, incremental hashing, more FNV variants and more. The API isn't compatible (and neither are the generated hash values. The hash values computed by this module match the official FNV hash values as documented on http://www.isthe.com/chongo/tech/comp/fnv/).
Compute the 32 or 64 bit FNV-1a hash of the given string.
$init is the optional initialisation value, allowing incremental hashing. If missing or undef then the appropriate FNV constant is used.
$init
undef
The 64 bit variant is only available when perl was compiled with 64 bit support.
The FNV-1a algorithm is the preferred variant, as it has slightly higher quality and speed then FNV-1.
Compute the 32 or 64 bit FNV-1 hash of the given string.
The FNV-1a variant is preferable if you can choose.
The obsolete FNV-0 algorithm. Same as calling the FNV1 variant with $init = 0.
$init = 0
XOR-folds the 32 (64) bit FNV hash to $bits bits, which can be any value between 1 and 32 (64) inclusive.
$bits
XOR-folding is a good method to reduce the FNV hash to a power of two range.
These two functions can be used to reduce a 32 (64) but FNV hash to an integer in the range 0 .. $range, using the retry method, which distributes any bias more evenly.
$range
You can hash data incrementally by feeding the previous hahs value as $init argument for the next call, for example:
$hash = fnv1a_32 $data1; $hash = fnv1a_32 $data2, $hash; # and so on
Or in a loop (relying on the fact that $hash is undef initially):
$hash
my $hash; $hash = fnv1a_32 $_, $hash for ...;
A common problem is to reduce the 32 (64) bit FNV hash value to a smaller range, 0 .. $range.
The easiest method to do that, is to mask (For power of two) or modulo (for other values) the hash value, i.e.:
$inrage = $hash & ($range - 1) # for $range values that are power of two $inrage = $hash % $range # for any range
This is called the lazy mod mapping method, which creates small biases that rarely cause any problems in practise.
Nevertheless, you can improve the distribution of the bias by using XOR folding, for power of two ranges (and 32 bit hashews, there is also forfold_64)
forfold_64
$inrage = Digest::FNV::XS::xorfold_32 $hash, $log2_of_range
And, using the retry method, for generic ranges (and 32 bit hashes, there is also reduce_64):
reduce_64
$inrange = Digest::FNX::XS::reduce_32 $hash, $range
Marc Lehmann <schmorp@schmorp.de> http://software.schmorp.de/pkg/Digest-FNV-XS.html
To install Digest::FNV::XS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Digest::FNV::XS
CPAN shell
perl -MCPAN -e shell install Digest::FNV::XS
For more information on module installation, please visit the detailed CPAN module installation guide.