Tag Archives: checksum
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());
Posted in C#.
Tagged C#, checksum, csharp, HashAlgorithm, snippet, System.Security.Cryptography, winforms