public ComputeHash ( |
||
inputStream | ||
return | byte[] |
byte[] data = Encoding.UTF8.GetBytes("Hello world!"); using SHA256 sha256 = SHA256.Create(); byte[] hash = sha256.ComputeHash(data); string hashString = BitConverter.ToString(hash).Replace("-", "").ToLower(); Console.WriteLine(hashString); // output: 9ef46bbee79477d1b28f3e7d21d43a3f83176c5908e1b1e46b7df2db9aee711b
byte[] data = File.ReadAllBytes("image.jpg"); using MD5 md5 = MD5.Create(); byte[] hash = md5.ComputeHash(data); string hashString = BitConverter.ToString(hash).Replace("-", "").ToLower(); Console.WriteLine(hashString); // output: 757719edb57f56b5e43e6b508ebdc1c7This example reads the contents of an image file as a byte array and computes the MD5 hash value of the data. The resulting hash value is again converted to a string representation. Both examples use the using statement to ensure that the HashAlgorithm instance is properly disposed of after use. The package library used in these examples is the .NET Framework's built-in class library, which includes the System.Security.Cryptography namespace.