Skip to content Skip to sidebar Skip to footer

Comparing A C# Generated Checksum With A Sql Server One

I want to send a lot of data from c# to my database, together with a calculated checksum as the last value, which should be compared to the one that the SQL Server's stored procedu

Solution 1:

Comments considered, here is how you can do it (/ is used as a guard char, only one is needed as the int is always 4 bytes):

declare@DataIDint=1234declare@Data1 nvarchar(max) = N'foo æøåè𨝫 bar'declare@Data2 nvarchar(max) = N'qux quee'declare@buffervarbinary(max)
 =cast(@DataIDasvarbinary(4)) +cast(@Data1+ N'/'asvarbinary(max)) +cast(@Data2asvarbinary(max))

select@buffer, select hashbytes('MD5', @buffer)

For

0x000004D266006F006F002000E600F800E500E80061D86BDF20006200610072002F0071007500780020007100750065006500  
0x9DA035DB9D9C319BB636D5E89F4D0EC6

C#

int    DataID = 1234;
string Data1  = "foo æøåè𨝫 bar";
string Data2  = "qux quee";

List<byte> buffer = new List<byte>(BitConverter.GetBytes(DataID));
buffer.Reverse(); // swap endianness for int
buffer.AddRange(Encoding.Unicode.GetBytes(Data1 + "/"));
buffer.AddRange(Encoding.Unicode.GetBytes(Data2));

using (MD5 md5 = MD5.Create())
{
    byte[] hashBytes = md5.ComputeHash(buffer.ToArray());

    //...
}

For

000004D266006F006F002000E600F800E500E80061D86BDF20006200610072002F0071007500780020007100750065006500
9DA035DB9D9C319BB636D5E89F4D0EC6

Post a Comment for "Comparing A C# Generated Checksum With A Sql Server One"