5

I was bit this week by a bug that occurred when my code was hosted in an x64 process. I am using a hash value for lookup and I am storing that hash value in a database. The hash value that has been generated in the past was a x86 hash and now that x64 hashes are being generated I am getting errors because the lookup values don't match anymore.

I am highly skeptical of this, but I thought I'd ask anyway. Is there a way to generate an x86 hash value if my code is running in an x64 process?

For reference, I am running on .NET 4.0 using C#.

Edit:

Here's the problem I've been running into: String.GetHashCode() returns different values

You can duplicate the problem by creating a console app with the following code:

"DDD.Events.Application.ApplicationReferenceCreated".GetHashCode()

Run the app with x86 platform, then run it with the x64 platform.

I just want to get consistent values across platforms. However, I may just create a pre-compiled list of hashes so I can fail over in the event I need to. I just wanted to know if there were a way to get consistent values from GetHashCode(). I don't think so, but if it is possible it would be the easiest solution in my case.

Community
  • 1
  • 1
Mark J Miller
  • 4,751
  • 5
  • 44
  • 74
  • 3
    What are x86 and x64 hashes? Hashes are generally created via an algorithm that is irrespective of platform and processor. – Joe Dec 17 '10 at 16:11
  • How are you generating the hash? Can we see the code? – kemiller2002 Dec 17 '10 at 16:16
  • Could you let us know why you need consistent values across systems? – PeteT Dec 17 '10 at 16:36
  • @Kevin every object in .NET has the GetHashCode method which can be overridden same as ToString. He is using the default implementation I think. – PeteT Dec 17 '10 at 16:39
  • @Petet, yeah that was my question, he hadn't added how he got the hash when I asked the question. I had originally thought he might be creating his own, and that there was some issue with it. – kemiller2002 Dec 17 '10 at 16:42
  • @PeteT, as I mentioned in the original post the hash value is being stored in the database for lookup purposes. We are serializing types using protobuf.net which doesn't store metadata about the type. So we prepend the data with the hash so we can identify the type for deserialization. The hash needs to be consistent so we can deserialize the type from storage. – Mark J Miller Dec 17 '10 at 17:19

1 Answers1

7

I'm afraid if you are using the default implementation in .NET then this is not possible. They do not even guarantee it will return the same id between different versions of .NET; and explicitly state it must not be used as a unique identifier. Have a read of this on MSDN

Bigtoe
  • 3,372
  • 1
  • 31
  • 47
  • I think this is right on I believe binary serialization also says it's not guaranteed to be the same between .NET versions just for anyone reading. – PeteT Dec 17 '10 at 16:35
  • Thanks, I just wanted confirmation. I have figured out a way to do this that will be compatible with types that were serialized using the GetHashCode() method. I'm going to create an attribute where I can specifiy the "Id" of a type and assign it to all types. The attribute will have an optional "LegacyId" which I can use to lookup types that were serialized using the old method. This way I can have consistent values for all types. – Mark J Miller Dec 17 '10 at 17:22