Skip to content

Instantly share code, notes, and snippets.

@agehlot
Created March 13, 2021 18:35
Show Gist options
  • Save agehlot/7fae15538ed68687e139a0fba5c81515 to your computer and use it in GitHub Desktop.
Save agehlot/7fae15538ed68687e139a0fba5c81515 to your computer and use it in GitHub Desktop.
Helper class to generate one way hash value of given parameter
// ***********************************************************************
// Assembly : Core.Foundation.SitecoreExtensions
// Author : agehlot
// Created : 10-24-2020
//
// Last Modified By : agehlot
// Last Modified On : 10-24-2020
// ***********************************************************************
// <copyright file="HashHelper.cs" company="">
// Copyright © 2019
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Security.Cryptography;
using System.Text;
namespace Core.Foundation.SitecoreExtensions.Helpers
{
/// <summary>
/// Class HashHelper.
/// </summary>
public class HashHelper
{
/// <summary>
/// Computes the hash.
/// </summary>
/// <param name="plainText">The plain text.</param>
/// <param name="saltkey">The saltkey.</param>
/// <param name="hashAlgorithm">The hash algorithm.</param>
/// <returns>System.String.</returns>
public static string ComputeHash(string plainText, string saltkey, string hashAlgorithm = "SHA512")
{
var saltBytes = Convert.FromBase64String(saltkey);
// If salt is not specified, generate it on the fly.
if (saltBytes == null)
{
// Define min and max salt sizes.
int minSaltSize = 4;
int maxSaltSize = 8;
// Generate a random number for the size of the salt.
Random random = new Random();
int saltSize = random.Next(minSaltSize, maxSaltSize);
// Allocate a byte array, which will hold the salt.
saltBytes = new byte[saltSize];
// Initialize a random number generator.
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
// Fill the salt with cryptographically strong byte values.
rng.GetNonZeroBytes(saltBytes);
Convert.ToBase64String(saltBytes);
}
// Convert plain text into a byte array.
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
// Allocate array, which will hold plain text and salt.
byte[] plainTextWithSaltBytes =
new byte[plainTextBytes.Length + saltBytes.Length];
// Copy plain text bytes into resulting array.
for (int i = 0; i < plainTextBytes.Length; i++)
plainTextWithSaltBytes[i] = plainTextBytes[i];
// Append salt bytes to the resulting array.
for (int i = 0; i < saltBytes.Length; i++)
plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
HashAlgorithm hash;
// Make sure hashing algorithm name is specified.
if (hashAlgorithm == null)
hashAlgorithm = "";
// Initialize appropriate hashing algorithm class.
hash = GetHashMethod(hashAlgorithm);
// Compute hash value of our plain text with appended salt.
byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);
// Create array which will hold hash and original salt bytes.
byte[] hashWithSaltBytes = new byte[hashBytes.Length +
saltBytes.Length];
// Copy hash bytes into resulting array.
for (int i = 0; i < hashBytes.Length; i++)
hashWithSaltBytes[i] = hashBytes[i];
// Append salt bytes to the result.
for (int i = 0; i < saltBytes.Length; i++)
hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
// Convert result into a base64-encoded string.
string hashValue = Convert.ToBase64String(hashWithSaltBytes);
// Return the result.
return hashValue;
}
/// <summary>
/// Gets the hash method.
/// </summary>
/// <param name="hashAlgorithm">The hash algorithm.</param>
/// <returns>HashAlgorithm.</returns>
private static HashAlgorithm GetHashMethod(string hashAlgorithm)
{
HashAlgorithm hash;
switch (hashAlgorithm.ToUpper())
{
case "SHA1":
hash = new SHA1Managed();
break;
case "SHA256":
hash = new SHA256Managed();
break;
case "SHA384":
hash = new SHA384Managed();
break;
case "SHA512":
hash = new SHA512Managed();
break;
default:
hash = new MD5CryptoServiceProvider();
break;
}
return hash;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment