Calculate a MD5 hash from a string

后端 未结 16 2107
独厮守ぢ
独厮守ぢ 2020-11-28 21:06

I use the following C# code to calculate a MD5 hash from a string. It works well and generates a 32-character hex string like this: 900150983cd24fb0d6963f7d28e17f72

16条回答
  •  萌比男神i
    2020-11-28 22:02

    I suppose it is better to use UTF-8 encoding in the string MD5.

    public static string MD5(this string s)
    {
        using (var provider = System.Security.Cryptography.MD5.Create())
        {
            StringBuilder builder = new StringBuilder();                           
    
            foreach (byte b in provider.ComputeHash(Encoding.UTF8.GetBytes(s)))
                builder.Append(b.ToString("x2").ToLower());
    
            return builder.ToString();
        }
    }
    

提交回复
热议问题