Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

call perl hash function?

by whartman (Novice)
on Jan 26, 2003 at 16:13 UTC ( [id://229982]=perlquestion: print w/replies, xml ) Need Help??

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

Perl uses an internal hash function to hash a string of any length into an integer in its implementaion of hash structures. How do I call this hash function directly, in standard portable perl, as a kind of message digest function? Alternatively, does anyone have the perl code to implement the function in perl? Note that I am not asking how to use perl hash structures, but rather how to accomplish the perl "hash" (in the original computer science meaning of the term) of a string into an integer. Thanks!

Replies are listed 'Best First'.
Re: call perl hash function?
by broquaint (Abbot) on Jan 26, 2003 at 16:30 UTC
    You can find perl's hashing function used in 5.005 in Abhijit Menon-Sen's article How Hashes Really Work.
    HTH

    _________
    broquaint

      If I recall correctly, the hash function has changed over time. So it may very well be the case that the hash value of "Somestring" is different in Perl 5.005, 5.6.1 and 5.8. Try the following with different Perl versions:

      $ perl -MDevel::Peek -e 'Dump({ key => "value" })'

      and look for the line with

      Elt "key" HASH =

      in it. The hash values are different.

      Arjen

        Good point; this might have tripped me up. Thanks!
      Reference gave me what I needed. Thanks!
Re: call perl hash function?
by dbp (Pilgrim) on Jan 26, 2003 at 20:28 UTC
    From the perlguts manual:

    The hash algorithm is defined in the "PERL_HASH(hash, key, klen)" macro:

    hash = 0; while (klen--) hash = (hash * 33) + *key++; hash = hash + (hash >> 5); /* after 5.6 */
    The last step was added in version 5.6 to improve distribution of lower bits in the resulting hash value.

    If you just want a look at the hashing function, this is it. A look at the full source might give you a better feel for it. If you want to call this from perl, you can use Inline::C which automatically includes perl.h. Alternatively, you can reimplement this function in pure perl and use it from there.

Re: call perl hash function?
by Hofmator (Curate) on Jan 26, 2003 at 17:26 UTC
    There are quite some perl modules out there for creating message digests, see Digest.

    -- Hofmator

      Thanks. Rounded out my understanding. Boy, you monks are good!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://229982]
Approved by gjb
Front-paged by JaWi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2025-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.