gpt4 book ai didi

excel - VBA有Hash_HMAC吗

转载 作者:行者123 更新时间:2023-12-02 09:31:53 27 4
gpt4 key购买 nike

您好,我正在尝试加密字符串以从 VBA 调用 Web 服务。我需要在 VBA 中执行以下函数,并且我有 PHP 示例代码。这是 PHP 代码。有谁知道如何在VBA中做到这一点?

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

最佳答案

这是您需要的:

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

关于excel - VBA有Hash_HMAC吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com