HMAC SHA1 hash hex input string

Julien888

New Member
Joined
Jun 19, 2020
Messages
12
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
Hi, I am new to HMAC, I need to perform HMAC SHA1 using some HEX input data and TEXT key to get some HEX output data, I have found the below VBA code to process some TEXT input data and get Base 64 output data and this works fine, if I use it in Excel and check one some website as below, I get the same result. However I need to start from some hexadecimal representation of binary data and get the hashed value as hexadecimal, I have been able to modify the code below to get an hexadecimal output instead of base 64 (using some function to convert to hex string instead of the EncodeBase64 function), but I am unable to adjust the code to use my hex input data... strangely, using some function to convert hex to ascii and replacing sTextToHash by HEX2ASCII(sTextToHash) did exactly what I want... but only for some small hex strings, for example with 0000007B000004 I get the same result as on the website, however with my actual data 0000007B000004D2546578747572650043314641313260027800004DB00301B6D81E0000000003001303E8000A05DC000507D0000501F400010BB800050DAC00080BB800080FA0003C1388003C I don't get the correct output... (and I actually noticed it worked up to 0000007B00000472 but from 0000007B00000482 it doesn't work anymore...)

Would anyone have any idea about that? thank you very much.


VBA Code:
Public Function Base64_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As String)
Dim asc As Object, enc As Object
Dim TextToHash() As Byte
Dim SharedSecretKey() As Byte
Set asc = CreateObject("System.Text.UTF8Encoding")
Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")

TextToHash = asc.Getbytes_4(sTextToHash)
SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
enc.Key = SharedSecretKey

Dim bytes() As Byte
bytes = enc.ComputeHash_2((TextToHash))
Base64_HMACSHA1 = EncodeBase64(bytes)
Set asc = Nothing
Set enc = Nothing
End Function


Private Function EncodeBase64(ByRef arrData() As Byte) As String
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement

Set objXML = New MSXML2.DOMDocument

' byte array to base64
Set objNode = objXML.createElement("b64")
objNode.DataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.Text

Set objNode = Nothing
Set objXML = Nothing
End Function


Private Function HEX2ASCII(ByVal hextext As String) As String
    
For Y = 1 To Len(hextext)
    num = Mid(hextext, Y, 2)
    value = value & Chr(Val("&h" & num))
    Y = Y + 1
Next Y

HEX2ASCII = value
End Function


Private Function ConvToHexString(vIn As Variant) As Variant

    Dim oD As Object
      
    Set oD = CreateObject("MSXML2.DOMDocument")
      
      With oD
        .LoadXML "<root />"
        .DocumentElement.DataType = "bin.Hex"
        .DocumentElement.nodeTypedValue = vIn
      End With
    ConvToHexString = Replace(oD.DocumentElement.text, vbLf, "")
    
    Set oD = Nothing

End Function
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Julien888, welcome to MrExcel!

You might be looking for something like this. Input is hex representation, output is in hex representation as well.
VBA Code:
Public Function HEX_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As String) As String
    Dim asc                 As Object
    Dim enc                 As Object
    Dim TextToHash()        As Byte
    Dim SharedSecretKey()   As Byte
    Dim Bytes()             As Byte
    Dim sHexString          As String
    Dim i                   As Long
    
    Set asc = CreateObject("System.Text.UTF8Encoding")
    Set enc = CreateObject("System.Security.Cryptography.HMACSHA1")
    
    TextToHash = asc.Getbytes_4(sTextToHash)
    SharedSecretKey = asc.Getbytes_4(sSharedSecretKey)
    enc.Key = SharedSecretKey
    
    Bytes = enc.ComputeHash_2((TextToHash))
    For i = LBound(Bytes) To UBound(Bytes)
        sHexString = sHexString & Hex(Bytes(i))
    Next
    HEX_HMACSHA1 = sHexString
    Set asc = Nothing
    Set enc = Nothing
End Function
 
Upvote 0
Dear GWteB,

thank you for your reply, actually in the meantime I found a solution to do it but a paying option, using the Cryptosys API, however I am still on the free trial period (which lasts two months), so if I can find a free solution it is interesting.

I tried your macro but the output does not seem right...

Let's say the key is 01234567890123456789 and the input is 00003039000004D24372656D6520746F7563686572207365630043314641323164027800006DB6DB6DB6001E556400B401FFFF00FA01F4000F03E8000F07D0000F00FA000501F4000A03E8000A00FA000501F4000503E8000500FA000501F4000503E80005 then the output should be 4EDC29C10F2ED47A363DCA044338E884210B639C (this is what I get using the Cryptosys API, I get the same on online generators such as liavaag) but your macro returns E5D626AFC6EB979DD68F756BDDD7E2925F4794B2.

Thank you again!
 
Upvote 0
The code as in my post #2 assumes that both the input and the key are in text format, although the text may have a hexadecimal appearance.
 
Upvote 0
Right, I should have explained better in my first post, my input data is rather binary data viewed in a hexadecimal format. Best regards.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top