views:

873

answers:

2

Hello,

I need to create a keyed hash for a string of XML to send to a 3rd party. This is the code I am using but it is producing a different hash then the example the 3rd party has sent me. I have been through all the tutorials I can find and re-read MSDN again and again, what am I doing wrong? Or should I suspect a problem at the other end?

Public Shared Function HashString(ByVal StringToHash As String) As String
    Dim myEncoder As New System.Text.UTF32Encoding
    Dim Key() As Byte = myEncoder.GetBytes(My.Settings.PortalHASH)
    Dim XML() As Byte = myEncoder.GetBytes(StringToHash)
    Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
    Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
    Return Convert.ToBase64String(HashCode)
End Function

It needs to be base-64 encoded, which is why I have the last line.

Thanks

A: 

I can't comment on the correctness of the VB, but from the obviousness of the naming, I can say it looks correct.

So there's not enough information here to say whats wrong; and you've likely said everything you know. Therefore, the people to verify this for you would be your third party. They'd be able to say what was wrong.

PS: strange that there is no nonce sent by the other party as a challenge?

Will
The other end just kicks to a server fault :) at the moment, our side is in very early stages of development and so I can't identify what is wrong as it could be anything! If the above genuinely looks correct then maybe I have the key wrong from them or some such, cheers for your input
David A Gibson
+1  A: 

(Read my comments)
If My.Settings.PortalHASH is the Base64 encoded key you need to do this:

Dim Key() As Byte = Convert.FromBase64String(My.Settings.PortalHASH)
Sani Huttunen
Hi CKret that makes a lot of sense - I tried the line above and it still does not match but I can't believe the 3rd party is sending me the key hashed - I will however get on the phone to them and double check all the details, Thanks.
David A Gibson
Hi CKret, as well as the answer above I was using an incorrect Encoding object I wanted ASCII not UTF32 apparently - would have been nice if they told me! Your answer was closest though - cheers.
David A Gibson
Great news that you fixed it! I should have picked up the possible encoding problem but alas did not. Well... good thing you solved it.
Sani Huttunen