Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CPAN module that digests a short string into a short number?

I need to create unique numerical ids for some short strings.

some.domain.com    -> 32423421
another.domain.com -> 23332423
yet.another.com    -> 12131232

Is there a Perl CPAN module that will do something like this?

I've tried using Digest::MD5 but the resulting numbers are too long:

some.domain.com    -> 296800572457176150356613937260800159845 
like image 864
git-noob Avatar asked Nov 28 '22 12:11

git-noob


2 Answers

Just take the first 8 digits of the MD5 hash. This works because MD5 is uniformly distributed over its hash address space. This means that any consecutive sequence of MD5 hash digits will itself be a uniformly distributed hash.

Alternatively, just use some other uniformly-distributed hashing mechanism that returns 8 numbers. Whatever's easiest for you.

like image 64
John Feminella Avatar answered Dec 05 '22 19:12

John Feminella


Either Digest::CRC or String::CRC32. The first gives you option to calculate 8-, 16- and 32-bit chcecksums, while second only supports 32-bit.

like image 42
vartec Avatar answered Dec 05 '22 20:12

vartec