views:

115

answers:

3

I'm looking for a CPAN module that will take a short string:

my $hash_value = hash_this('short string not too long');

And hash it into an integer key:

say $hash_value;

12345671234    # an integer key
A: 

Digest::MD5 should work:

http://search.cpan.org/~gaas/Digest-MD5-2.38/MD5.pm

With the binary, you should be able to convert it using it:

Math::BaseCnv

NoahD
I'm currently using this but I would like the output in integer form: 2342345334
$md5->digest returns a binary then you just need to convert it to an int or is there a different problem?
NoahD
What is "0b" in oct "0b$bin"?
If EXPR starts off with "0b", it is interpreted as a binary string
I tried this and it comes back with 0.
use 5.10.0;use Digest::MD5 qw(md5);my $binary_key = md5("something"); say oct("0b$binary_key");# prints 0
I will try this out later tonight to confirm, but Math::BaseCnv should do it for you?
NoahD
A: 

If you need an hash that is only 32-bit or 64-bit long*, that is, if you need an hash of the type used in computer science terms such as "hash table" and NOT an hash in the cryptography meaning (which CAN'T be that short and strong at the same time) you can use CRC32 or one of its friends.

OTOH if you need a cryptographically strong hash function, I would avoid MD5 and use SHA-256 nowadays.

use String::CRC32;

$crc = crc32("some string");

*: I don't know how big is a perl integer value, so I might be wrong there

lapo
A: 

I wrote Algorithm::Nhash to solve this exact problem. It generates a cheap hash from a string and optionally does modulo arithmetic to throw the strings into buckets.

Peter Corlett