次のVBA関数を使用して、ファイルのSHA256値を取得しています。system.security.cryptography.SHA256 .NET 4.5(Windows 10)で管理
Public Function FileToSHA256(sfilename As String) As String
Dim enc
Dim bytes
Dim outstr As String
Dim pos As Integer
Set enc = CreateObject("System.Security.Cryptography.SHA256Managed")
'Convert the string to a byte array and hash it
bytes = GetFileBytes(sfilename)
bytes = enc.ComputeHash_2((bytes))
'Convert the byte array to a hex string
For pos = 1 To LenB(bytes)
outstr = outstr & LCase(Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2))
Next
FileToSHA256 = outstr 'Returns a 40 byte/character hex string
Set enc = Nothing
End Function
Private Function GetFileBytes(ByVal path As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte
lngFileNum = FreeFile
If LenB(Dir(path)) Then ''// Does file exist?
Open path For Binary Access Read As lngFileNum
ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
Get lngFileNum, , bytRtnVal
Close lngFileNum
Else
Err.Raise 53
End If
GetFileBytes = bytRtnVal
Erase bytRtnVal
End Function
これは、私はこれが(3.5を想定)で動作します.NETバージョンを知らないのWord 2013でのWindows 7環境で完璧に動作します。
これはWindows 10環境(まだWord 2013)上に起動されています。私は私たちのIT部門から、これが.NET 4.5であると言われましたが、VBAによると、.NETフレームワーク4.0.30319から実行しています。
は、今ではスローエラー - この行の
実行時エラー '-2146232576(80131700)' オートメーションエラー
。
Set enc = CreateObject("System.Security.Cryptography.SHA256Managed")
私はMSCORLIB.DLLをプロジェクトで参照しています。
参照を追加したりコードを変更する必要があるかどうかわかりません。
コードを変更する必要がある場合は、以前のバージョンの.NETもカバーする必要があります。別のバージョンをカバーするためのケースチェックがありますが、これを行う方法がわかりません。
暗闇でドッキリにしていますが、 'Cからmscorlibを追加してみてくださいでした:\ WINDOWS \ Microsoft.NET \ Framework64 \ v4.0.30319'? –
それはうまくいきませんでした。それは企業環境の中にあるので、私はアクセス権について少し縛られているからかもしれません。私は一緒にmscorlibを回避することによって問題を分類することができました。 –