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