Calculating an MD5 Hash in VB.net

I was looking through my photos yesterday and I ran into a problem because it looks like I have hundreds of duplicate pictures.  I think that this must have happened when I was trying to get all the files that I had in the three or four different picture folders into one single folder.  I knew that calculating an MD5 hash would find the duplicates without having to get into huge programs that would compare creating dates, file sizes, etc to find the duplicates but I wasn’t sure how to find and MD5 hash in VB.net so after some work with MSDN I came up with the following function:

In theory an MD5 hash should return a unique 128-bit value for every file in existence.  It has been shown in the past that it is possible to create collisions in the MD5 hash function so keep that in mind.  My duplicate file sorter makes you compare the files before you delete them.

Private Function getFileMd5(ByVal filePath As String) As String
    ' get all the file contents
    Dim File() As Byte = System.IO.File.ReadAllBytes(filePath)

    ' create a new md5 object
    Dim Md5 As New MD5CryptoServiceProvider()

    ' compute the hash
    Dim byteHash() As Byte = Md5.ComputeHash(File)

    ' return the value in base 64
    Return Convert.ToBase64String(byteHash)
End Function

I returned the value in base64 because it was quick and because it results in a shorter string.

6 Responses to “Calculating an MD5 Hash in VB.net”

  1. links for 2008-01-10 « Andy’s Blog Says:

    [...] Calculating an MD5 Hash in VB.net « Scott Warren’s Weblog (tags: VB.NET) [...]

  2. Jagan Says:

    hi…..
    can u plz tell me to in details how to carry out this process…..

  3. Scott Warren Says:

    Jagen,
    What do you mean? What specifically do you need more details about?

  4. Me Says:

    This does not generate a MD5 hash -.-

    The text: Form1
    Becomes: G6PcnpYkBhW7lmXvUn9/Ag==

    The text: Hello There
    Becomes: bcvsyEVNZpxOvwyGQnNacw==

    Totally NOT an MD5 hash.

  5. Scott Warren Says:

    These are base 64 encoded MD5 string. If you return just the byteHash value you will get the standard hex MD5 hash.

  6. Joe Strahm Says:

    I am using Visual Studio.net 2003 and I tried using your code.

    on this line of code:
    Dim File() As Byte = System.IO.File.ReadAllBytes(filePath)

    I get an error:
    ReadAllBytes is not a member of System.IO.File

Leave a Reply