MD5 Scan

Posts 1–6 of 6 · Page 1 of 1
MD5 Scan
I want to scan for a select list of MD5 checksums, and if found, delete the file. how would i go about doing this? i have absolutely no idea S:
edit: I mean like scanning a computer for files with md5 checksums, like a virus scanner scanning for the md5 checksums of known viruses.
[php]Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Function MD5FileHash(ByVal sFile As String) As String
Dim MD5 As New MD5CryptoServiceProvider
Dim Hash As Byte()
Dim Result As String = ""
Dim Tmp As String = ""

Dim FN As New FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
MD5.ComputeHash(FN)
FN.Close()

Hash = MD5.Hash
For i As Integer = 0 To Hash.Length - 1
Tmp = Hex(Hash(i))
If Len(Tmp) = 1 Then Tmp = "0" & Tmp
Result += Tmp
Next
Return Result
End Function[/php]

I don't understand your question.
We don't flame a lot in the programming section unless you are really an arse and you're denying it, don't worry.

Although some more information towards what you want accomplished would be fine. I'm not sure exactly what you want done.
I guess he wants to make a duplicate finder. Using the files md5 to determine if two files are duplicates.
Here we go. Correct any errors as necessary. Just wrote it on MPGH post reply, so yeahs. C# and Visual Basic versions provided.

You will need to put your MD5 hashes which you want to check against in an array, either hard coded or loaded from a file or from a website or however you want into a string[] called "MD5Hashes". You can change the variable names if you wish.

C#
Code:
string MD5file(string fileName)
{
  FileStream file = new FileStream(fileName, FileMode.Open);
  MD5 md5 = new MD5CryptoServiceProvider();
  byte[] retVal = md5.ComputeHash(file);
  file.Close();
 
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < retVal.Length; i++)
  {
    sb.Append(retVal[i].ToString("x2"));
  }
  return sb.ToString();
}

int main()
{
	string[] filePaths = Directory.GetFiles(@"C:\freedompeace");
	foreach (string file in filePaths)
	{
		foreach (string MD5hash in MD5Hashes)
		{
			if (MD5file(file) == MD5hash)
			{
				File.Delete(file);
			}
		}
	}
}
Visual Basic
Code:
Private Function MD5file(fileName As String) As String
	Dim file As New FileStream(fileName, FileMode.Open)
	Dim md5 As MD5 = New MD5CryptoServiceProvider()
	Dim retVal As Byte() = md5.ComputeHash(file)
	file.Close()

	Dim sb As New StringBuilder()
	For i As Integer = 0 To retVal.Length - 1
		sb.Append(retVal(i).ToString("x2"))
	Next
	Return sb.ToString()
End Function

Private Function main() As Integer
	Dim filePaths As String() = Directory.GetFiles("C:\freedompeace")
	For Each file__1 As String In filePaths
		For Each MD5hash As String In MD5Hashes
			If MD5file(file__1) = MD5hash Then
				File.Delete(file__1)
			End If
		Next
	Next
End Function
Why are you posting code in c#? No1 really needs that here...
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Need help?