create a sha512 hash from a string in csharp

To create a SHA512 hash from a string in C#, you can use the SHA512 class from the System.Security.Cryptography namespace. Here's an example code snippet:

main.cs
using System.Security.Cryptography;
using System.Text;

public static string GetSHA512Hash(string input)
{
    using (SHA512 sha512 = SHA512.Create())
    {
        byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        byte[] hashBytes = sha512.ComputeHash(inputBytes);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hashBytes.Length; i++)
        {
            sb.Append(hashBytes[i].ToString("X2"));
        }
        return sb.ToString();
    }
}
484 chars
18 lines

The GetSHA512Hash method takes a string input and returns its SHA512 hash as a string. The method first converts the input string to a byte array and then uses the SHA512.Create() method to create an instance of the SHA512 class. It then calls the ComputeHash method on the SHA512 instance passing in the input bytes to compute the hash. Finally, it converts the hash bytes to a hex string and returns it.

gistlibby LogSnag