Question

I have been searching a little but can't find anything about it. I wan't to know if it is possible to add the file path of the file to the calculation of the MD5 hash code.

Right now I have the normal calculation working and it is returning unique hash codes for all files that are different. But If I copy paste a file it will return the same hash code.

So is it possible to add til file path to the calculation?

BR

Was it helpful?

Solution

An MD5 hash is just an algorithm for taking some binary data, and creating a 128-bit hash value from it.

How you determine the binary data to hash is entirely up to you. It's entirely reasonable to (say) include the file path then the contents of the file (or vice versa) if that's useful to you. Usually it isn't, as you normally want to validate that you've got the same data as you originally had, and the metadata of file location isn't normally part of the "useful" information to be checked. You'd normally want copy/paste to give you the same hash code. But if you're in some different situation, that's fine.

OTHER TIPS

The MD5 is just a hash, it is totally up to you what you hash, so if you pass the full path it will add it to the hash.

Here is some code (it’s not optimised but written to be simple to understand), it will return the MD5 hash for any string passed in

public string MD5_Encrypt(string EncryptString)
{
  string strReturn = string.Empty;
  ASCIIEncoding ASCIenc = new System.Text.ASCIIEncoding();
  byte[] InputString = ASCIenc.GetBytes(EncryptString);
  System.Security.Cryptography.MD5CryptoServiceProvider MD5Hash = new System.Security.Cryptography.MD5CryptoServiceProvider();
  byte[] ByteHash = MD5Hash.ComputeHash(InputString);
  foreach (byte b in ByteHash)
  {
    strReturn += b.ToString("x2");
  }
  return strReturn.ToString();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top