I am working on a VBA project where I need to compute an HMAC-SHA256 hash for Binance API authentication. Despite multiple attempts, I keep encountering errors related to the hashing process. Here is the code I have been using:
Function GetHMACSHA256(secretKey As String, message As String) As String
Dim hash As Object
Dim key() As Byte
Dim data() As Byte
Dim result() As Byte
Dim i As Integer
Dim tempHex As String
Dim hexResult As String
Set hash = CreateObject("System.Security.Cryptography.HMACSHA256")
key = StrConv(secretKey, vbFromUnicode)
data = StrConv(message, vbFromUnicode)
hash.Key = key
result = hash.ComputeHash_2(data)
hexResult = ""
For i = 1 To LenB(result)
tempHex = Right("0" & Hex(AscB(MidB(result, i, 1))), 2)
hexResult = hexResult & LCase(tempHex)
Next i
GetHMACSHA256 = hexResult
End Function
However, I receive the following error:
Runtime Error: Unable to set the property value
Could someone please provide guidance on how to resolve this issue? Any help would be greatly appreciated!
Additional Information:
Excel version: Office 365
Operating System: Windows 10
MaoGe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.