KingOfBSOD
KingOfBSOD

Reputation: 21

VB.NET MD5 Comparing using large files

I'm trying to create a program in VB.NET 2010 that has a simple functionality. It has to compare thousands of MD5 Hashes stored in a text file to current file MD5 Hash that program is automatically calculating while opening the file. Like a antivirus scanner. Actually my program use ReadAllText system to add all hashes from text file to a textbox and then it compare them.

Everyting is okay when Md5 database (that text file with hashes) is small but when the file get bigger, my program simply freezes after opening so I decided to use ReadLine instead of ReadAllText.

Now I can't use textbox any longer so give me a way how I can compare them. I tried a way but it don't seem to work.

Here is my code using ReadLine.

Problem is on If Read contains(buff.tostring) it used to be If textbox.text contains (buff.toString)

Here is the code

Try
            TextBox2.Text = e.FullPath
            ListBox3.Items.Add(TextBox2.Text.ToString)
            Me.OpenFileDialog1.FileName = ""
            Dim reader As StreamReader = New StreamReader("def.txt")
            Dim read = reader.ReadLine()
            Dim md5 As New MD5CryptoServiceProvider
            Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
            md5.ComputeHash(f)
            Dim hash As Byte() = md5.Hash
            Dim buff As New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            If read.Contains(buff.ToString) Then
                Me.OpenFileDialog1.FileName = e.FullPath
                Form2.ShowDialog()

            End If

        Catch exception1 As Exception
            ProjectData.SetProjectError(exception1)
            Dim ex As Exception = exception1
            ProjectData.ClearProjectError()
        End Try

Upvotes: 0

Views: 1424

Answers (1)

the_lotus
the_lotus

Reputation: 12748

I would first create functions to split the functionality. It's a lot easier to understand the code afterward. Store the hashes in a list, this list can then be cached is needed.

Try

    TextBox2.Text = e.FullPath
    ListBox3.Items.Add(TextBox2.Text.ToString)
    Me.OpenFileDialog1.FileName = ""

    Dim allHash As List(Of String) = GetAllHash()
    Dim curHash As String = GetFileHash(e.FullPath)

    If allHash.Contains(curHash) Then
        Me.OpenFileDialog1.FileName = e.FullPath
        Form2.ShowDialog()
    End If

Catch exception1 As Exception
    ProjectData.SetProjectError(exception1)
    Dim ex As Exception = exception1
    ProjectData.ClearProjectError()
End Try

Function GetAllHash() As List(Of String)

    ' Store the data in a list instead
    Return System.IO.File.ReadAllLines("def.txt").ToList()
End Function

Function GetFileHash(ByVal filename As String) As String

    Dim md5 As New MD5CryptoServiceProvider
    ' Only open the file once
    Dim f As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, &H2000)
    md5.ComputeHash(f)
    Dim hash As Byte() = md5.Hash
    Dim buff As New StringBuilder

    For Each hashByte As Byte In hash
        buff.Append(String.Format("{0:X2}", hashByte))
    Next

    f.Close()

    Return buff.ToString()
End Function
  • I didn't compile this code, this is just a example to show you what can be done.

Upvotes: 1

Related Questions