How can I SHA512 a string in C#?

2019-01-23 11:21发布

问题:

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!

回答1:

Your code is correct, but you should dispose of the SHA512Managed instance:

using (SHA512 shaM = new SHA512Managed())
{
   hash = shaM.ComputeHash(data);
}

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:

var data = Encoding.UTF8.GetBytes("text");    
using (...


回答2:

This is from one of my projects:

public static string SHA512(string input)
{
    var bytes = System.Text.Encoding.UTF8.GetBytes(input);
    using (var hash = System.Security.Cryptography.SHA512.Create())
    {
        var hashedInputBytes = hash.ComputeHash(bytes);

        // Convert to text
        // StringBuilder Capacity is 128, because 512 bits / 8 bits in byte * 2 symbols for byte 
        var hashedInputStringBuilder = new System.Text.StringBuilder(128);
        foreach (var b in hashedInputBytes)
            hashedInputStringBuilder.Append(b.ToString("X2"));
        return hashedInputStringBuilder.ToString();
    }
}

Please, note:

  1. SHA512 object is disposed ('using' section), so we do not have any resource leaks.
  2. StringBuilder is used for efficient hex string building.


回答3:

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?



回答4:

I'm not sure why you are expecting 128.

8 bits in a byte. 64 bytes. 8 * 64 = 512 bit hash.



回答5:

From the MSDN Documentation:
The hash size for the SHA512Managed algorithm is 512 bits.



回答6:

You could use the System.Security.Cryptography.SHA512 class

MSDN on SHA512

Here is an example, straigt from the MSDN

byte[] data = new byte[DATA_SIZE];
byte[] result;
SHA512 shaM = new SHA512Managed();
result = shaM.ComputeHash(data);


回答7:

Instead of WinCrypt-API using System.Security.Cryptography, you can also use BouncyCastle:

public static byte[] SHA512(string text)
{
    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(text);

    Org.BouncyCastle.Crypto.Digests.Sha512Digest digester = new Org.BouncyCastle.Crypto.Digests.Sha512Digest();
    byte[] retValue = new byte[digester.GetDigestSize()];
    digester.BlockUpdate(bytes, 0, bytes.Length);
    digester.DoFinal(retValue, 0);
    return retValue;
}

If you need the HMAC-version (to add authentication to the hash)

public static byte[] HmacSha512(string text, string key)
{
    byte[] bytes = Encoding.UTF8.GetBytes(text);

    var hmac = new Org.BouncyCastle.Crypto.Macs.HMac(new Org.BouncyCastle.Crypto.Digests.Sha512Digest());
    hmac.Init(new Org.BouncyCastle.Crypto.Parameters.KeyParameter(System.Text.Encoding.UTF8.GetBytes(key)));

    byte[] result = new byte[hmac.GetMacSize()];
    hmac.BlockUpdate(bytes, 0, bytes.Length);
    hmac.DoFinal(result, 0);

    return result;
}


回答8:

UnicodeEncoding UE = new UnicodeEncoding();            
        byte[] message = UE.GetBytes(password);
        SHA512Managed hashString = new SHA512Managed();
        string hexNumber = "";
        byte[]  hashValue = hashString.ComputeHash(message);
        foreach (byte x in hashValue)
        {
            hexNumber += String.Format("{0:x2}", x);
        }
        string hashData = hexNumber;


标签: c# hash