MD5 Hashing a file for download checks

B

Benny Raymond

I've nearly finished my personal auto updater and the only thing left to
implement is a hash check against the downloaded file to make sure it's
good before extracting it. Right now I havn't released it so I've
tested that the selected update components download correctly and
extract and that the program starts back up as the new version...

I've googled for about a half hour and couldn't figure out how to
generate an MD5 hash for both storing in my website's database and for
generating on the downloaded file to compare against the xml file I
download in the first place in order to download the updates.

So, does anyone have a quick tutorial on how to generate an MD5 hash on
a file?

Thanks in advance,
Benny
 
B

Benny Raymond

is this what i should be doing?

public class MD5Check
{
private string _hash;
private string _file;
public MD5Check(string file, string hash)
{
_hash = hash;
_file = file;
}

public bool Verify()
{
MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
if (File.Exists(_file))
{
FileStream fs = File.OpenRead(_file);
byte[] fileHash = csp.ComputeHash(fs);
fs.Close();

// convert to string
string computed = BitConverter.ToString(fileHash);
if (computed == _hash)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Benny,

Have you checked out the MD5CryptoServiceProvider class? It takes a
byte array and hashes the contents.

Hope this helps.
 
A

Abubakar

Hi,
check this:
.....
using System.Security.Cryptography;

.....

FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read,
FileShare.Read);
byte [] HashCode = new MD5CryptoServiceProvider().ComputeHash(fs);
fs.Close();
....

at this point HashCode byte array has the code. Once u receive the file,
perform this operations again and check against the has bits generated for
the received file.

Ab.
http://joehacker.blogspot.com
 

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