【问题标题】:Does VBA have a Hash_HMACVBA 是否有 Hash_HMAC
【发布时间】:2012-01-04 23:45:42
【问题描述】:

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

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

【问题讨论】:

标签: excel vba sha512


【解决方案1】:

这是你需要的:

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

【讨论】:

  • 哦,还应该是“System.Security.Cryptography.HMACSHA512”
  • 有人能解释一下CreateObject("System.Text.UTF8Encoding") 的作用吗?看起来ComputeHash_2 是一个使用过的名称,因为 VBA 不能重载函数。是否有一些有据可查的系统方法来使用该代码正在利用的 VBA 中的 .NET 类?
猜你喜欢
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 2012-08-17
相关资源
最近更新 更多