문제

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?

도움이 되었습니까?

해결책

The hash differs because the data differs.

The file is UTF-8, not ASCII, so you should use the UTF-8 encoding to convert the string to bytes to get the same result:

byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog");

Also, the file may contain a BOM (byte order mark) at the beginning. That is included in the data, as the file isn't read as text.

Adding the UTF-8 BOM at the beginning of the data would give the same hash:

byte[] bom = { 239, 187, 191 };
byte[] data = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog");

byte[] bomdata = new byte[bom.Length + data.Length];
bom.CopyTo(bomdata, 0);
data.CopyTo(bomdata, bom.Length);
byte[] hash = MD5.Create().ComputeHash(bomdata);

다른 팁

Did you trim the string from the file for whitespace and new lines?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top