如何使用SHA1散列文件夹

Pac*_*art 0 c# hash

有没有办法使用SHA1来散列包含其中所有内容的文件夹?我能够使用MD5做到这一点,但我担心MD5遭受的冲突.我正在尝试构建一个应用程序来检查本地文件,以查看它们是否与使用哈希的在线版本匹配.

这是我在MD5中使用的代码:

var path = leftCheckTextbox.Text;
var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
                     .OrderBy(p => p).ToList();

MD5 md5 = MD5.Create();

for (int i = 0; i < files.Count; i++)
{
    string file = files[i];

    string relativePath = file.Substring(path.Length + 1);
    byte[] pathBytes = Encoding.UTF8.GetBytes(relativePath.ToLower());
    md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);

    byte[] contentBytes = File.ReadAllBytes(file);
    if (i == files.Count - 1)
        md5.TransformFinalBlock(contentBytes, 0, contentBytes.Length);
    else
        md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes,0);
}

leftHash = BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
Run Code Online (Sandbox Code Playgroud)

Ale*_*nov 9

只需在源代码中更改所有内容MD5即可SHA1.

  • 我觉得这是这个问题唯一明智的答案. (5认同)