This is an extension for FlorentB's answer that worked brilliantly for me until my files exceeded the 2GB LOF () size limit.
I tried to adapt to get the file length by alternative means as follows:
Public Function ComputeMD5(filepath As String) As String If Len(Dir(filepath)) Then Else Err.Raise 5, , "File not found." & vbCr & filepath Dim blockSize As Long: blockSize = 2 ^ 20 Dim blockSize_f As Double Dim buffer() As Byte Dim fileLength As Variant Dim hFile As Integer Dim n_Reads As Long Dim i As Long Dim svc As Object: Set svc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider") fileLength = DecGetFileSize(filepath) If fileLength < blockSize Then blockSize = ((fileLength + 1024) \ 1024) * 1024 ReDim buffer(0 To blockSize - 1) n_Reads = fileLength / blockSize blockSize_f = fileLength - (CDbl(blockSize) * n_Reads) hFile = FreeFile Open filepath For Binary Access Read As hFile For i = 1 To n_Reads Get hFile, i, buffer svc.TransformBlock buffer, 0, blockSize, buffer, 0 Next i Get hFile, i, buffer svc.TransformFinalBlock buffer, 0, blockSize_f buffer = svc.Hash svc.Clear Close hFile ComputeMD5 = String$(32, "0") For i = 0 To 15 Mid$(ComputeMD5, i + i + 2 + (buffer(i) > 15)) = Hex(buffer(i)) Next End Function Public Function DecGetFileSize(fname As String) As Variant Dim fso As New FileSystemObject Dim f: Set f = fso.GetFile(fname) DecGetFileSize = CDec(f.Size) Set f = Nothing Set fso = Nothing End Function
This all works fine by returning a line, however this line is not equal to MD5 calculated using other tools in the same file.
I cannot understand where the discrepancy arises.
I checked and double checked filelength, n_reads, blockSize and blockSize_f, and I'm sure that these values ββare correct.
I had some problems with the Get function, where if I had not explicitly indicated the block number to it, it dies in block 2048.
Any ideas / pointers would be highly appreciated.
FrugalTPH
source share