2016-04-03 3 views
1

Ich möchte eine VB-Funktion in VBA übersetzen. Die Funktion verwendet "System.Text.UTF8Encoding" und "System.Security.Cryptography.HMACSHA256"Cant Verwendung. GetBytes und. ComputeHash-Methoden auf VBA

Objekte und ihre ".GetBytes" und ".ComputeHash" Methoden.

Ich habe bereits "System" und "Mscorlib.dll" References zum VBA-Code hinzugefügt, aber ich erhalte "ungültiger Prozeduraufruf oder Argument" Fehler.

Hier ist meine VB-Funktion:

Function HashString(ByVal StringToHash As String, ByVal HachKey As String) As String 
    Dim myEncoder As New System.Text.UTF8Encoding 
    Dim Key() As Byte = myEncoder.GetBytes(HachKey) 
    Dim Text() As Byte = myEncoder.GetBytes(StringToHash) 
    Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key) 
    Dim HashCode As Byte() = myHMACSHA256.ComputeHash(Text) 
    Dim hash As String = Replace(BitConverter.ToString(HashCode), "-", "") 
    Return hash.ToLower 
End Function 

Und das ist, was ich schon in VBA übersetzt habe:

Function HashString(ByRef StringToHash As String, ByRef HachKey As String) As String 
    Dim myEncoder As Object 
    Dim myHMACSHA256 As Object 
    Dim Key As Byte 
    Dim Text As Byte 
    Dim HashCode As Byte 
    Dim hash As String 
    Set myEncoder = CreateObject("System.Text.UTF8Encoding") 
    Set myHMACSHA256 = CreateObject("System.Security.Cryptography.HMACSHA256") 
    Key = myEncoder.GetBytes(HachKey) 
    Text = myEncoder.GetBytes(StringToHash) 
    HashCode = myHMACSHA256.ComputeHash(Text) 
    hash = Replace(BitConverter.ToString(HashCode), "-", "") 
    HashString = hash.ToLower 
End Function 

auf diese Kann jemand helfen? Meine erste Vermutung ist, dass ich im Voraus „.GetBytes“ und „.ComputeHash“ Methoden falsch

Dank bin mit

Antwort

2

Ein Arbeitsbeispiel zur Berechnung der HMACSHA256 mit VBA akzeptiert eine Zeichenfolge und _2 für:

Function ComputeHMACSHA256(key As String, text As String) As String 
    Dim encoder As Object, crypto As Object 
    Dim hash() As Byte, hmacsha As String, i As Long 

    ' compute HMACSHA256 
    Set encoder = CreateObject("System.Text.UTF8Encoding") 
    Set crypto = CreateObject("System.Security.Cryptography.HMACSHA256") 
    crypto.key = encoder.GetBytes_4(key) 
    hash = crypto.ComputeHash_2(encoder.GetBytes_4(text)) 

    ' convert to an hexa string 
    hmacsha = String(64, "0") 
    For i = 0 To 31 
    Mid$(hmacsha, i + i + (hash(i) > 15) + 2) = Hex(hash(i)) 
    Next 

    ComputeHMACSHA256 = LCase(hmacsha) 
End Function 


Sub UsageExample() 
    Debug.Print ComputeHMACSHA256("abcdef", "12345") 
End Sub 
+0

Danke, arbeitete wie ein Charme :) – Behnam2016

1

Wenn über COM verwendet, um zu unterstützen .NET-Funktionen Überlastung haben Implementierungen auf Basis von Name_n. Als GetBytes Überlastung müssen Sie GetBytes_4() die die Überlastung, die ComputeHash()


Function HashString(ByRef StringToHash As String, ByRef HachKey As String) As String 
    Dim myEncoder As Object 
    Dim myHMACSHA256 As Object 
    Dim Key() As Byte '// all need to be arrays 
    Dim Text() As Byte 
    Dim HashCode() As Byte 
    Dim hash As String 
    Set myEncoder = CreateObject("System.Text.UTF8Encoding") 
    Set myHMACSHA256 = CreateObject("System.Security.Cryptography.HMACSHA256") 
    Key = myEncoder.GetBytes_4(HachKey) 
    Text = myEncoder.GetBytes_4(StringToHash) 
    HashCode = myHMACSHA256.ComputeHash_2(Text) 

    Dim i As Long 
    For i = 0 To UBound(HashCode) 
     Debug.Print Format$(Hex(HashCode(i)), "00") 
    Next 
End Function 

?HashString("qwe", "rty") 
80 
D5 
22 
5D 
83 
06 
... 
+0

Danke, dass Sie auf "Überladung" hingewiesen haben, aber ich bekomme "ungültige Qual ifier "error on" GetBytes_4 "Methode – Behnam2016

+0

danke für Überladung klarstellen zu – user6698332