Monday, July 13, 2020

C# - How to compute hash value for the file

using( var md5 = MD5.Create() )
using( var sha256 = SHA256.Create() )
{
  using ( FileStream stream = File.OpenRead( @"c:\_ax\3of9.ttf" ) )
  {          
    byte[] hashMD5 = md5.ComputeHash( stream );
    Console.WriteLine( BitConverter.ToString( hashMD5 ) );  

    byte[] hashSHA256 = sha256.ComputeHash( stream );
    Console.WriteLine( BitConverter.ToString( hashSHA256 ) );  
  }
}
Output:
DB-E9-C0-88-6B-BB-A1-84-5D-4C-D8-95-60-DD-91-4E
E3-B0-C4-42-98-FC-1C-14-9A-FB-F4-C8-99-6F-B9-24-27-AE-41-E4-64-9B-93-4C-A4-95-99-1B-78-52-B8-55