The difference between a text-based MD5 hash sum and a file-based hash sum
There is a program whose essence is to accept an HTTP request with a file and in the response you need to send an MD5 hash amount. I found out that it is possible to read a hash based on text and based on a file, and in some points they are different, but because of what-I did not understand.
After studying the examples, I wrote the following code
FileStream fstream = File.OpenRead(pathStr + @"\" + partnerName + @"\IN\" + strname);
byte[] array = new byte[fstream.Length];
fstream.Read(array, 0, array.Length);
string textFromFile = win1251.GetString(array);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(Encoding.Default.GetBytes(textFromFile));
fstream.Close();
Here's the problem. If I build a console application with this code in VS2019, then my hash is calculated as I need it-from the text. BUT if you try to build a project like the Windows service, then the same code counts the amount from the file. I check it in Notepad++.
Help teach the Windows service build to calculate a hash based on the text
1 answers
Because you are transcoding the file to a string and back, using different encodings for reading and writing. But you can do it without transcoding at all.
byte[] array = File.ReadAllBytes(Path.Combine(pathStr, partnerName, "IN", strname));
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(array);
Console.WriteLine(BitConverter.ToString(retVal).Replace("-", "").ToLowerInvariant();
Just like that.
To avoid loading the entire file into memory, you can read the hash from the data from the stream
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal;
using (Stream stream = File.OpenRead(Path.Combine(pathStr, partnerName, "IN", strname)))
{
retVal = md5.ComputeHash(stream);
}
If you need to convert from Win-1251 to UTF-8, you will get this
byte[] array = File.ReadAllBytes(Path.Combine(pathStr, partnerName, "IN", strname));
string text = win1251.GetString(array);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(Encoding.UTF8.GetBytes(text));