-1

As of now I can hash listbox selected item with

    Public Function Md5FromString(ByVal Source As String) As String
    Dim Bytes() As Byte
    Dim sb As New StringBuilder()
    'Check for empty string.
    If String.IsNullOrEmpty(Source) Then
        Throw New ArgumentNullException
    End If
    'Get bytes from string.
    Bytes = Encoding.Default.GetBytes(Source)
    'Get md5 hash
    Bytes = MD5.Create().ComputeHash(Bytes)
    'Loop though the byte array and convert each byte to hex.
    For x As Integer = 0 To Bytes.Length - 1
        sb.Append(Bytes(x).ToString("x2"))
        On Error Resume Next
    Next
    'Return md5 hash.
    Return sb.ToString()

End Function

And collect them in another listbox, but I get an error (An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll) after about 4K somewhere random as if it fails to update a label or textbox then have to edit my list and reset and i just feel there is a better way to do this. Can someone more experienced offer some guidance in making this routine more efficient?

2 Answers2

0

Not sure where your error is coming from, but you definitely don't need to be using Create() in a tight loop. Store it as a local variable and re-use it by declaring it as static (or keep the reference at class level instead):

Public Function Md5FromString(ByVal Source As String) As String
    Static local_MD5 As MD5 = MD5.Create

    If String.IsNullOrEmpty(Source) Then
        Throw New ArgumentNullException
    End If

    Dim sb As New StringBuilder()
    For Each b As Byte In local_MD5.ComputeHash(Encoding.Default.GetBytes(Source))
        sb.Append(b.ToString("x2"))
    Next
    Return sb.ToString()
End Function
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Ok, here is how I figured out to work better than what I was doing by a tremendous bound, I was waiting for about an hour for 4K hashes now I get 50K in mins by hashing the string as they come in from the .txt. May still be messy but it works way faster.

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = ""
    OpenFileDialog1.Title = "Please Select a File"
    OpenFileDialog1.InitialDirectory = "C:temp"
    OpenFileDialog1.ShowDialog()
    Dim path As String = OpenFileDialog1.FileName
    TextBox1.Text = path
    Dim lines() As String = IO.File.ReadAllLines(TextBox1.Text)
    For Each line In lines
        ListBox1.Items.Add(Md5FromString(line) + ":" + line)
        ListBox1.Refresh()
        Label1.Text = ListBox1.Items.Count
        Label1.Refresh()
        If ListBox1.Items.Count = 1000 Then
            save()
            ListBox1.Items.Clear()
            ListBox1.Refresh()
            Label1.Text = 0
        End If
    Next


    ' ListBox1.SelectedIndex = 0

End Sub
  • Changed the 1000 to a 1 and now it rips though a list and instantly saves to a new .txt – Bobby_Boner May 07 '17 at 15:11
  • It would go a heck of a lot faster if you got rid of the ListBox all together! You can't read them that fast, so why bother updating the screen?...updating the screen slows it down. Just set a ProgressBar to "Marquee" style and process all of the lines in a BackgroundWorker(). Furthermore, for larger files, you should [use File.ReadLines() instead of File.ReadAllLines()](http://stackoverflow.com/a/21970035/2330053). Using ReadAllLines() cause the whole file to be read into memory at once. If the file is large enough this will cause the swapfile to be used slowing things down even further. – Idle_Mind May 08 '17 at 23:11
  • ...and how does this fix your error from your original question?...you never told us what line was causing it. – Idle_Mind May 08 '17 at 23:12
  • @idle mind, The error I was able to work around by using a a slightly altered approach. Before I could handle maybe 4040 at the total max before the error. Now I have ran 3 Million with no sign of the error or any for that matter. I will change the Readlines() from now on tho. – Bobby_Boner May 09 '17 at 14:57