如何在 C# 中对字符串进行 SHA512 处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11367727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I SHA512 a string in C#?
提问by James
I am trying to write a function to take a string and sha512 it like so?
我正在尝试编写一个函数来获取字符串和 sha512 像这样吗?
public string SHA512(string input)
{
string hash;
~magic~
return hash;
}
What should the magic be?
魔法应该是什么?
采纳答案by Carsten Schütte
Your code is correct, but you should dispose of the SHA512Managed instance:
您的代码是正确的,但您应该处理 SHA512Managed 实例:
using (SHA512 shaM = new SHA512Managed())
{
hash = shaM.ComputeHash(data);
}
512 bits are 64 bytes.
512 位是 64 字节。
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:
要将字符串转换为字节数组,您需要指定编码。如果你想创建一个哈希码,UTF8 是可以的:
var data = Encoding.UTF8.GetBytes("text");
using (...
回答by Andrew T Finnell
I'm not sure why you are expecting 128.
我不确定你为什么期待 128。
8 bits in a byte. 64 bytes. 8 * 64 = 512 bit hash.
一个字节中的 8 位。64 字节。8 * 64 = 512 位哈希。
回答by luiscubal
512/8 = 64, so 64 is indeed the correct size. Perhaps you want to convert it to hexadecimal afterthe SHA512 algorithm.
512/8 = 64,所以 64 确实是正确的大小。也许您想在 SHA512 算法之后将其转换为十六进制。
See also: How do you convert Byte Array to Hexadecimal String, and vice versa?
回答by Joel Rondeau
From the MSDN Documentation:
The hash size for the SHA512Managed algorithm is 512 bits.
来自MSDN 文档:
SHA512Managed 算法的哈希大小为 512 位。
回答by Mare Infinitus
You could use the System.Security.Cryptography.SHA512 class
您可以使用 System.Security.Cryptography.SHA512 类
Here is an example, straigt from the MSDN
这是一个示例,直接来自 MSDN
byte[] data = new byte[DATA_SIZE];
byte[] result;
SHA512 shaM = new SHA512Managed();
result = shaM.ComputeHash(data);
回答by Mahesh.P
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;
回答by Nazar
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:
请注意:
- SHA512 object is disposed ('using' section), so we do not have any resource leaks.
- StringBuilder is used for efficient hex string building.
- SHA512 对象已处理(“使用”部分),因此我们没有任何资源泄漏。
- StringBuilder 用于高效的十六进制字符串构建。
回答by Stefan Steiger
Instead of WinCrypt-API using System.Security.Cryptography, you can also use BouncyCastle:
除了使用 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;
}
If you need the HMAC-version (to add authentication to the hash)
如果您需要 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;
}
回答by V.7
You might try these lines:
您可以尝试以下几行:
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;
}
- It is disposed at the end
- It's safe
- Supports lower case
- 它被放置在最后
- 它是安全的
- 支持小写
回答by Mohammad Dayyan
I used the following
我使用了以下
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)));
}
}

