CSharp · Console
using System;
using System.Text;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
// input, plain text here
string plainText = "Technology crowds";
string hashedData = ComputeStringToSha256Hash(plainText);
// Ouput of plaintext to Sha256Hash
Console.WriteLine(string.Format("The SHA256 hash of {0} is: {1}",plainText,hashedData));
}
static string ComputeStringToSha256Hash(string plainText)
{
// Create a SHA256 hash from string
using (SHA256 sha256Hash = SHA256.Create())
{
// Computing Hash - returns here byte array
byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(plainText));
// now convert byte array to a string
StringBuilder stringbuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
stringbuilder.Append(bytes[i].ToString("x2"));
}
return stringbuilder.ToString();
}
}
}