This post will discuss how to generate the SHA512 hash for the input data in C#.

We can use the SHA512 class to compute the SHA512 hash. To compute the SHA512 hash value of a string, first initialize a SHA512 hash object using the SHA512.Create() method and convert the given string into a byte array with the Encoding.GetBytes() method. Then compute the hash value for the specified byte array using the ComputeHash() method. Finally, convert the byte array to a 128-character, hexadecimal-formatted string. This approach is demonstrated below:

Download  Run Code

Output:

31FCBF83B5BA1C6A378A46645558A961FFB94FBB61F502D2F3BECB12FEAE63D9433851E8B83EA56D143BDFFC83874DB881F8E5E6AC2FC787ED628D4E7E7BF6F0

 
On .NET 5 and above, we can use the Convert.ToHexString() method to convert the byte array to its equivalent string representation.

Download  Run Code

Output:

31FCBF83B5BA1C6A378A46645558A961FFB94FBB61F502D2F3BECB12FEAE63D9433851E8B83EA56D143BDFFC83874DB881F8E5E6AC2FC787ED628D4E7E7BF6F0

That’s all about generating the SHA512 hash for the input data in C#.