By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Getting MD5 of EXE

P: n/a
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
Jul 10 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Jul 10, 3:06*pm, greatbarrier86
<greatbarrie...@discussions.microsoft.comwrote:
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 tothe
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-ckinda helps, but i am
still stuck.
Open the file as a stream, and call MD5.ComputeHash.

Jon
Jul 10 '08 #2

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:07**********************************@microsof t.com...
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

Jul 10 '08 #3

P: n/a
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"greatbarrier86" <gr************@discussions.microsoft.comwrote in message
news:07**********************************@microsof t.com...
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


Jul 10 '08 #4

P: n/a
greatbarrier86 wrote:
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
Jul 14 '08 #5

P: n/a
greatbarrier86 wrote:
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
Jul 14 '08 #6

P: n/a
On Jul 14, 3:41*am, Arne Vajhøj <a...@vajhoej.dkwrote:
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
Jul 14 '08 #7

P: n/a
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("-", "");
}
Jul 14 '08 #8

P: n/a
On Jul 14, 7:51*am, Kuma <kumasa...@hotmail.comwrote:
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
Jul 14 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.