Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HMAC SHA-1 Challenge

13 views
Skip to first unread message

atwork8

unread,
May 14, 2008, 11:42:02 AM5/14/08
to
Hi there,

I'm using the following code to hash a string using sha-1:

http://p2p.wrox.com/archive/proasp_howto/2002-02/27.asp

Now what I'm trying to do is write a function to return an HMAC
(HMAC-SHA-1). I've used the implementations listed on the following links:

http://en.wikipedia.org/wiki/HMAC#Implementation
http://tools.ietf.org/html/rfc2104

And my function so far looks like this:

<%
function hmac(key,text)
dim h 'hash function md5, sha1 etc
dim k 'secret key
dim b 'Blocksize "in bytes" of the hash function (sha1 blocksize = 512 bits
: 8bits in byte : 512/8 = 64 bytes)
dim l 'Byte length of the generated hash (sha1 outputs 160 bits : 8bits in
byte : 160/8 = 20 bytes)
dim arrSize 'array size for our string byte array. will be "b" above (b-1)
as arrays are 0 based

h = "sha1"
k = key '64 bytes to keep it simple
b = 64
l = 20
arrSize = b-1

dim arrIpad()
redim arrIpad(arrSize)

dim arrOpad()
redim arrOpad(arrSize)

dim strIpad
dim strOpad
dim i

'TODO: add code to check key size. made key 64 bytes to keep things simple

for i=0 to arrSize
arrIpad(i) = &h36
arrOpad(i) = &h5c
next

for i=0 to arrSize
arrIpad(i) = arrIpad(i) Xor Asc(CStr(Mid(k,i+1,1)))
arrOpad(i) = arrOpad(i) Xor Asc(CStr(Mid(k,i+1,1)))
strIpad = strIpad & arrIpad(i)
strOpad = strOpad & arrOpad(i)
next

hmac = sha1(strOpad & sha1(strIpad & text))
end function

response.write
hmac(")!%rw{LH:[9b|!2A,an_n}]}aLnzTzbHdOIC%Y/?4bC&J[h-+hfM`Lj_>B[/7i#I","hello world")
%>

The expected output is: 1da1b8f5901f02b0d95532acbf9c0db9f7b427ee
Actual output: 85c1891c3a7ba16d0582126ee4849f250fc95c45

I've been at this for ages, can anybody point me in the right direction to
get this to work?

Any help would be really appreciated, thanks :o)

tlviewer

unread,
May 16, 2008, 9:05:30 PM5/16/08
to
hello,

see inline

I changed maybe four lines and added one function StrFromHex()

good luck,
Mark from tlviewer.org

"atwork8" <atw...@discussions.microsoft.com> wrote in message
news:00B08C20-3293-4190...@microsoft.com...


> Hi there,
>
> I'm using the following code to hash a string using sha-1:
>
> http://p2p.wrox.com/archive/proasp_howto/2002-02/27.asp
>
>

strIpad = strIpad & Chr(arrIpad(i))

> strOpad = strOpad & arrOpad(i)

strOpad = strOpad & Chr(arrOpad(i))

> next
>
> hmac = sha1(strOpad & sha1(strIpad & text))

hmac = sha1(strOpad & StringFromHex(sha1(strIpad & text)))

> end function

' add this helper function
Public Function StringFromHex(sText) 'As String
' 10-16-02 moved into the global module
Dim lCount 'As Long
Dim sChar 'As String
Dim sResult 'As String
Dim lLength 'As Long
Dim nJ 'As Long

lLength = Len(sText)
nJ = 1
For lCount = 1 To lLength Step 2
' Mid$(sResult, nJ, 1) = Chr(Val("&H" & Mid(sText, lCount, 2)))
sResult = sResult & Chr(Eval("&H" & Mid(sText, lCount, 2)))
nJ = nJ + 1
Next

StringFromHex = sResult
End Function

' to test rfc2204 vector use
mystr = StringFromHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b")
' mystr = ")!%rw{LH:[9b|!2A,an_n}]}aLnzTzbHdOIC%Y/?4bC&J[h-+hfM`Lj_>B[/7i#I"
mystr = mystr & String(64 - Len(mystr), vbNullStr)

'WSCript.echo sha1("The quick brown fox jumps over the lazy dog")
WScript.echo "hmac=",hmac(mystr,"Hi There")
' answer is b617318655057264e28bc0b6fb378c8ef146be00

0 new messages