Thursday, September 5, 2013

How to: Generate the same hash value in Sql Server, C# and Java.

Since SQL Server 2005, there is one built-in function called hashbytes

image

please by aware if you define the string type as char and varchar , the hash varies.

so , here is one example

image

to be safe, using Rtrim on nchar type to get the same result vs the varchar one
image

C# and Sql, If you want to get the same hash for a given string, be sure to use the same encoding logic for the string, like Ascii vs Unicode.
image

in Java, try the commons-codec,
image

if you use nvarchar or nchar, make sure use unicde in c# also.
image

Sql server unicode

image

something in Java,switched it to unicdoe, follow the same byte orders
image

No comments:

 
Locations of visitors to this page