VBA计算文件内容的MD5哈希值

时间:2015-11-14 03:05:20

标签: vba ms-access md5 ms-access-2013

我需要一个VBA例程来计算文件内容的MD5哈希值。我找到了一些例子(例如,here),但我发现当文件名包含某些Unicode字符时它们会崩溃,所以我试图调整代码以避免这种情况。

此代码不会导致错误,但它也不会返回正确的MD5哈希值。怎么了?

Public Function FileToMD5Hex(sFileName As String) As String
    Dim enc
    Dim bytes
    Dim outstr As String
    Dim pos As Integer
    Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
    '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
    FileToMD5Hex = outstr
    Set enc = Nothing
End Function

Private Function GetFileBytes(path As String) As Byte()
    Dim fso As Object
    Set fso = CreateObject("scripting.FileSystemObject")

    Dim fil As Object
    Set fil = fso.GetFile(path)

'    Dim fpga As Variant
    GetFileBytes = fil.OpenAsTextStream().Read(fil.Size)

    Set fil = Nothing
    Set fso = Nothing
End Function

1 个答案:

答案 0 :(得分:3)

有一些字符序列Scripting.FileSystemObject无法正常处理TextStream

使用ADODB.Stream ActiveX从文件中检索字节数组。它与文本和二进制类型的数据完美配合,也允许更改字符串的字符集(FSO仅适用于ASCII和Unicode,仅适用于文件)。

Function GetFileBytes(strPath As String) As Byte()
    With CreateObject("ADODB.Stream")
        .Type = 1 ' adTypeBinary
        .Open
        .LoadFromFile (strPath)
        GetFileBytes = .Read()
    End With
End Function

另一个ActiveX处理二进制数据是SAPI.spFileStream。最重要的优点之一 - 它允许仅将文件的一部分加载到内存中(在某些情况下,比较大文件时,它可以帮助大幅提高性能,通过块检查md5)。

Function GetFileBytes(strPath As String) As Byte()
    Dim arrContent As Variant
    With CreateObject("SAPI.spFileStream")
        .Open strPath, 0
        .Read arrContent, CreateObject("Scripting.FileSystemObject").GetFile(strPath).Size
        .Close
    End With
    GetFileBytes = arrContent
End Function