The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Algorithm::Nhash - Exim nhash algorithm

VERSION

version 0.002

SYNOPSIS

Procedural usage:

 use Algorithm::Nhash qw( nhash );
 # prints 228769
 print nhash('supercalifragilisticexpialidocious');
 # prints 417 (which is 228769 % 512)
 print nhash('supercalifragilisticexpialidocious', 512);
 # prints '6/33' (6*64 + 3 == 417)
 print nhash('supercalifragilisticexpialidocious', 8, 64);
 # assigns (6, 33) to @nhash
 my @nhash = nhash('supercalifragilisticexpialidocious', 8, 64);

OO usage:

 use Algorithm::Nhash;
 my $nhash = new Algorithm::Nhash 8, 64;
 # prints '6/33'
 print $nhash->nhash('supercalifragilisticexpialidocious');

And how Exim does it:

 # prints '6/33'
 exim4 -be '${nhash_8_64:supercalifragilisticexpialidocious}'
 # prints '417' (which is 6*64+33)
 exim4 -be '${nhash_512:supercalifragilisticexpialidocious}'

DESCRIPTION

This is an implementation of the Exim nhash algorithm. It also supports an arbitrary number of divisors and not just the one or two that Exim permits.

The nash algorithm is a fast and simple hashing algorithm that attempts to evenly-distribute values but does not attempt to avoid collisions. Thus, it should not be used in place of a cryptographically-secure algorithm such as Digest::SHA. It is mainly intended for hashing filenames into directories to avoid placing too many files into a single directory.

If nhash is not given any divisors, then the hash result is returned as-is. If one divisor is given, the hash result is given modulo that divisor. If more than one divisor is given, the hash result is successively divided and the modulo at each stage returned.

Since the result is typically a 20-30 bit number, the product of all the divisors shouldn't be more than about 2**20 or the returned values will not be evenly-distributed.

FUNCTIONS AND METHODS

new

 use Algorithm::Nhash;
 my $nhash = new Algorithm::Nhash 8, 64;

This creates a new Algorithm::Nhash object that squirrels away the divisors for later use.

nhash

 # OO invocation
 print $nhash->nhash('supercalifragilisticexpialidocious');

 # procedural invocation
 print nhash('supercalifragilisticexpialidocious', 8, 64);

This calculates the nhash of the given string. In scalar context, it returns the nhash values as a string with slashes separating the components, like "6/33". In list context, it returns a list of values like (6, 33).

SEE ALSO

http://www.exim.org/exim-html-current/doc/html/spec_html/ch11.html (search for nhash.)

AUTHOR

Peter Corlett <abuse@cabal.org.uk>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Peter Corlett.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.