Jeremy__
03-03-2021, 01:32 AM
I must, with Excel file, hash a data list (Text String) with secret key (Hex String) with HMAC SHA1 algorithm, do you have some ideas ?
I found this code (see below) but the secret key is not in hexadecimal...
Public Function HEX_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As Variant)
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))
'HEX_HMACSHA1 = ConvToHexString(Bytes)
Set asc = Nothing
Set enc = Nothing
End Function
Private Function ConvToHexString(vIn As Variant) As Variant
'Check that Net Framework 3.5 (includes .Net 2 and .Net 3 is installed in windows
'and not just Net Advanced Services
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
Jérémy
I found this code (see below) but the secret key is not in hexadecimal...
Public Function HEX_HMACSHA1(ByVal sTextToHash As String, ByVal sSharedSecretKey As Variant)
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))
'HEX_HMACSHA1 = ConvToHexString(Bytes)
Set asc = Nothing
Set enc = Nothing
End Function
Private Function ConvToHexString(vIn As Variant) As Variant
'Check that Net Framework 3.5 (includes .Net 2 and .Net 3 is installed in windows
'and not just Net Advanced Services
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
Jérémy