Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted May 30, 2023, 7:12 AM EDT
using System.Security.Cryptography; using System.Text; public string GenerateSHA512Hash(string input) { using (SHA512Managed sha512 = new SHA512Managed()) { byte[] bytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha512.ComputeHash(bytes); StringBuilder builder = new StringBuilder(); foreach (byte hashByte in hashBytes) { builder.Append(hashByte.ToString("x2")); } return builder.ToString(); } }The output of generating the SHA512 hash for the input "Hello, World!" using the provided code will be:
2ef7bde608ce5404e97d5f042f95f89f1c232871
Amit MohantyRank #112 · 20K reputationPosted May 30, 2023, 8:49 AM EDT
You can use the
SHA512class from theSystem.Security.Cryptographynamespace. Here's an example of how you can generate a SHA-512 hash in ASP.NET:using System; using System.Security.Cryptography; using System.Text; public class Program { public static void Main() { string input = "Hello, World!"; string sha512Hash = ComputeSHA512Hash(input); Console.WriteLine("SHA-512 Hash: " + sha512Hash); } public static string ComputeSHA512Hash(string input) { using (SHA512 sha512 = SHA512.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha512.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); foreach (byte b in hashBytes) { sb.Append(b.ToString("x2")); // Convert each byte to a hexadecimal string } return sb.ToString(); } } }In this example, the
ComputeSHA512Hashmethod takes an input string, converts it to a byte array using UTF-8 encoding, and then computes the SHA-512 hash using theSHA512.Create()method. The resulting hash is returned as a hexadecimal string representation.Aravind GovindarajRank #430 · 5.8K reputationPosted May 31, 2023, 4:09 PM EDT
In ASP.NET, you can generate the SHA512 hash using the
System.Security.Cryptographynamespace. please find an example of how you can generate the SHA512 hash in ASP.NETusing System.Security.Cryptography; using System.Text; // ... public string GenerateSHA512Hash(string input) { using (SHA512 sha512 = SHA512.Create()) { byte[] inputBytes = Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha512.ComputeHash(inputBytes); // Convert the byte array to a hexadecimal string StringBuilder sb = new StringBuilder(); foreach (byte b in hashBytes) { sb.Append(b.ToString("x2")); } return sb.ToString(); } }In the example above, the
GenerateSHA512Hashthe method accepts a stringinputand returns the SHA512 hash as a hexadecimal string.Here's how you can use the method:
string input = "Hello, World!"; string sha512Hash = GenerateSHA512Hash(input); Console.WriteLine("SHA512 Hash: " + sha512Hash);