Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VBA have a Hash_HMAC

Tags:

excel

vba

sha512

Hi I am trying to Encrypt a string to invoke a web service from VBA. I need to do the following function in VBA and i have example code in PHP. Here is the PHP code. Does anyone know how to do this in VBA?

$binaryHash = hash_hmac('sha512', $url.$timestamp, $ws_session_array["sharedSecret"], true);
$hash = base64_encode($binaryHash);
like image 352
craig_nelson Avatar asked Nov 23 '11 17:11

craig_nelson


1 Answers

Here's what you need:

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
like image 196
HK1 Avatar answered Nov 15 '22 09:11

HK1