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
}

 

Want to learn more about cryptography

If you want to learn more about cryptography, follow along with this video course Practical Cryptography in .NET

 

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

 

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 use Donut Caching in Umbraco and MVC

In this post I show you how easy Donut Caching is to use in Umbraco and MVC. It's very clever and no…

Read Post

How to start working with Umbraco v8

In this post I give you step by step instructions for getting Umbraco v8 forked, checked out and wor…

Read Post

How to add default values to Umbraco properties in the backoffice

This post shows you how you can easily add a default value to a property when using the editor in th…

Read Post

How to solve Multiple types were found that match the controller named x

This post shows you how to solve the error 'Multiple types were found that match the controller name…

Read Post