How To Get The MD5 Hash Of A File In VB.NET

MD5 UPTUTORIALS
MD5 is a widely used cryptographic algorithm that produces a 128-BIT hash value. I think all the files you have in your computer has a unique md5 hash.. in some virus scanning process the files get identified easily with the hash value. Today we are going to make a simple application that will help you to get the md5 hash of any file.

Start a new project and do the traditions lol.. you must add a button, a label and a textbox.

user interface :)

You must add these namespaces before you could do any coding :

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Here's the code for the application it's all under button click event.. I have explained the basic things by small comments.

  'creating the openfiledialog
        Dim Open As OpenFileDialog = New OpenFileDialog
        Open.Filter = "All Files (*.*)|*.*"
        If (Open.ShowDialog() = DialogResult.OK) Then TextBox1.Text = Open.FileName
        If Open.FileName = "" Then Exit Sub

        'accessing file & getting the hash
        Dim RD As FileStream = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        RD = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
        md5.ComputeHash(RD)
        RD.Close()

        'converting the bytes into string
        Dim hash As Byte() = md5.Hash
        Dim SB As StringBuilder = New StringBuilder
        Dim HB As Byte
        For Each HB In hash
            SB.Append(String.Format("{0:X1}", HB))
        Next
        LabelHash.Text = "MD5 : " & SB.ToString()

The usage is pretty easy all you have to do is select and load the file from the button and you will get the Md5 hash for the selected file in the Label.