// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Security.Cryptography; namespace E { public class Program { public static string MD5(string fnm) { HashAlgorithm md5 = new MD5CryptoServiceProvider(); using(Stream f = new FileStream(fnm, FileMode.Open, FileAccess.Read)) { byte[] b = new byte[100000]; byte[] garbage = new byte[100000]; int n; while((n = f.Read(b, 0, b.Length)) > 0) { md5.TransformBlock(b, 0, n, garbage, 0); } md5.TransformFinalBlock(b, 0, 0); return BitConverter.ToString(md5.Hash).Replace("-", ""); } } public static string SHA256(string fnm) { HashAlgorithm sha256 = new SHA256Managed(); using(Stream f = new FileStream(fnm, FileMode.Open, FileAccess.Read)) { byte[] b = new byte[100000]; byte[] garbage = new byte[100000]; int n; while((n = f.Read(b, 0, b.Length)) > 0) { sha256.TransformBlock(b, 0, n, garbage, 0); } sha256.TransformFinalBlock(b, 0, 0); return BitConverter.ToString(sha256.Hash).Replace("-", ""); } } public static void Main(string[] args) { Console.WriteLine(MD5(@"C:\zlib-1.2.3.tar")); Console.WriteLine(SHA256(@"C:\zlib-1.2.3.tar")); } } }