VBA有Hash_HMAC吗?

7

你好,我正在尝试使用VBA加密字符串以调用web服务。 我需要在VBA中执行以下函数,并且我有PHP示例代码。 这是PHP代码。 有人知道如何在VBA中执行此操作吗?

$binaryHash = hash_hmac('sha512', $url.$timestamp, $ws_session_array["sharedSecret"], true);
$hash = base64_encode($binaryHash);

http://www.karenware.com/powertools/pthasher.asp - Tim Williams
@Tim Williams,总的来说,那是一个有趣的网站,谢谢。 - Doug Glancy
请查看此帖子:https://dev59.com/jmLVa4cB1Zd3GeqPwnhV - HK1
1个回答

13

以下是您所需要的内容:

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

1
哦,还有它应该是“System.Security.Cryptography.HMACSHA512”。 - user3074620
有人能解释一下 CreateObject("System.Text.UTF8Encoding") 是做什么的吗?看起来 ComputeHash_2 是一个混淆的名称,因为 VBA 无法重载函数。这段代码是否利用了某种系统化的、有文档支持的方法来使用 .NET 类库呢? - eksortso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接