MD5 speedup

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hi group.

Any ideas how I could speed up/streamline the below code? Particuarly,
is there a way to use a progress bar for when it calculates the MD5?
Currently it just looks like it is hung for large files.


Private Function MakeMD5(srcFileName as String)

Dim DBfs As FileStream
Dim byt() As Byte

If (New FileInfo(srcFileName).Exists) Then

Try
Dim md5 As New MD5CryptoServiceProvider

DBfs = File.Open(srcFileName, FileMode.Open,
_FileAccess.Read, FileShare.ReadWrite)

byt = md5.ComputeHash(DBfs)

Return (BitConverter.ToString(byt))

Catch ex As Exception

Msgbox(ex.Message)

Finally '

If (Not IsNothing(DBfs)) Then DBfs.Close()

End Try

End If

End Function
 
Since System.Security.Cryptography.MD5.ComputeHash() is doing all the work,
I don't think there's much more you can do.

However, System.Security.Cryptography.MD5.ComputeHash() also accepts a Byte
array. I doubt you would see any sort of performance increase by loading
the file into a byte array yourself and then passing that to ComputeHash(),
but it's something to try.

If you want to have a progress bar increment with this, then you'll have to
write a function that will compute the hash yourself. ComputeHash() doesn't
appear to raise any trappable progress events.

- Don
 
Don said:
Since System.Security.Cryptography.MD5.ComputeHash() is doing all the work,
I don't think there's much more you can do.

However, System.Security.Cryptography.MD5.ComputeHash() also accepts a Byte
array. I doubt you would see any sort of performance increase by loading
the file into a byte array yourself and then passing that to ComputeHash(),
but it's something to try.

If you want to have a progress bar increment with this, then you'll have to
write a function that will compute the hash yourself. ComputeHash() doesn't
appear to raise any trappable progress events.

- Don

I thought about doing it via a stream as this will at least allow me to
draw something on the screen. Any ideas how to do this?

Thanks.
 
Back
Top