http://www.perlmonks.org?node_id=11151716

cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks. I'm trying to find a module that will create a hash of a string. By that I mean a reduced-length unique string that uniquely represents the original. When I search for that, all I get is references to hash as in the data structure.
  • Comment on How to hash a string--not data structure

Replies are listed 'Best First'.
Re: How to hash a string--not data structure
by haukex (Archbishop) on Apr 17, 2023 at 19:36 UTC

    Careful - there is no such thing as "a reduced-length unique string that uniquely represents the original". Unless you're talking about lossless compression, when you reduce the length of the string (i.e. "hash it"), you lose information, and there is always a small but real chance of collisions. So replace the two instances of "unique" in your sentence with "almost unique", and then your question has an answer.

    It sounds to me like you are looking for Digest and one of the many algorithms such as MD5, SHA-1, SHA-256, SHA-512, etc. - Digest::MD5 and Digest::SHA are in the core. If you want a shorter checksum with a larger chance of collisions, there are also algorithms like CRC32 with corresponding modules on the CPAN.

Re: How to hash a string--not data structure
by 1nickt (Canon) on Apr 17, 2023 at 19:33 UTC

    Hi,

    See for example Digest::SHA.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: How to hash a string--not data structure
by Corion (Patriarch) on Apr 17, 2023 at 19:33 UTC

    Likely you want the Digest module :)

Re: How to hash a string--not data structure
by philipbailey (Curate) on Apr 17, 2023 at 19:33 UTC
Re: How to hash a string--not data structure
by cormanaz (Deacon) on Apr 17, 2023 at 20:42 UTC
    Thanks everyone. Just what I was looking for. I note haukex's caution.