Indivara
Indivara

Reputation: 765

Calling .NET Cryptography from VBA

I am trying to generate an SHA-1 hash in VBA (which I am not very familiar with). In .NET it is pretty straightforward, using System.Security.Cryptography. One method is as follows:

using System.Security.Cryptography;
...

byte[] data;
// fill data here
SHA1Managed sha = new SHA1Managed();
bytes[] hash = sha.ComputeHash(data);

How would you go about calling this from VBA? I got as far as this

Dim oSHA As Object
Set oSHA = CreateObject("System.Security.Cryptography.SHA1Managed")

but I can't figure out how to pass a Byte array to ComputeHash

hash = oSHA.ComputeHash(oBytes)

This throws an error about the parameter being incorrect. How do you convert it to a format accepted by .NET?

Note that I do NOT want to use VBA calculations for SHA-1 (too slow, and I don't want to reinvent the wheel). It would also be nice if I didn't have to write any wrappers for the .NET portion.

Upvotes: 2

Views: 1982

Answers (2)

Evan Courtney
Evan Courtney

Reputation: 41

From System.Security.Cryptography.HashAlgorithm:

  • ComputeHash_1(inputStream As stream)
  • ComputeHash_2(buffer As byte())
  • ComputeHash_3(buffer As byte(), offset As int, count As int)

Upvotes: 4

Alex K.
Alex K.

Reputation: 175936

ComputeHash is overloaded, you need to specify which one you want to call, in this case:

hash = oSHA.ComputeHash_2(bytes)

Upvotes: 3

Related Questions