C# Sha512 hash example

Here a quick example how to create a Sha512 hash in C#. We are using the functions from System.Security.Cryptography, so don’t forget to add the using at top of you file.

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.