Skip to content

Instantly share code, notes, and snippets.

@tiesont
Last active December 22, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiesont/6411627 to your computer and use it in GitHub Desktop.
Save tiesont/6411627 to your computer and use it in GitHub Desktop.
C# Hashing utility, as used in Monkey.CMS.
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace Utilities
{
/// <summary>
/// Simple utility class for generating hashes for string values.
/// </summary>
public class HashUtility
{
/// <summary>
/// Gets the hash.
/// </summary>
/// <remarks>This method uses UTF-8 encoding and is hashed using the SHA1 algorithm.</remarks>
/// <param name="text">The text.</param>
/// <returns></returns>
public static string GetHash(string text)
{
return GetHash(text, HashType.SHA1, false, true);
}
/// <summary>
/// Gets the hash.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="hashType">
/// Type of hash algorithm used to hash the text. Integer value is cast to an enumerated value; <see cref="HashType"/>
/// </param>
/// <returns></returns>
public static string GetHash(string text, int hashType)
{
return GetHash(text, (HashType)hashType, false, true);
}
/// <summary>
/// Gets the hash.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="hashType">
/// Type of hash algorithm used to hash the text; <see cref="HashType"/>
/// </param>
/// <param name="forceUpperCase">if set to <c>true</c>, forces uppercase characters in the hash-string.</param>
/// <returns></returns>
/// <remarks>
/// This method uses UTF-8 encoding.
/// </remarks>
public static string GetHash(string text, HashType hashType, bool forceUpperCase = false)
{
return GetHash(text, hashType, forceUpperCase, true);
}
/// <summary>
/// Gets the hash.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="hashType">Type of hash algorithm used to hash the text.</param>
/// <param name="forceUpperCase">if set to <c>true</c>, forces uppercase characters in the hash-string.</param>
/// <param name="unicode">if set to <c>true</c>, UTF-8 encoded bytes are extracted from the source string; otherwise, bytes are ASCII encoded.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">Invalid hash type;hashType</exception>
/// <remarks>
/// This method uses UTF-8 encoding by default.
/// </remarks>
public static string GetHash(string text, HashType hashType, bool forceUpperCase = false, bool unicode = true)
{
HashAlgorithm _algorithm;
switch (hashType)
{
case HashType.MD5:
_algorithm = MD5.Create();
break;
case HashType.SHA1:
_algorithm = SHA1.Create();
break;
case HashType.SHA256:
_algorithm = SHA256.Create();
break;
case HashType.SHA512:
_algorithm = SHA512.Create();
break;
default:
throw new ArgumentException("Invalid hash type", "hashType");
}
byte[] bytes = unicode ? Encoding.UTF8.GetBytes(text) : Encoding.ASCII.GetBytes(text);
byte[] hash = _algorithm.ComputeHash(bytes);
return string.Concat(hash.Select(b => b.ToString(forceUpperCase ? "X2" : "x2")));
}
}
public enum HashType
{
NONE,
MD5,
SHA1,
SHA256,
SHA512
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment