using System.Security.Cryptography; string inputString = "Hello World"; byte[] inputBytes = Encoding.ASCII.GetBytes(inputString); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] hashBytes = md5.ComputeHash(inputBytes); string hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); Console.WriteLine("MD5 hash: " + hashString);
using System.Security.Cryptography; string filePath = @"C:\example.txt"; using (FileStream stream = new FileStream(filePath, FileMode.Open)) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); byte[] hashBytes = md5.ComputeHash(stream); string hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); Console.WriteLine("MD5 hash: " + hashString); }This example shows how to generate the MD5 hash of a file using the MD5CryptoServiceProvider class. The file is opened using the FileStream class, and then the ComputeHash method is used to generate the hash. The package library for the MD5CryptoServiceProvider class is the System.Security.Cryptography package, which is included in the .NET framework.