15,395,181 members
See more:
Hello I have somewhat of a problem. I am trying to execute a MD5 hashing algorithm for a given file but all I have is the directory as a string.

for example I have this as my string..
C:\Users\Subfolder\file.exe

How can I use the string to locate the file to then calculate the md5 hash of the file and not just the md5 calculation of the string?

Posted

## Solution 1

VB
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 = ""

MD5.ComputeHash(FN)
FN.Close()

Hash = MD5.Hash
Result = Strings.Replace(BitConverter.ToString(Hash), "-", ""
Return Result
End Function

should do the trick ^^
Draco2013 28-Aug-13 4:47am

this along with other examples I have found do not return the correct hash of the file. I am testing with the eicar text file witch has a hash of 44D88612FEA8A8F36DE82E1278ABB02F but with your example I get E37D3A9FF32C946E7A3934E8E7DC6345 which I suspect to be the hash of the string of the filename and not the file itself. any help would be greatly appreciated

thank you cheers!
David Goebet 28-Aug-13 5:44am

if i put a test.pdf in three different locations and let this code run (MD5CryptoServiceProvider("Path\to\my\file.txt")

i get three times the same hash (with three different locations(over 3 Harddrives)
and the other file has another hash so i think its not to wrong
Draco2013 28-Aug-13 4:50am

the fullpath and the file name are being passed as a string and not within the fileinfo class is this possible?..