简体   繁体   English

将文件路径添加到MD5哈希计算

[英]Add file path to the MD5 hash calculation

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. 我不知道是否可以将文件的文件路径添加到MD5哈希码的计算中。

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? 那么是否可以将til文件路径添加到计算中?

BR BR

An MD5 hash is just an algorithm for taking some binary data, and creating a 128-bit hash value from it. MD5哈希只是一种算法,用于获取一些二进制数据,并从中创建一个128位哈希值。

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. 但是,如果您处于其他情况,那就很好。

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. MD5只是一个哈希,它完全取决于您哈希的内容,因此,如果传递完整路径,它将把它添加到哈希中。

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 这是一些代码(未经优化,但编写起来很容易理解),它将为传入的任何字符串返回MD5哈希

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM