C# Sha512 hash example

This is a brief demonstration of how to generate a Sha512 hash in C#. Make sure to include the using at the top of your code because we are using the System.Security.Cryptography methods.

using System.Security.Cryptography;

Here is the example function:

        public static string GetSha512(string text)
        {
            if (text == null)
            {
                return string.Empty;
            }

            byte[] message = System.Text.Encoding.ASCII.GetBytes(text);
            byte[] hashValue = GetSha512(message);

            string hashString = string.Empty;
            foreach (byte x in hashValue)
            {
                hashString += string.Format("{0:x2}", x);
            }

            return hashString;

        }

        private static byte[] GetSha512(byte[] message)
        {
            SHA512Managed hashString = new SHA512Managed();
            return hashString.ComputeHash(message);
        }

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.