Getting MD5 of EXE

G

greatbarrier86

Hi,

I'm completely stumped on this but i want to create something that can get
the md5 value of an EXE based on the passed in argument and compare it to the
specified MD5.

I can do the comparison, but i dont know how to get the EXE md5 value. Can
someone help?

http://www.howforge.com/how-to-calculate-md5-in-c kinda helps, but i am
still stuck.

Thanks,
Jason
 
N

Nicholas Paldino [.NET/C# MVP]

Jason,

Take a look at the MD5Cng or the MD5CryptoServiceProvider classes in the
System.Security.Cryptography namespace. Basically, you will open a
FileStream for the EXE file and pass that to the ComputeHash method on the
instance of the object. It will return the bytes representing the hash,
which you can compare to your stored value.
 
G

greatbarrier86

I'm just starting out here so i need a little more help, if you don't mind.
Also, does the passed in argument have to be converted too?

Nicholas Paldino said:
Jason,

Take a look at the MD5Cng or the MD5CryptoServiceProvider classes in the
System.Security.Cryptography namespace. Basically, you will open a
FileStream for the EXE file and pass that to the ComputeHash method on the
instance of the object. It will return the bytes representing the hash,
which you can compare to your stored value.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

greatbarrier86 said:
Hi,

I'm completely stumped on this but i want to create something that can get
the md5 value of an EXE based on the passed in argument and compare it to
the
specified MD5.

I can do the comparison, but i dont know how to get the EXE md5 value. Can
someone help?

http://www.howforge.com/how-to-calculate-md5-in-c kinda helps, but i am
still stuck.

Thanks,
Jason
 
A

Arne Vajhøj

greatbarrier86 said:
I'm completely stumped on this but i want to create something that can get
the md5 value of an EXE based on the passed in argument and compare it to the
specified MD5.

I can do the comparison, but i dont know how to get the EXE md5 value. Can
someone help?

http://www.howforge.com/how-to-calculate-md5-in-c kinda helps, but i am
still stuck.

Try:

public static string MD5(string fnm)
{
HashAlgorithm md5 = new MD5CryptoServiceProvider();
using(Stream f = new FileStream(fnm, FileMode.Open,
FileAccess.Read))
{
byte[] b = new byte[100000];
byte[] garbage = new byte[100000];
int n;
while((n = f.Read(b, 0, b.Length)) > 0)
{
md5.TransformBlock(b, 0, n, garbage, 0);
}
md5.TransformFinalBlock(b, 0, 0);
return BitConverter.ToString(md5.Hash).Replace("-", "");
}
}

but MD5 is not good any longer, so I will actually
recommend SHA256 instead:

public static string SHA256(string fnm)
{
HashAlgorithm sha256 = new SHA256Managed();
using(Stream f = new FileStream(fnm, FileMode.Open,
FileAccess.Read))
{
byte[] b = new byte[100000];
byte[] garbage = new byte[100000];
int n;
while((n = f.Read(b, 0, b.Length)) > 0)
{
sha256.TransformBlock(b, 0, n, garbage, 0);
}
sha256.TransformFinalBlock(b, 0, 0);
return BitConverter.ToString(sha256.Hash).Replace("-", "");
}
}

Arne
 
J

Jon Skeet [C# MVP]

Try:

         public static string MD5(string fnm)
         {
             HashAlgorithm md5 = new MD5CryptoServiceProvider();
             using(Stream f = new FileStream(fnm, FileMode.Open,
FileAccess.Read))
             {
                 byte[] b = new byte[100000];
                 byte[] garbage = new byte[100000];
                 int n;
                 while((n = f.Read(b, 0, b.Length)) >0)
                 {
                     md5.TransformBlock(b, 0, n, garbage, 0);
                 }
                 md5.TransformFinalBlock(b, 0, 0);
                 return BitConverter.ToString(md5.Hash)..Replace("-", "");
             }
         }

<snip>

Any reason for explicitly doing all the stream reading, rather than
letting ComputeHash do it all for you?

Jon
 
K

Kuma

Maybe this helps::

public static string GetMD5Hash(string filename)
{
MD5CryptoServiceProvider md5 = new
MD5CryptoServiceProvider();
FileStream stream = new FileStream(filename,
FileMode.Open, FileAccess.Read);
byte[] bytes = md5.ComputeHash(stream);
return BitConverter.ToString(bytes).Replace("-", "");
}
 
J

Jon Skeet [C# MVP]

Maybe this helps::

    public static string GetMD5Hash(string filename)
        {
             MD5CryptoServiceProvider md5 = new
MD5CryptoServiceProvider();
             FileStream stream = new FileStream(filename,
FileMode.Open, FileAccess.Read);
             byte[] bytes = md5.ComputeHash(stream);
             return BitConverter.ToString(bytes).Replace("-", "");
        }

One slight modification - I'd always put the stream in a "using"
statement so it gets closed at the end. But yes, that kind of thing :)

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top