I’ve decided to start blogging about some code that took me a while to find or figure out… Hopefully other people looking for this code won’t have to suffer what I did :).
MD5 Checksums are used frequently to verify both the validity of files and to check for corruption after large downloads or uploads. There are several free applications for Windows that can calculate the MD5 Checksum of a file though I wanted to be able to make myself an application customized for my own use.
You will need to import a few .NET Classes to get started:
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
IO is used to manage loading the files in and out of your application. Cryptography will retrieve the raw MD5 Hash and Text will append it. Of course, you don’t need to import any of these but it will clean up your code and is a good programming practice.
You now need to select the file you wish to run the MD5 algorithm on. If you are building an application that will always be checking the MD5 of the same file in the same place, this is very simple. However, this is usually not the case.
Dim o As OpenFileDialog = New OpenFileDialog
Dim temp As String
Dim path As Stringo.Filter = “.zip Files|*.zip|All Files|*.*”
If (o.ShowDialog() = DialogResult.OK) Then
path = o.FileName
This snippet of code creates an OpenFileDialog and declares temp and path (temp will be used later on). The OpenFileDialog is then showed and if the user successfully selects a file it is set to path. The rest of the coding will be done in this If/Then block to avoid attempting to run the MD5 algorithm on an incorrect or nonexistent file.
The o.Filter can be used if you want only certain types of files to be visible in the file browser. The way o.Filter is set in my example, .zip Files and All Files will be selectable from the drop down list. It is quite simple to customize the o.Filter to your desire or simply omit it altogether.
We will now get the raw MD5 checksum of our file with the following code:
Dim fs As FileStream
fs = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
md5.ComputeHash(fs)
fs.Close()
Here we create a new FileStream for our previously selected file and use the built in MD5CryptoServiceProvider to compute the raw hash of our file. We end by closing the FileStream so the file can be accessed by other programs and the user.
We will now use the StringBuilder class to append our string and format it correctly:
Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format(”{0:X2}”, hashByte))
Nexttemp = buff.ToString()
We now have our MD5 Checksum stored to temp! By default it is in all caps… This may be suitable in your situation but i need lower case. This is as simple as:
return temp.ToLower
And there you have it! For those of you that don’t like to read and understand things for yourself here is a fully working function.
Imports System.Security.Cryptography
Imports System.IO
Imports System.TextFunction MD5Checksum(ByVal path As String) As String
Dim temp As StringDim fs As FileStream
fs = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
md5.ComputeHash(fs)
fs.Close()Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format(”{0:X2}”, hashByte))
Nexttemp = buff.ToString()
Return temp
End Function
You will need to call this function in one of two ways:
MD5Checksum(”C:\MyFile.ext”)
In that case, you know exactly where the file you want is… if you want to browse for the file, you can use this:
Dim o As OpenFileDialog = New OpenFileDialog
Dim path As StringIf (o.ShowDialog() = DialogResult.OK) Then
MD5Checksum(o.FileName)
Feel free to download the source code to the MD5 Demo App I’ve created below! Note: I use VB.NET 2008 so you will not be able to view the source on anything below 2008. I’ve added a download link for a .txt document of the source code so you can view it if you have VB.NET 2005 or lower. And of course, you can download the standalone .exe and just use the program!
Download VB.NET 2008 Source Code
[...] - bookmarked by 3 members originally found by Ran56 on July 14, 2008 MD5 Checksum in Visual Basic .NET http://sideapps.com/2008/06/11/md5-checksum-in-visual-basic-net/ - bookmarked by 4 members [...]