I am trying to write a function to take a string and sha512 it like so?
public string SHA512(string input)
{
string hash;
~magic~
return hash;
}
What should the magic be?
P.S.
I tried the following, but hash
kept ending up as 64bytes long (not 128 as expected).
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
byte[] hash;
SHA512 shaM = new SHA512Managed();
hash = shaM.ComputeHash(data);
Thanks in advance!
You could use the System.Security.Cryptography.SHA512 class
MSDN on SHA512
Here is an example, straigt from the MSDN
This is from one of my projects:
Please, note:
Instead of WinCrypt-API using System.Security.Cryptography, you can also use BouncyCastle:
If you need the HMAC-version (to add authentication to the hash)
Your code is correct, but you should dispose of the SHA512Managed instance:
512 bits are 64 bytes.
To convert a string to a byte array, you need to specify an encoding. UTF8 is okay if you want to create a hash code:
512/8 = 64
, so 64 is indeed the correct size. Perhaps you want to convert it to hexadecimal after the SHA512 algorithm.See also: How do you convert Byte Array to Hexadecimal String, and vice versa?