brandondahler / Data.HashFunction

C# library to create a common interface to non-cryptographic hash functions.
MIT License
255 stars 42 forks source link

How to get hash code #19

Closed nehmebilal closed 9 years ago

nehmebilal commented 9 years ago

Hi,

I am trying to use your library to get the hash code of a given string. I'd like to use the hash code for load balancing and I'm looking for a consistent hash code (for a given string, I always get the same hash code).

Here is what it looks like:

int nodeIndex = GetHashCode(sessionId) % nodeCount;
Nodes[nodeIndex].DoWork();

Does this library provide such functionality?

kentkost commented 9 years ago

You will always get the same value for the same string your're trying to hash. But the GetHashCode() will change everytime. Also there is no method of GetHashCode that takes 1 argument.

xxhash is a Non-cryptographic hash method.

xxHash returns a byte array that you will have to convert.

xxHash h = new xxHash();
int nodeIndex = BitConverter.ToInt32(h.ComputeHash(sessionId)) % nodeCount;
Nodes[nodeIndex].Dowork()

This will always give you the same nodeIndex as long as your nodeCount doesn't change.

nehmebilal commented 9 years ago

Perfect thanks!