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.
Would anyone have any idea about that? thank you very much.
HMAC Generator — Online Hash Encryption
Here is an HMAC (keyed-hash message authentication code) online generator that generates a cryptographic hash function in combination with a secret encryption key.
www.liavaag.org
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