I want to generate an integer hashcode for strings, that will stay constant forever; i.e. the same string should always result in the same hashcode.
The hash does not have to be cryptographically secure, it will not be used for passwords or sensitive data.
My first attempt was to use the .net framework string.GetHashCode() function. However upon reading the sources I found the following commment:
// We want to ensure we can change our hash function daily. // This is perfectly fine as long as you don't persist the // value from GetHashCode to disk or count on String A // hashing before string B. Those are bugs in your code. hash1 ^= ThisAssembly.DailyBuildNumber;
This seems to indicate that the hashcode will not remain constant.
If so, does the framework have another method to generate repeatable hashcodes? Or would the code from GetHashCode be a reasonable starting point to implement my own?
I am looking for something as lightweight and fast as possible.
I found System.Security.Cryptography.MD5, but that seems overkill for a simple int32 hashcode, and I am worried about the overhead. At the very least it would require conversion from string to byte array, and from byte array to int, and either creation of a new MD5()
object for each hash, or management of some static shared MD5 object().