tags:

views:

129

answers:

2

Hi! I hope that someone could help me with reading exe files in C# and create a SHA1 hash from it. I have tried to read from executable file using StreamReader and BinaryReader. Then using built-in SHA1 algorithm I tried to create a hash but without success. The algorithm results for StreamReader was "AEUj+Ppo5QdHoeboidah3P65N3s=" and for BinaryReader was "rWXzn/CoLLPBWqMCE4qcE3XmUKw=". Can anyone help me to acheive SHA1 hash from exe file? Thx.

BTW Sorry for my English ;)

+5  A: 

Don't use a StreamReader - that will try to convert the opaque binary data into text data... an exe file is not text data.

Just use a FileStream and call ComputeHash:

byte[] hash;
using (Stream stream = File.OpenRead(filename))
{
    hash = SHA1.Create().ComputeHash(stream);
}
string base64Hash = Convert.ToBase64String(hash);
Jon Skeet
A: 

StreamReader implements TextReader so we are not in a binary world :-)

Patrice Pezillier