Created
November 5, 2018 23:11
-
-
Save obrassard/766951b3c65984273ce4b6475e568cf5 to your computer and use it in GitHub Desktop.
SHA-256 & SHA-512 Hashing Utility in C#
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.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