SHA 256 and SHA 512 hash examples

Posted written by Paul Seal on December 03, 2015 C#

What is this?

This post contains examples of how to generate a SHA-256 and SHA-512 hash key with the examples in C# and VB.NET

This solution matches the expected result for Ingenico's implementation for their payment gateway.

C# - UPDATED

using System;
using System.Text;
using System.Security.Cryptography;
using CodeShare.Cryptography;

namespace CodeShare.Cryptography
{
    public static class SHA
    {

        public static string GenerateSHA256String(string inputString)
        {
            SHA256 sha256 = SHA256Managed.Create();
            byte[] bytes = Encoding.UTF8.GetBytes(inputString);
            byte[] hash = sha256.ComputeHash(bytes);
            return GetStringFromHash(hash);
        }

        public static string GenerateSHA512String(string inputString)
        {
            SHA512 sha512 = SHA512Managed.Create();
            byte[] bytes = Encoding.UTF8.GetBytes(inputString);
            byte[] hash = sha512.ComputeHash(bytes);
            return GetStringFromHash(hash);
        }

        private static string GetStringFromHash(byte[] hash)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                result.Append(hash[i].ToString("X2"));
            }
            return result.ToString();
        }

    }
}

 

Usage Example

  public void UsageExample()
  {
    Console.WriteLine(SHA.GenerateSHA256String("abc"));
    //returns BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

    Console.WriteLine(SHA.GenerateSHA512String("abc"));
    //returns DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F
  }

Have you gone through this course on Pluralsight yet? Blockchain – Principles and Practices

VB.NET

Imports System.Text
Imports System.Security.Cryptography

Namespace CodeShare.Cryptography
  Public Class SHA

    Public Shared Function GenerateSHA256String(ByVal inputString) As String
      Dim sha256 As SHA256 = SHA256Managed.Create()
      Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputString)
      Dim hash As Byte() = sha256.ComputeHash(bytes)
      Dim stringBuilder As New StringBuilder()

      For i As Integer = 0 To hash.Length - 1
        stringBuilder.Append(hash(i).ToString("X2"))
      Next

      Return stringBuilder.ToString()
    End Function

    Public Shared Function GenerateSHA512String(ByVal inputString) As String
      Dim sha512 As SHA512 = SHA512Managed.Create()
      Dim bytes As Byte() = Encoding.UTF8.GetBytes(inputString)
      Dim hash As Byte() = sha512.ComputeHash(bytes)
      Dim stringBuilder As New StringBuilder()

      For i As Integer = 0 To hash.Length - 1
        stringBuilder.Append(hash(i).ToString("X2"))
      Next

      Return stringBuilder.ToString()
   End Function
 End Class
End Namespace

 

Please feel free to comment if you would like to propose any improvements for a better solution.

Paul