FileStream usage to create MD5 sum on a DLL that is loaded?

L

Lothar Behrens

Hi,

I have developed an update mechanism to pull new files and also update
recently downloaded files by the md5 sum. The recently downloaded
files are from a prior try to update files. This does hopefully reduce
traffic. The problem arises with the following code:

MD5 md5 = MD5.Create();
FileStream fs = new FileStream(file, FileMode.Open);
md5.ComputeHash(fs);

The new file to update has been downloaded previously. Now the old
file (DLL) is still loaded and thus the above code does not work.

How to create a checksum of a file that is loaded in a process?

I think copying the file and do the checksum with the copy is a
solution but not the best.

Thanks

Lothar
 
A

Arne Vajhøj

I have developed an update mechanism to pull new files and also update
recently downloaded files by the md5 sum. The recently downloaded
files are from a prior try to update files. This does hopefully reduce
traffic. The problem arises with the following code:

MD5 md5 = MD5.Create();
FileStream fs = new FileStream(file, FileMode.Open);
md5.ComputeHash(fs);

The new file to update has been downloaded previously. Now the old
file (DLL) is still loaded and thus the above code does not work.

How to create a checksum of a file that is loaded in a process?

I think copying the file and do the checksum with the copy is a
solution but not the best.

Try:

FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read,
FileShare.Read);

Note that the state of MD5 security today is so that it is
fine for detecting accidental wrong files but not sufficient for
detecting malicious bad files. If the latter is the purpose
then switch to SHA-256. Maybe swicth to SHA-256 anyway - it does
not really cost much extra and it is easier just to ban MD5
completely.

Arne
 

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