I'm trying to implement an Hmac in T-SQL using [this question][1] as an guide. Please critique this: create function dbo.Hmac ( @key varbinary(8000) , @message varbinary(8000) ) returns table with schemabinding as return ( select HashBytes('SHA2_256', Pads.o + HashBytes('SHA2_256', Pads.i + @message)) as [Hash] from (values( Cast(case when DataLength(@key) > 64 then HashBytes('SHA2_256', @key) else @key end as varbinary(64)) -- keys longer than blocksize are shortened and keys shorter than blocksize are zero-padded , Cast(0x3636363636363636 as bigint) -- Rfc2104 inner padding constant , Cast(0x5C5C5C5C5C5C5C5C as bigint) -- Rfc2104 outer padding constant )) as Vars ([key], innerPadding, outerPadding) cross apply (values( -- iPad unrolled loop Cast((Substring(Vars.[key], 1, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 9, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 17, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 25, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 33, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 41, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 49, 8) ^ Vars.innerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 57, 8) ^ Vars.innerPadding) as varbinary(64)) -- oPad unrolled loop , Cast((Substring(Vars.[key], 1, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 9, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 17, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 25, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 33, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 41, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 49, 8) ^ Vars.outerPadding) as varbinary(64)) + Cast((Substring(Vars.[key], 57, 8) ^ Vars.outerPadding) as varbinary(64)) )) as Pads (i, o) ); [1]: http://stackoverflow.com/questions/7837547/is-there-a-sql-implementation-of-pbkdf2