4

VBA (Excel 2003) で文字列のハッシュを計算しようとしていますが、 を呼び出すComputeHashInvalid argument/procedure callエラーが発生します。

DLL リファレンス: mscorlib v4.0、System v4.0

MSDN リファレンス: 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
4

2 に答える 2

5

そのように完全に使用することはできませんが、ほとんどそこにあります。.ComputeHash はオーバーロード可能な関数であり、VBA はこれを処理できないため、呼び出す関数を明示する必要があります。したがって、UTF-8 文字列を使用して base64 にエンコードされた以下を検討してください。

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
于 2012-07-10T09:36:35.630 に答える
2

リンクするライブラリを取得できないため、これを自分でテストすることはできません...

このように結果を割り当てることも意味しますか?

result = instance.ComputeHash(data)

あなたが提供したそのリンクをさらに詳しく調べると、次の例が見つかりました。

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

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

これは私の頭では正しくないようですが、確かにテストすることはできませんが、宣言()の最後に追加されています。Newあなたはそれを試しましたか?

于 2012-07-09T14:11:43.810 に答える