Skip to content

Instantly share code, notes, and snippets.

@panagis
Created July 26, 2017 13:16
Show Gist options
  • Save panagis/f281f0908b894d619bea73e040a00794 to your computer and use it in GitHub Desktop.
Save panagis/f281f0908b894d619bea73e040a00794 to your computer and use it in GitHub Desktop.
[C#] An interface to the .NET SHA256 hashing function.
using System;
using System.Security.Cryptography;
using System.Text;
namespace Hashing
{
/// <summary>
// mySHA provides an interface to the SHA256 hashing function.
/// </summary>
public static class mySHA
{
/// <summary>
/// Computes the SHA256 hash value of the input string.
/// </summary>
/// <param name="String">UTF-16 encoded.</param>
/// <returns>Returns a Base64 representation of the hash bytes.</returns>
public static string ComputeHash(string String)
{
using (SHA256Managed sha = new SHA256Managed())
{
byte[] hashBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(String), 0,
Encoding.UTF8.GetByteCount(String));
return Convert.ToBase64String(hashBytes);
}
}
/// <summary>
/// Computes the SHA256 hash value of the input string.
/// The output string will be the hash value of the Salt+String+Salt.
/// </summary>
/// <param name="String">UTF-16 encoded.</param>
/// <returns>Returns a Base64 representation of the hash bytes.</returns>
public static string ComputeHash(string String, string Salt)
{
try
{
return ComputeHash(Salt + String + Salt);
}
catch (Exception)
{
throw;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment