Question : TSQL HMAC function

I'm trying to create a HMAC function in SQL according to this specification: http://tools.ietf.org/html/rfc2104

Wikipedia also has a very good explanation: http://en.wikipedia.org/wiki/HMAC

Other than that the trail goes cold! I'm sure I can't be the first person to try and do this in SQL???

Below is as far as I've got, I'm totally lost as to where I'm going wrong, but it definetly doesn't work. If anyone could shed some light I'd really appreciate it ;)

--------------------------------------------------------------------------------------------
THE FUNCTION AS IT STANDS THUS FAR
--------------------------------------------------------------------------------------------

ALTER FUNCTION [dbo].[HMAC]
(
            @key VARCHAR(MAX),
            @message VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)



AS
BEGIN
      --HASH key if longer than 16 characters
      IF(LEN(@key) >16)
            SET @key = HASHBYTES('md5',@key)


      --pad key with '0's if shorter that 16 characters
      WHILE LEN(@key) <16
            BEGIN
                  SET @key = @key + '0'
            END

      --prepare ipad & opod
      DECLARE @ipad VARCHAR(MAX), @opad VARCHAR(MAX)
      
            SET @ipad = '6666666666666666'
            SET @opad = '\\\\\\\\\\\\\\\\'

      DECLARE @i_key_pad VARCHAR(MAX), @o_key_pad VARCHAR(MAX), @position INT
            SET @position = 1
            SET @i_key_pad = ''
            SET @o_key_pad = ''

      --splice ipad & opod with key
      WHILE @position <= 16
         BEGIN
            SET @i_key_pad = @i_key_pad + CHAR(ASCII(SUBSTRING(@ipad, @position, 1)) ^ ASCII(SUBSTRING(@key, @position, 1)))
            SET @o_key_pad = @o_key_pad + CHAR(ASCII(SUBSTRING(@opad, @position, 1)) ^ ASCII(SUBSTRING(@key, @position, 1)))
            SET @position = @position + 1
         END

      
RETURN HASHBYTES('md5',(@o_key_pad + CONVERT(VARCHAR(max),HASHBYTES('md5',(@i_key_pad + @message)))))

END


Answer : TSQL HMAC function

Cracked it!!! :)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
ALTER FUNCTION [dbo].[HMAC]
(
		@key VARCHAR(MAX),
		@message VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)

AS
BEGIN
	--HASH key if longer than 16 characters
	IF(LEN(@key) >64)
		SET @key = HASHBYTES('md5',@key)


	DECLARE @i_key_pad VARCHAR(MAX), @o_key_pad VARCHAR(MAX), @position INT
		SET @position = 1
		SET @i_key_pad = ''
		SET @o_key_pad = ''

	--splice ipad & opod with key
	WHILE @position <= LEN(@key)
	   BEGIN
		SET @i_key_pad = @i_key_pad + CHAR(ASCII(SUBSTRING(@key, @position, 1)) ^ 54) 
		SET @o_key_pad = @o_key_pad + CHAR(ASCII(SUBSTRING(@key, @position, 1)) ^ 92) 
		SET @position = @position + 1
	   END 
	
	--pad i_key_pad & o_key_pad
		SET @i_key_pad = LEFT(@i_key_pad + REPLICATE('6',64),64)
		SET @o_key_pad = LEFT(@o_key_pad + REPLICATE('\',64),64)
	
	
RETURN HASHBYTES('md5',CONVERT(VARBINARY(MAX),@o_key_pad) + HASHBYTES('md5',@i_key_pad + @message))

END
Random Solutions  
 
programming4us programming4us