Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHA256 in T-sql stored procedure

Is it possible to generate a SHA-256 hash of a string from a stored procedure in Sql Server 2008?

For deployment reasons, I'd prefer it in TSQL.

like image 262
WOPR Avatar asked Jun 02 '10 03:06

WOPR


2 Answers

Update: SQL Server 2012 HASHBYTES() now supports SHA-256 and SHA-512 out of the box.

HASHBYTES ( '<algorithm>', { @input | 'input' } )

<algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512

Sure. You can do it in TSQL, but it will be much easier to implement it as a CLR Stored procedure.

Here's an actual example, that simply uses the .NET Framework types: Let's Hash a BLOB

like image 140
Mitch Wheat Avatar answered Dec 21 '22 16:12

Mitch Wheat


SHA256, SHA512 in SQL SERVER 2008 OR SQL SERVER 2005!

You can do if you use the fnEnCryptSHA.dll!!

USE [master] GO
EXEC sp_configure 'clr enabled', 1 GO RECONFIGURE GO

CREATE ASSEMBLY InnoDll FROM 'C:\sqltip\fnEnCryptSHA.dll' WITH PERMISSION_SET= SAFE GO

CREATE FUNCTION dbo.fnGetStringToSha256 (@Str nvarchar(1000)) RETURNS varbinary(8000) AS EXTERNAL NAME InnoDll.fnEnCryptSHA.GetStringToSha256 GO

CREATE FUNCTION dbo.fnGetBinaryToSha256 (@Str varbinary(8000)) RETURNS varbinary(8000) AS EXTERNAL NAME InnoDll.fnEnCryptSHA.GetBinaryToSha256 GO

SELECT dbo.fnGetStringToSha256('abc')

SELECT dbo.fnGetBinaryToSha256(0x9F04F41A848514162050E3D68C1A7ABB441DC2B5)
like image 34
inno Avatar answered Dec 21 '22 15:12

inno