Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an ASCII SHA-512 hash in SQL Server

I'm working on a C# project where we have a text value that is placed in a SQL Server database table in a nvarchar field. The value is hashed using the code below:

byte[] data = Encoding.ASCII.GetBytes("valuetohash");
byte[] bytes = new SHA512Managed().ComputeHash(data);
String result = Encoding.ASCII.GetString(bytes);

Now I need to duplicate creating that same value using T-SQL.

Can someone tell me how I can do that?

I tried HASHBYTES ( 'SHA2_512', 'valuetohash' )

but that lacks the ASCII encoding and produces a different value.

like image 676
crazysnake Avatar asked Dec 10 '22 15:12

crazysnake


1 Answers

You need to convert binary data to a Base64 string and you may try using the CONVERT function:

SELECT CONVERT(varchar(max), HASHBYTES ('SHA2_512', 'valuetohash') ,2) 
like image 66
Trifon Avatar answered Jan 02 '23 15:01

Trifon