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

 

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

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.

Related Posts

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post

How to create a dynamic image from multiple images using ImageProcessor

This post shows you how I created a handler to create dynamic header images for my umbraco website u…

Read Post

How to send Slack messages programmatically using C#

In this post I show you how you can send Slack messages programmatically using C#

Read Post

How to include scripts from partial views in MVC and Umbraco

This post will show you how you can include scripts from inside an MVC partial view rather than havi…

Read Post