GetHashCode of string return different values on different OS

Hi,

I have a vb.net 3.5 desktop application. This uses couple of .net dll which are also written in vb.net 3.5. These dll have common funtions. Now I wanted that these dlls should not be used by other people where this desktop app is installed. So whenever I create instance of this dll, I pass a hard-coded string as "MyApp_Protected_2000". This string is passed from my win form to the constructor of the dll. The constructor of the dll uses .GetHashCode to get the hashcode of this string. I compare this hashcode return value which I have pre-generated in development environment (XP with 32-bit OS) and compare it like this:

========= Class Code ===================
If strValidate.Trim.GetHashCode <> -5999234 then      
      Throw new exception "Unauthorized User"
Endif
========================================

This way I ensured that noone can use this dll.

But now I find that if this app is used on Windows 7 (64-bit), the GetHashCode returns different value other than -5999234  for the same string "MyApp_Protected_2000".

a. Why is this so? And how can I resolve this issue? What can I do to ensure that the string returns same value no matter what CLR version and OS versiion?

b. Or should I find the encrypted value of "MyApp_Protected_2000" in design-time and compare with that? The only problem I see here is that the encrypted value would have some special chars and I am afraid that might break the app. Also now I have doubt that even the encryption function may return different values on 32-bit and 64-bit OS.


Please help.

Regards,
MI



vj_miAsked:
Who is Participating?
 
WayneATaylorConnect With a Mentor Commented:
Hashcode do vary by versions, as is explained in the MS article.

http://msdn.microsoft.com/en-us/library/system.string.gethashcode.aspx

I guess the only way round this would be to write your own code for creating the hashes.

Wayne
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.