Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the .NET string hash function portable? [duplicate]

Possible Duplicate:
How do I create a HashCode in .net (c#) for a string that is safe to store in a database?

I use C# 4.0 and gets the string hash by invoking:

"my string".GetHashCode()

Code generated by this call is stored into database to future use. This hash code is used to find some subset of strings and then to equal comparison.

Questions are:

  1. Is it a standardized hash calculation? May I assume that it is possible to calculate the same hash in different environments like C# in .Net 3.0 or future .Net editions?
  2. Is it possible to calculate the same hash function on yourself by writing it in Java, PL/SQL, Ruby, etc?
  3. Can I assume that hash generated today will be the same tomorrow in the same environment? For example when I shutdown my computer and run the program again, or change locale, or some other settings?
  4. What are the limits of portability?
  5. I know I can do it yourself, but maybe some kind of portability is provided?
like image 685
Max Avatar asked Nov 06 '11 18:11

Max


People also ask

Is hashCode unique in C#?

NO! A hash code is not an id, and it doesn't return a unique value. This is kind of obvious, when you think about it: GetHashCode returns an Int32 , which has “only” about 4.2 billion possible values, and there's potentially an infinity of different objects, so some of them are bound to have the same hash code.

Is string GetHashCode unique?

If two string objects are equal, the GetHashCode method returns identical values. However, there is not a unique hash code value for each unique string value. Different strings can return the same hash code. The hash code itself is not guaranteed to be stable.

Is string GetHashCode deterministic?

The key point is that the hash codes are deterministic for a given program execution, that means the only time it'll be an issue is if you're saving the hash code outside of a process, and loading it into another one.

How does GetHashCode work in C#?

GetHashCode method of the base class, which computes a hash code based on an object's reference; for more information, see RuntimeHelpers. GetHashCode. In other words, two objects for which the ReferenceEquals method returns true have identical hash codes. If value types do not override GetHashCode, the ValueType.


1 Answers

From MSDN:

The default implementation of the GetHashCode method does not guarantee unique return values for different objects. Furthermore, the .NET Framework does not guarantee the default implementation of the GetHashCode method, and the value it returns will be the same between different versions of the .NET Framework. Consequently, the default implementation of this method must not be used as a unique object identifier for hashing purposes.

So no, you cannot assume that the value produced by GetHashCode is stable. This isn't just theoretical, either - we've seen the value change in the past.

If you want a stable hash, you'll have to generate it yourself.

like image 127
Michael Petrotta Avatar answered Nov 03 '22 10:11

Michael Petrotta