2011-11-23 86 views
2

嗨我想加密一個字符串來調用VBA的Web服務。我需要在VBA中執行以下功能,並使用PHP中的示例代碼。這裏是PHP代碼。有誰知道如何在VBA中做到這一點?是否VBA有一個Hash_HMAC

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

http://www.karenware.com/powertools/pthasher.asp –

+0

@Tim威廉姆斯,這是一般的,由於一個有趣的網站。 –

+0

看到這個職位:http://stackoverflow.com/questions/10068548/base64-hmac-sha1-string-in-vba – HK1

回答

8

這裏有您需要什麼:

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 
+0

哦,也應該是「System.Security.Cryptography.HMACSHA512」 – user3074620