Calculate file checksum
You might have noticed by now that a lot of websites list their files checksum values in their downloads section. Checksums are extremely useful when you want to verify that the file you have downloaded from another source is indeed the same file that is hosted on the official website and that it has not been altered in any way.
For this very reason I’ve put together a method that will generate the checksum of the file of your choice. Simply provide the location of the file and the algorithm you wish to compute the checksum with.
private string GetFileChecksum(string file, HashAlgorithm algorithm)
string result = string.Empty;
using (FileStream fs = File.OpenRead(file))
result = BitConverter.ToString(algorithm.ComputeHash(fs)).ToLower().Replace("-", "");
string MD5checksum = GetFileChecksum("c:\\myfile.exe", new MD5CryptoServiceProvider());
string SHA1checksum = GetFileChecksum("c:\\myfile.exe", new SHA1CryptoServiceProvider());
string SHA256checksum = GetFileChecksum("c:\\myfile.exe", new SHA256CryptoServiceProvider());
string SHA512checksum = GetFileChecksum("c:\\myfile.exe", new SHA512CryptoServiceProvider());
I’m just learning some coding, so i was trying to create a program that does that, but i dont have enough knowledge :) i try to complete some of your posts! Any help is welcome!
The easiest way to compile C# (and VB.NET) source code, as well as to create applications, would be through the use of the Visual Studio IDE from Microsoft. It comes with a lot of features that speed up the development, allows for automatic error checking and so on. Best of all they even have a free version of the IDE called Visual Studio Express.
Other than that start with the basics first as they are very important in order to understand what are you doing, which is crucial if you want to progress.
The information at http://msdn.microsoft.com/en-us/library/67ef8sbd.aspx should come in handy as well if you want to check it out.
How can we compile that? How to provide the algorithm? in what way?
The code above is in C#. If you are unfamiliar on how to compile the code you could use Microsoft’s File Checksum Integrity Verifier which will do the work for you. If you want to use the tool I mentioned the usage is fairly simple.
Example for single file check: fciv.exe -add <file> -both. This will calculate both the md5 and sha1 hashes for you.
For more detailed information about its usage simply run the program and it will display the help menu.