Search code examples
c#sql-server-2008hmacsha1

Is it possible to provide a custom key for SQL HashBytes function?


I'm using HMACSHA1 with machine validation key in C# .NET 4.0 to generate hash in my application but I can't seem to find any resource on TSQL HashBytes function being able to take a key string.

Can anyone provide a working example to generate matching hashes with the same key?


Solution

  • As far as I know, SQL server does not have a documented function for HMACSHA1. You will likely have to write a SQL-CLR function to do that. Given that you already have the code in C#, it should be fairly simple to convert that into a SQL-CLR scalar function. You may be able to do something incredibly hack-ish by re-implementing the algorithm in T-SQL; but it would be inefficient and prone to error. T-SQL wasn't meant for that.