从 VBA 调用 .NET 加密

Calling .NET Cryptography from VBA

我正在尝试在 VBA 中生成 SHA-1 哈希(我不是很熟悉)。在 .NET 中,它非常简单,使用 System.Security.Cryptography。一种方法如下:

using System.Security.Cryptography;
...

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

你如何从 VBA 调用它?我做到了这一点

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

但我不知道如何将 Byte 数组传递给 ComputeHash

hash = oSHA.ComputeHash(oBytes)

这将引发有关参数不正确的错误。如何将其转换为 .NET 接受的格式?

请注意,我不想对 SHA-1 使用 VBA 计算(太慢了,我不想重新发明轮子)。如果我不必为 .NET 部分编写任何包装器,那也很好。

ComputeHash被重载,需要指定调用哪一个,本例为:

hash = oSHA.ComputeHash_2(bytes)

来自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)