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

 

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
stkrs

  • Custom stickers for startups, artists and brands.
  • Bespoke easy-apply, removable, custom brand stickers printed in the UK.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.