What is Ruby's hash function algorithm?
-
4Ruby doesn't even have an official spec, so I doubt that there's a properly correct answer. In any case, why do you need to know this?polygenelubricants– polygenelubricants2010-07-17 08:33:01 +00:00Commented Jul 17, 2010 at 8:33
-
5Hash function of what? Strings? Numbers? Objects?kennytm– kennytm2010-07-17 08:38:04 +00:00Commented Jul 17, 2010 at 8:38
-
I think he means something like this ruby-doc.org/core-1.9.3/Bignum.html#method-i-hashJames– James2012-09-09 16:33:38 +00:00Commented Sep 9, 2012 at 16:33
Add a comment
|
1 Answer
The standard Ruby implementation uses the Murmur hash for some types (integer, string)
From string.c:1901:
/* MurmurHash described in http://murmurhash.googlepages.com/ */
static unsigned int
hash(const unsigned char * data, int len, unsigned int h)
(note that this function seems to be renamed to st_hash in the SVN trunk)
Search for rb_memhash in the source code if you want to know where it gets used. I have used the Murmur2 hash in an own project before, it is very fast and has good cryptographic properties (but not good enough to be used as cryptographic hash function).
1 Comment
Steven Sudit
Murmur2 has excellent distribution, but while that's necessary for cryptographic hashing, it's not sufficient. In other words, the hash is not resistant to intentional tampering.