Md5哈希整个列表框

时间:2017-05-07 13:52:34

标签: vb.net loops listbox md5

截至目前,我可以使用

哈希列表框所选项目
    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

并在另一个列表框中收集它们,但是我得到一个错误(类型' System.Wackows.Forms.dll中发生System.StackOverflowException'未处理的异常)大约4K随机随机,好像它失败了要更新标签或文本框,然后必须编辑我的列表并重置,我只是觉得有更好的方法来做到这一点。 更有经验的人可以提供一些指导,使这个例程更有效率吗?

2 个答案:

答案 0 :(得分:0)

不确定您的错误来自何处,但您绝对不需要在紧密循环中使用Create()。将其存储为局部变量,并通过将其声明为静态(或在类级别保留引用)来重新使用它:

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

答案 1 :(得分:0)

好的,这就是我想通过一个巨大的限制比我正在做的更好的工作,我等待大约一个小时的4K哈希现在我通过散列字符串来获得50K分钟。文本。可能仍然很乱,但工作速度更快。

     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