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.
1 2 3 4 5 6 7 8 9 10 11 | 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("-", ""); } return result; } |
Examples:
1 2 3 4 5 6 7 8 9 10 11 | // a3ccfd0aa0b17fd23aa9fd0d84b86c05 string MD5checksum = GetFileChecksum("c:\\myfile.exe", new MD5CryptoServiceProvider()); // 89c19274ad51b6fbd12fb59908316088c1135307 string SHA1checksum = GetFileChecksum("c:\\myfile.exe", new SHA1CryptoServiceProvider()); // d4ffa4559a1e22167933772d82cf714cd4bb7a0e79511c2424e18bdb619d63a4 string SHA256checksum = GetFileChecksum("c:\\myfile.exe", new SHA256CryptoServiceProvider()); // 3906661b755f22965182f9d1b6f6175cda557c1c66722c60a68037960fc55373586d7db8b24690af212c34ac6df125f08932a24e47ae0f7d6cb2822d8bea4e0f 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.