C# hash and sql server's hashbytel function, same implementation?

1.4k Views Asked by At

I want to has on both the c# level and the sql server 2012 level.

How do I know if the SHA1 implementation will produce the same results?

In sql server I am doing:

HASHBYTES('SHA1', @data)

I haven't written my c# function yet.

I'm just worried if they don't produce the same hash value, I will get some inconsistencies in my application.

Note: The columns I will be using to create a hash are datetime, nvarchar and int. I have to convert these to strings correct?

1

There are 1 best solutions below

10
Alexandre Severino On

You won't have any problems using this code:

    public static byte[] GetHash(string inputString)
    {
        HashAlgorithm algorithm = SHA1.Create();
        return algorithm.ComputeHash(Encoding.Unicode.GetBytes(inputString));
    }

It creates just the same hash as you can create on SQL Server:

HASHBYTES('SHA1', CAST(@data AS NVARCHAR(100)))