using System.Security.Cryptography; using System.Text; public static string GetSHA256Hash(string input) { using (var sha256 = SHA256.Create()) { var bytes = Encoding.UTF8.GetBytes(input); var hash = sha256.ComputeHash(bytes); var stringBuilder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { stringBuilder.Append(hash[i].ToString("X2")); } return stringBuilder.ToString(); } }
using System.Security.Cryptography; using System.IO; public static byte[] GetSHA256Hash(byte[] input) { using (var sha256 = SHA256.Create()) { return sha256.ComputeHash(new MemoryStream(input)); } }This code example shows how to compute the SHA256 hash of a byte array using the System.Security.Cryptography.SHA256Managed class in C#. Here, the input byte array is first wrapped in a MemoryStream object, and then passed directly to the ComputeHash method of SHA256Managed to produce the hash value. Package Library: The System.Security.Cryptography namespace is part of the .NET Standard Library, making it available in all .NET implementations, including .NET Framework, .NET Core, and Xamarin.