- using System.Security.Cryptography;
- using System.Text;
-
- public class MD5Encryptor
- {
- /// <summary>
- /// 计算输入字符串的 MD5 哈希值。
- /// </summary>
- /// <param name="input">要计算哈希值的字符串。</param>
- /// <returns>返回 MD5 哈希值的十六进制字符串表示。</returns>
- public static string ComputeMD5Hash(string input)
- {
- // 使用 MD5 哈希算法
- using (MD5 md5 = MD5.Create())
- {
- // 将输入字符串转换为字节数组
- byte[] inputBytes = Encoding.UTF8.GetBytes(input);
-
- // 计算字节数组的哈希值
- byte[] hashBytes = md5.ComputeHash(inputBytes);
-
- // 将哈希字节数组转换为十六进制字符串
- StringBuilder sb = new StringBuilder();
- foreach (byte b in hashBytes)
- {
- sb.Append(b.ToString("x2")); // 将每个字节转换为两位的十六进制表示
- }
-
- return sb.ToString();
- }
- }
- }
|