C# SHA256 Hashing Explained
In C#, you can use the SHA256Managed class to implement the SHA256 encryption algorithm. Here is a simple example code:
using System;
using System.Security.Cryptography;
using System.Text;
public class Program
{
public static string CalculateSHA256(string input)
{
using (SHA256 sha256 = SHA256Managed.Create())
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
byte[] hash = sha256.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
}
public static void Main()
{
string input = "Hello, World!";
string hash = CalculateSHA256(input);
Console.WriteLine("Input: " + input);
Console.WriteLine("SHA256 Hash: " + hash);
}
}
In the above example, the CalculateSHA256 method takes a string input and returns its SHA256 hash value. The main function demonstrates how to use this method to calculate the SHA256 hash and output the result. You can replace the content in the input variable with the string you want to encrypt.