如何使用MD5哈希搜索文件 [英] How Do I search files using MD5 hash

查看:274
本文介绍了如何使用MD5哈希搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在特定的递归目录中使用MD5哈希搜索多个* .exe文件。





目前我正在使用此代码获取* .exe文件,但是当有许多子目录时需要花费很多时间。





尝试

For Each f在Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.ProgramFiles,*。exe,SearchOption.AllDirectories)

ListBox2.Items.Add(f)

下一页



Catch ex As UnauthorizedAccessException



结束尝试



-

并将每个* .exe文件MD5哈希与文本框中的MD5哈希列表相匹配,





Timer.Tick





试试



ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1



Catch ex As Exception



结束尝试



尝试

Dim sbox As New TextBox

Dim read As String = My.Computer.FileSystem.ReadAllText(MD5-Hash-Box.Text)

sbox.Text = read.ToString

Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider

Dim f As FileStream = New FileStream(ListBox2.SelectedItem,FileMode.Open,FileAccess.Read,FileShare.Read,8192)

f = New FileStream(ListBox2.SelectedItem,FileMode.Open ,FileAccess.Read,FileShare.Read,8192)

md5.ComputeHash(f)

Dim hash As Byte()= md5.Hash

Dim buff As StringBuilder = New StringBuilder

Dim hashByte As Byte

For Each hashByte in hash

buff.Append(String.Format( {0:X2},hashByte))

下一页



如果是sbox.Text.Contains(buff.ToString)那么

listbox1.items.add(listbox2.selecteditem)

结束如果



Catch ex As Exception



结束尝试

How Do I search multiple *.exe files using there MD5 hash in a particular recursive directory.


Currently I am using this Code to get *.exe files,But it takes a lot of time when there are many sub directories.


Try
For Each f In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.ProgramFiles, "*.exe", SearchOption.AllDirectories)
ListBox2.Items.Add(f)
Next

Catch ex As UnauthorizedAccessException

End Try

-
And matching each *.exe file MD5 hash with list of MD5 hash in a textbox,


Timer.Tick


Try

ListBox2.SelectedIndex = ListBox2.SelectedIndex + 1

Catch ex As Exception

End Try

Try
Dim sbox As New TextBox
Dim read As String = My.Computer.FileSystem.ReadAllText(MD5-Hash-Box.Text)
sbox.Text = read.ToString
Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
Dim f As FileStream = New FileStream(ListBox2.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
f = New FileStream(ListBox2.SelectedItem, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
md5.ComputeHash(f)
Dim hash As Byte() = md5.Hash
Dim buff As StringBuilder = New StringBuilder
Dim hashByte As Byte
For Each hashByte In hash
buff.Append(String.Format("{0:X2}", hashByte))
Next

If sbox.Text.Contains(buff.ToString) Then
listbox1.items.add(listbox2.selecteditem)
End If

Catch ex As Exception

End Try

推荐答案

没有奇迹这样的东西。如果您有一些MD5值,则需要至少读取一次文件及其所有内容,计算数据的MD5并与MD5值进行比较。这需要时间。



没有什么可以绕过的。如果你的代码中有一些愚蠢,你必须消除它,当然,但是......哦,等一下,你有一个:你在浪费时间考虑所有文件,而不只是* .exe和计算的endsWith 。您应该使用文件掩码: http://msdn.microsoft.com /en-us/library/ms143316%28v=vs.110%29.aspx [ ^ ]。



但是,您需要知道一个Microsoft问题与此相关,如下所述: Directory.Get.Files搜索模式问题 [ ^ ]。



您将节省一些时间,但我认为与读取文件和计算MD5所需的时间相比,这种优化会很小。



-SA
There is no such thing as miracle. If you have some value of MD5, you will need to read each file at least once, all its content, calculate MD5 of the data and compare with your MD5 value. It will take time.

There is nothing you can bypass. If you have some silliness in your code, you have to eliminate it, of course, but… Oh, wait a minute, you have one: you are wasting time on considering all the files, not just "*.exe" and calculating EndsWith. You should use file mask instead: http://msdn.microsoft.com/en-us/library/ms143316%28v=vs.110%29.aspx[^].

However, you need to know one Microsoft problem related to it, described here: Directory.Get.Files search pattern problem[^].

You will save some time on it, but I think this optimization would be minor compared to the time needed to read file and calculate MD5.

—SA


这篇关于如何使用MD5哈希搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆