在C#中,可以使用System.Security.Cryptography命名空间中的SHA256类来实现MessageDigest功能。下面是一个简单的示例:
using System;using System.Security.Cryptography;using System.Text;class Program{ static void Main() { string input = "Hello, world!"; using (SHA256 sha256 = SHA256.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha256.ComputeHash(inputBytes); string hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); Console.WriteLine("MessageDigest (SHA-256) of input: " + hashString); } }}在这个示例中,我们首先创建了一个SHA256实例,然后将输入字符串转换为字节数组,并使用ComputeHash方法计算出MessageDigest。最后,我们将MessageDigest转换为十六进制字符串并输出到控制台。


