2

I would like to retrieve the same checksum value in C# instead of using SQL code?

The goal is to retrieve the same value in c# instead of using SQL code.

Is it possible to do it?

I can't find any good answer to my question.

http://sqlfiddle.com/#!6/eb6d0/3/0

Draken
  • 3,109
  • 13
  • 34
  • 52
HelloWorld1
  • 12,940
  • 25
  • 73
  • 128

2 Answers2

2

According to this old article:

The built-in CHECKUM function in SQL Server is built on a series of 4 bit left rotational xor operations

So you can try to implement this algorithm in C#.

However, that article states that this approach is not reliable from the point of collisions so probably you should not use this function at all.

I would recommend to use HASHBYTES with any reliable algorithm i.e. MD5 instead of this CHECKSUM. At the same time C# has built in functions for MD5:

byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes("admin@sqlfiddle.com"));
Vsevolod Krasnov
  • 1,442
  • 2
  • 12
  • 19
0

The Microsoft requirement is to use HASHBYTES rather than CHECKSUM function because HASHBYTES is more efficient than CHECKSUM.

I don't know what is the algorithm used by CHECKSUM, but HASHBYTES can use these hash algorithm (MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512). You can easily do the same thing in C# with the HashAlgorithm Class from System.Security.Cryptography.