Search code examples
c#.netcryptographysha512

How to calculate SHA512/256 in .Net 6?


How to calculate SHA512/256 or SHA512/224 without using external library?

In .Net 6, SHA512 hash can be calculated(documentation). Here is my example:

    public string GetHashStringSHA512(string data)
    {
        using (SHA512 sha512 = SHA512.Create())
        {
            byte[] bytes = sha512.ComputeHash(Encoding.UTF8.GetBytes(data));

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                builder.Append(bytes[i].ToString("x2"));
            }
            return builder.ToString();
        }
    }

Solution

  • As noted in the comments, it appears that the .Net library has not implemented SHA512/256 or SHA512/224.

    To calculate SHA512/256 or SHA512/224 without using external library, the specification would need to be implemented. There's a document on the Cryptology ePrint Archive that includes some sample code. See also the NIST example. There are a variety of open source solutions as well to use as a starting point for your own code, such as the SHA512 library at wolfSSL that includes both SHA512/256 and SHA512/224.