13

What is Ruby's hash function algorithm?

3
  • 4
    Ruby 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? Commented Jul 17, 2010 at 8:33
  • 5
    Hash function of what? Strings? Numbers? Objects? Commented Jul 17, 2010 at 8:38
  • I think he means something like this ruby-doc.org/core-1.9.3/Bignum.html#method-i-hash Commented Sep 9, 2012 at 16:33

1 Answer 1

17

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).

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.