我使用System.Security.Cryptography.MD5从String和包含相同字符串的文件生成MD5哈希.但是哈希值不同.
这是从字符串生成的代码
byte[] data = Encoding.ASCII.GetBytes("The quick brown fox jumps over the lazy dog");
byte[] hash = MD5.Create().ComputeHash(data);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
Run Code Online (Sandbox Code Playgroud)
这是我从文件生成哈希时的代码
internal static string CalculateFileHashTotal(string fileLocation)
{
using(var md5 = MD5.Create())
{
using (var stream = File.OpenRead(fileLocation))
{
byte[] b = md5.ComputeHash(stream);
stream.Close();
return BitConverter.ToString(b).Replace("-", "").ToLower();
}
}
}
Run Code Online (Sandbox Code Playgroud)
字符串中的哈希是正确的,所以我假设文件中的哈希读取了一些额外的东西或者没有读取整个文件.我在Google上找不到答案.
有任何想法吗?