I generate a MD5 Hash from a String and from a File Containing the Same String using System.Security.Cryptography.MD5. However the Hash Values Differ.
Here is the code to generate from a string
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();
And Here is the code when I generate the hash from the file
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();
}
}
}
The Hash from the string is correct, So I assume the Hash from the file reads some extra stuffs or doesn't read the entire file. I couldn't find an answer on Google.
Any Ideas?