Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ALV - 19 Jul 2006 03:36 GMT
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?
Michael Nemtsev - 19 Jul 2006 06:15 GMT
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
Jesse Houwing - 19 Jul 2006 08:08 GMT
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

> Use Reflector to get algorithm
>
[quoted text clipped - 26 lines]
>      }
> }
ALV - 19 Jul 2006 18:36 GMT
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?

> Hello ALV,
>
[quoted text clipped - 35 lines]
> "At times one remains faithful to a cause only because its opponents do not
> cease to be insipid." (c) Friedrich Nietzsche
Jon Skeet [C# MVP] - 19 Jul 2006 18:24 GMT
> 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().

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

ALV - 19 Jul 2006 18:57 GMT
Thanks Jon...that explains why I get different numbers when generating hashes
outside of .NET. I thought the 2.0 algorithm had changed.

> > 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
[quoted text clipped - 6 lines]
> *not* be persisted (which is presumably what has happened here) unless
> you have clear documentation saying that this is valid.
Michael Nemtsev - 19 Jul 2006 19:25 GMT
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>
A> "Jon Skeet [C# MVP]" wrote:
A>

>>> 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
[quoted text clipped - 12 lines]
>> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
>> If replying to the group, please do not mail me too
---
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
Jon Skeet [C# MVP] - 19 Jul 2006 20:48 GMT
> Thanks Jon...that explains why I get different numbers when generating hashes
> outside of .NET. I thought the 2.0 algorithm had changed.

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.