Algorithm-Nhash

 view release on metacpan or  search on metacpan

lib/Algorithm/Nhash.pm  view on Meta::CPAN

=head1 VERSION

version 0.002

=head1 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}'

=head1 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

lib/Algorithm/Nhash.pm  view on Meta::CPAN


 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.

=head2 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
C<"6/33">. In list context, it returns a list of values like C<(6, 33)>.

=head1 SEE ALSO

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

t/nhash.t  view on Meta::CPAN

#!/usr/bin/env perl
use warnings;
use strict;

# Test "sane" usage

use Test::More tests => 6;

use Algorithm::Nhash qw( nhash );

is(nhash('supercalifragilisticexpialidocious'), 228769,
   'no-value nhash');

is(nhash('supercalifragilisticexpialidocious', 512), 417,
   'one-value nhash');

is(nhash('supercalifragilisticexpialidocious', 8, 64), '6/33',
   'scalar two-value nhash');

is_deeply(
    [nhash('supercalifragilisticexpialidocious', 8, 64)],
    [6, 33],
    'list two-value nhash');

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

is($nhash->nhash('supercalifragilisticexpialidocious'), '6/33',
   'scalar two-value OO nhash');

is_deeply(
    [$nhash->nhash('supercalifragilisticexpialidocious')],
    [6, 33],
    'list two-value OO nhash');



( run in 0.975 second using v1.01-cache-2.11-cpan-b16cb0d3907 )