Behnam2016
Behnam2016

Reputation: 261

Cant use .GetBytes and .ComputeHash methods on VBA

I want to translate a VB function to VBA. The function is using "System.Text.UTF8Encoding" and "System.Security.Cryptography.HMACSHA256"

Objects and their ".GetBytes" and ".ComputeHash" methods.

I already added "System" and "mscorlib.dll" referrences to the VBA code, but I'm receiving "Invalid procedure call or argument" error.

Here is my VB function:

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

And this is what I've already translated into VBA:

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

Can anybody help on this? My first guess is that I'm using ".GetBytes" and ".ComputeHash" methods incorrectly

Thanks in advance

Upvotes: 2

Views: 3682

Answers (2)

Alex K.
Alex K.

Reputation: 175876

When used via COM in order to support overloading .Net functions have implementations based on Name_n. As GetBytes is overloaded you need GetBytes_4() which is the overload that accepts a string and _2 for 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
...

Upvotes: 1

Florent B.
Florent B.

Reputation: 42528

A working example to compute the HMACSHA256 with VBA:

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

Upvotes: 3

Related Questions