all groups > dotnet clr > july 2006 >
You're in the

dotnet clr

group:

Match GetHashCode between .NET 1.1 app and non-.NET app


Match GetHashCode between .NET 1.1 app and non-.NET app ALV
7/18/2006 7:36:01 PM
dotnet clr:
Within a native C++ app , I need to be able to generate a hash for a given
string that matches the hash that the same string returns from the .NET 1.1
String.GetHashCode().

The .NET framework may not be on the machine, so I can't rely on it in any
way.

Does anyone know what the .NET 1.1 GetHashCode algorithm is?
Re: Match GetHashCode between .NET 1.1 app and non-.NET app Michael Nemtsev
7/19/2006 12:00:00 AM
Hello ALV,

Use Reflector to get algorithm

Code below from the .net 2.0 (have no 1.1 nearby)
/// <summary>Returns the hash code for this string.</summary>
/// <returns>A 32-bit signed integer hash code.</returns>
/// <filterpriority>2</filterpriority>
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
public override unsafe int GetHashCode()
{
fixed (char* text1 = ((char*) this))
{
char* chPtr1 = text1;
int num1 = 0x15051505;
int num2 = num1;
int* numPtr1 = (int*) chPtr1;
for (int num3 = this.Length; num3 > 0; num3 -= 4)
{
num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
if (num3 <= 2)
{
break;
}
num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
numPtr1 += 2;
}
return (num1 + (num2 * 0x5d588b65));
}
}



A> GetHashCode algorithm is?
A>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Re: Match GetHashCode between .NET 1.1 app and non-.NET app Jesse Houwing
7/19/2006 12:00:00 AM
Do keep in mind that this algorithm can be overridden (and is
overridden) for a large number of classes in the library. There is no
such thing as *the* hashcode algo.

Jesse



[quoted text, click to view]
Re: Match GetHashCode between .NET 1.1 app and non-.NET app ALV
7/19/2006 10:36:02 AM
Thanks Michael.

I found that code in .NET 2.0 using Reflector, but when I try with .NET 1.1,
I only get the method signature.

IDLASM also doesn't have any code for 1.1...just says GetHashCode a managed
internalcall.

Any idea if its possible to get code or even IL for that?

[quoted text, click to view]
Re: Match GetHashCode between .NET 1.1 app and non-.NET app ALV
7/19/2006 10:57:02 AM
Thanks Jon...that explains why I get different numbers when generating hashes
outside of .NET. I thought the 2.0 algorithm had changed.

[quoted text, click to view]
Re: Match GetHashCode between .NET 1.1 app and non-.NET app Jon Skeet [C# MVP]
7/19/2006 6:24:53 PM
[quoted text, click to view]

Michael has replied with the algorithm, but it should be stressed that
you *should not* rely on a hashcode being valid outside the environment
it was originally generated in. Hashcodes as given by GetHashCode are
meant to be temporary indications of potential equality. They should
*not* be persisted (which is presumably what has happened here) unless
you have clear documentation saying that this is valid.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Re: Match GetHashCode between .NET 1.1 app and non-.NET app Michael Nemtsev
7/19/2006 6:25:52 PM
Hello ALV,

U hardly get them both the same, and there is no reason to look at .net realization.
Everything depends on you data. Hash algorithm should be tuned to your specific
data to get the most rage of data distribution
I'd recomend to read Donald E. Knuth books about it, smth like "golden cut
set of data distribution" and etc.

A> Thanks Jon...that explains why I get different numbers when
A> generating hashes outside of .NET. I thought the 2.0 algorithm had
A> changed.
A>
[quoted text, click to view]
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch

Re: Match GetHashCode between .NET 1.1 app and non-.NET app Jon Skeet [C# MVP]
7/19/2006 8:48:22 PM
[quoted text, click to view]

It *has* changed in .NET 2.0 - and it may well change again. Further,
it shouldn't break anything if the runtime decided to take a random
number when it started, and added that to the result of every call to
String.GetHashCode: the value is crucial *within* that run of an
application, but shouldn't be taken to be useful for different runs,
even with the same framework version.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
AddThis Social Bookmark Button