Created
July 26, 2017 13:16
-
-
Save panagis/f281f0908b894d619bea73e040a00794 to your computer and use it in GitHub Desktop.
[C#] An interface to the .NET SHA256 hashing function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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