Skip to content

Instantly share code, notes, and snippets.

@obrassard
Created November 5, 2018 23:11
Show Gist options
  • Save obrassard/766951b3c65984273ce4b6475e568cf5 to your computer and use it in GitHub Desktop.
Save obrassard/766951b3c65984273ce4b6475e568cf5 to your computer and use it in GitHub Desktop.
SHA-256 & SHA-512 Hashing Utility in C#
using System;
using System.Text;
namespace System.Security.Cryptography;
{
public static class SHA
{
public static string sha256(string inputString)
{
SHA256 sha256 = SHA256.Create();
byte[] bytes = Encoding.UTF8.GetBytes(inputString);
byte[] hash = sha256.ComputeHash(bytes);
return GetStringFromHash(hash);
}
public static string sha512(string inputString)
{
SHA512 sha512 = SHA512.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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment