我怎样才能在C#中使用SHA512字符串?

时间:2012-07-06 18:28:02

标签: c# hash

我正在尝试编写一个函数来获取字符串和sha512就像这样?

public string SHA512(string input)
{
     string hash;

     ~magic~

     return hash;
}

魔法应该是什么?

11 个答案:

答案 0 :(得分:53)

您的代码是正确的,但您应该丢弃SHA512Managed实例:

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

512位是64字节。

要将字符串转换为字节数组,您需要指定编码。如果要创建哈希码,UTF8就可以了:

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

答案 1 :(得分:13)

这来自我的一个项目:

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();
    }
}

请注意:

  1. 处理SHA512对象('使用'部分),因此我们没有任何资源泄漏。
  2. StringBuilder用于高效的十六进制字符串构建。

答案 2 :(得分:8)

512/8 = 64,所以64确实是正确的大小。也许你想在 SHA512算法之后将其转换为十六进制

另请参阅:How do you convert Byte Array to Hexadecimal String, and vice versa?

答案 3 :(得分:1)

我不确定你为什么期待128.

一个字节中的8位。 64个字节。 8 * 64 = 512位散列。

答案 4 :(得分:1)

来自MSDN Documentation
SHA512Managed算法的哈希大小为512位。

答案 5 :(得分:1)

您可以使用System.Security.Cryptography.SHA512类

MSDN on SHA512

这是一个例子,来自MSDN的straigt

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

答案 6 :(得分:1)

使用System.Security.Cryptography代替WinCrypt-API,您也可以使用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;
}

如果您需要HMAC版本(为哈希添加身份验证)

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;
}

答案 7 :(得分:1)

保持简单:

using (SHA512 sha512 = new SHA512Managed())
{
    password = Encoding.UTF8.GetString(sha512.ComputeHash(Encoding.UTF8.GetBytes(password)));
}

答案 8 :(得分:0)

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;

答案 9 :(得分:0)

您可以尝试以下行:

public static string GenSHA512(string s, bool l = false)
{
    string r = "";
    try
    {
        byte[] d = Encoding.UTF8.GetBytes(s);
        using (SHA512 a = new SHA512Managed())
        {
            byte[] h = a.ComputeHash(d);
            r = BitConverter.ToString(h).Replace("-", "");
        }
        if (l)
            r = r.ToLower();
    }
    catch
    {

    }
    return r;
}
  1. 放在最后
  2. 很安全
  3. 支持小写

答案 10 :(得分:-1)

我使用了以下

public static string ToSha512(this string inputString)
{
        if (string.IsNullOrWhiteSpace(inputString)) return string.Empty;
        using (SHA512 shaM = new SHA512Managed())
        {
            return Convert.ToBase64String(shaM.ComputeHash(Encoding.UTF8.GetBytes(inputString)));
        }
}