How can I SHA512 a string in C#?

后端 未结 10 1333
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 18:35

I am trying to write a function to take a string and sha512 it like so?

public string SHA512(string input)
{
     string hash;

     ~magic~

     return has         


        
10条回答
  •  无人及你
    2020-12-08 19:14

    You might try these lines:

    public static string GenSHA512(string s, bool l = false)
    {
        string r = "";
        try
        {
            byte[] d = Encoding.UTF8.GetBytes(s);
            using (SHA512 a = new SHA512Managed())
            {
                byte[] h = a.ComputeHash(d);
                r = BitConverter.ToString(h).Replace("-", "");
            }
            r = (l ? r.ToLowerInvariant() : r);
        }
        catch
        {
    
        }
        return r;
    }
    
    1. It is disposed at the end
    2. It's safe
    3. Supports lower case

提交回复
热议问题