Using the Bcrypt hash algorithm in MS-SQL Server
Storing the passwords in plain text within a database is not a good idea. Many people use the same password for their email, online-banking or social network accounts and probably your app. So the passwords should be safe if your database gets into the wrong hands.
So how do you safely store a password?
One way could be to use a library for PHP, Java, Ruby, Python and so on and store the generated hash in MS SQL Server.
The better way would be to have MS SQL Server do the encryption and keep your app server code free of this.
Unfortunately SQL Server doesn’t come with a builtin BCrypt function. But here’s how you can implement it in 10 minutes.
To make BCrypt available in MS SQL Server follow these steps:
The Common Language Runtime (CLR) for the .NET framework makes this possible. It integrates directly with Microsofts SQL Server from version 2005 on. By using .NET we are able to deploy a DLL directly to the SQL Server and map its Methods to functions and stored procedures to be used in T-SQL.
This is the quick way on how you can add this feature to your Server Management Console by doing a little bit of copy & paste.
Switch to the database where you want to add Bcrypt and issue the following SQL statements.
Turn CLR for your database on, so you are able to use modules that are written for .NET
- Download this file and run it in your database context.
This file is the dll encoded in hexadecimal values. Your SQL Server will accept this as a bytestream so you don’t have to download a library and place it somewhere on the filesystem.
By the way: In this file you will find the statement WITH PERMISSION_SET=SAFE. This sets restrictions for executing these functions so they can’t access the filesystem, network, registry or other external ressources.
- Now map the assembly methods BCrypt and CheckPassword to scalar valued functions, so they are available in T-SQL.
And the CheckPassword function:
Now check if everything is working:
Note: You can also do all this manually by downloading the source and compiling it with Visual Studio 2005 or higher, using a C# database project.
More information on bcrypt in .NET and the C# source code is available here.
If you've read this far you might as well follow me on Twitter here.