4

I'm trying to compute the hash of a string on VBA (Excel 2003), but when I call ComputeHash, it throws me an Invalid argument/procedure call error.

DLL References: mscorlib v4.0, System v4.0

MSDN reference: http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha512managed.aspx

 Sub Main()
        Dim instance As New SHA512Managed
        Dim data() As Byte
        data = StringToByte("mymsg")
        Dim result() As Byte
        instance.ComputeHash(data) 'Throws runtime error'
        MsgBox (ByteToString(result))
    End Sub

    Function StringToByte(ByVal s)
        Dim b() As Byte           
        b = s  'Assign Unicode string to bytes.'
        StringToByte = b
    End Function

    Function ByteToString(ByVal dBytes)
        Dim strText As String
        strText = dBytes
        ByteToString = strText
    End Function
Gaffi
  • 4,307
  • 8
  • 43
  • 73
WoF_Angel
  • 2,569
  • 9
  • 35
  • 54
  • See if this helps? http://www.vbaexpress.com/forum/showthread.php?t=23032 – Siddharth Rout Jul 09 '12 at 12:40
  • Where is that reference (SHA512Managed) coming from (which library)? I have a few different versions of the mscorlib.dll, but can't link them to the VBE... – Gaffi Jul 09 '12 at 13:40
  • mscorlib.dll v4.0, with System v4.0 – WoF_Angel Jul 09 '12 at 14:01
  • You mean .NET? I have that .dll for only up to .NET 2.0, but I still can't add the reference. Anyway, I'm adding an answer that I cannot test... – Gaffi Jul 09 '12 at 14:10

2 Answers2

5

You can't quite use it like that, but you're almost there. Since .ComputeHash is an overloadable function, and VBA can't handle this you need to be explicit in which function you want to call. So consider the below, encoded to base64 using a UTF-8 string:

Sub test()
Dim text As Object
Dim SHA512 As Object

Set text = CreateObject("System.Text.UTF8Encoding")
Set SHA512 = CreateObject("System.Security.Cryptography.SHA512Managed")

Debug.Print ToBase64String(SHA512.ComputeHash_2((text.GetBytes_4("Hello World"))))

End Sub

Function ToBase64String(rabyt)

  'Ref: http://stackoverflow.com/questions/1118947/converting-binary-file-to-base64-string
  With CreateObject("MSXML2.DOMDocument")
    .LoadXML "<root />"
    .DocumentElement.DataType = "bin.base64"
    .DocumentElement.nodeTypedValue = rabyt
    ToBase64String = Replace(.DocumentElement.text, vbLf, "")
  End With
End Function
SWa
  • 4,343
  • 23
  • 40
2

I can't get the library to link, so I can't test this myself...

Do you mean also to assign result like this?

result = instance.ComputeHash(data)

Looking deeper into that link you provided, I found this example:

Dim data(DATA_SIZE) As Byte
Dim result() As Byte

Dim shaM As New SHA512Managed()
result = shaM.ComputeHash(data)

This doesn't seem correct in my head, but again, I can't test for sure, but they've added () at the end of the New declaration. Have you tried that?

Gaffi
  • 4,307
  • 8
  • 43
  • 73
  • Not working, throws same error. About the references, i just go to tools and mark them, i didn't install the environment so i don't have much to say about it. You should try to unmark them, close the references window, open it, and see if the v4.0 version is there. – WoF_Angel Jul 09 '12 at 14:16
  • Updated answer - I think you need to actually assign `result`? – Gaffi Jul 09 '12 at 14:20
  • @WoF_Angel I'd like to, but they aren't even in the list to check. I have to manually browse for them, which then throws an error. I'm not going to worry about it, I have no use for myself at the present time for the libs; just looking to help test your code. – Gaffi Jul 09 '12 at 14:22
  • Oi! Reviewing the reference you linked to, it looks like you've done it correctly. Since I can't test/play with it, I'm not sure if I'm going to be of much more help to you, sorry. – Gaffi Jul 09 '12 at 14:25
  • I will try to implement it myself. Thanks anyway. – WoF_Angel Jul 09 '12 at 14:27
  • @WoF_Angel I have one more suggestion (see updated answer). Take a look at your `Dim instance As New SHA512Managed` line... – Gaffi Jul 09 '12 at 14:37
  • Syntax compile error on the Dim shaM As New SHA512Managed() line – WoF_Angel Jul 09 '12 at 16:44