Hash string in c#

前端 未结 7 1521
栀梦
栀梦 2020-12-07 10:30

I have a problem when trying get a hash string in c#.

I already tried a few websites, but most of them are using files to get the hash. Others that are

7条回答
  •  一生所求
    2020-12-07 10:56

    If performance is not a major concern, you can also use any of these methods:
    (In case you wanted the hash string to be in upper case, replace "x2" with "X2".)

    public static string SHA256ToString(string s) 
    {
        using (var alg = SHA256.Create())
            return string.Join(null, alg.ComputeHash(Encoding.UTF8.GetBytes(s)).Select(x => x.ToString("x2")));
    }
    

    or:

    public static string SHA256ToString(string s)
    {            
        using (var alg = SHA256.Create())
            return alg.ComputeHash(Encoding.UTF8.GetBytes(s)).Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.ToString("x2"))).ToString();
    }
    

提交回复
热议问题