Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent to SHA1() in MS-SQL?

Converting a couple stored procedures from MySQL to Microsoft SQL server. Everything is going well, except one procedure used the MySQL SHA1() function. I cannot seem to find an equivalent to this in MS-SQL.

Does anyone know a valid equivalent for SHA1() on MS-SQL?

like image 441
GEOCHET Avatar asked Oct 08 '08 19:10

GEOCHET


2 Answers

SQL Server 2005 and later has the HashBytes() function.

like image 146
Joel Coehoorn Avatar answered Sep 23 '22 08:09

Joel Coehoorn


If you want to get a SHA1 hash exactly as MySQL would generate it (i.e. as a varchar), you can combine HashBytes with sys.fn_varbintohexsubstring. E.g.

SELECT sys.fn_varbintohexsubstring(0, HashBytes('SHA1', 'password'), 1, 0) 

See http://accessrichard.blogspot.co.nz/2010/12/sql-server-and-net-equivalent-to-php.html for more details.

like image 37
Peter Avatar answered Sep 19 '22 08:09

Peter