VBA有一个Hash_HMAC [英] Does VBA have a Hash_HMAC

查看:948
本文介绍了VBA有一个Hash_HMAC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加密一个字符串来调用VBA中的一个Web服务。我需要在VBA中执行以下功能,我在PHP中有示例代码。这是PHP代码。有没有人知道如何在VBA中做到这一点?

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


解决方案

这是您需要的:

 公共功能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)
设置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
设置enc = Nothing

结束函数

私有函数EncodeBase64(ByRef arrData()As Byte)As String

Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement

设置objXML =新的MSXML2.DOMDocument

'字节数组到base64
设置objNode = objXML.createElement(b64)
objNode.DataType = bin.base64
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.Text

设置objNode = Nothing
设置objXML =没有

结束函数


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);

解决方案

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

这篇关于VBA有一个Hash_HMAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆