Friday, March 25, 2011

MD5 hasing in SQL Server 2008

Today I wanted to import a whole bunch of passwords in the database. The problem is, our passwords are md5 encrypted. The cool thing is, this is possible using SQL servers hashbytes function
HASHBYTES( 'md5', 'demo' )
this results in a varbinary value of: 0xFE01CE2A7FBAC8FAFAED7C982A04E229.
use the convert function to make this a readable string:
CONVERT(VARCHAR(MAX), HASHBYTES( 'md5', 'demo' ), 2)
for style make it a lowercase using the lower function:
LOWER(CONVERT(VARCHAR(MAX), HASHBYTES( 'md5', 'demo' ), 2))
+1 for SQL Server

Luuk