我正试图编写一个函数,以这种方式对字符串进行sha512哈希处理:
public string SHA512(string input)
{
string hash;
~magic~
return hash;
}
魔法应该是什么?
你的代码是正确的,但是你应该释放SHA512Managed实例:
using (SHA512 shaM = new SHA512Managed())
{
hash = shaM.ComputeHash(data);
}
512位等于64字节。
要将字符串转换为字节数组,您需要指定编码方式。如果您想创建一个哈希码,UTF8是可以的:
var data = Encoding.UTF8.GetBytes("text");
using (...
这是我其中一个项目的内容:
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();
}
}
512/8 = 64
,因此64是正确的大小。也许你想在SHA512算法之后将其转换为十六进制。
Encoding.UTF8.GetByteCount("text")
的方法。 - Carsten Schüttepublic 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("-", "");
}
r = (l ? r.ToLowerInvariant() : r);
}
catch
{
}
return r;
}
您可以使用BouncyCastle替换WinCrypt-API中的System.Security.Cryptography。
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;
}
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;
}
简单明了:
using (SHA512 sha512 = new SHA512Managed())
{
password = Encoding.UTF8.GetString(sha512.ComputeHash(Encoding.UTF8.GetBytes(password)));
}
我不确定你为什么期望128。
一个字节有8位。64个字节。8 * 64 = 512位哈希值。
我将其制作成了一个扩展方法,放在我的ExtensionUtility.cs类中。
public static string SHA512(this string plainText)
{
using (SHA512 shaM = new SHA512Managed())
{
var buffer = Encoding.UTF8.GetBytes(plainText);
var hashedInputBytes = shaM.ComputeHash(buffer);
return BitConverter.ToString(hashedInputBytes).Replace("-", "");
}
}
您可以使用System.Security.Cryptography.SHA512类
这里有一个例子,直接来自MSDN
byte[] data = new byte[DATA_SIZE];
byte[] result;
SHA512 shaM = new SHA512Managed();
result = shaM.ComputeHash(data);
var data = Encoding.UTF8.GetBytes(foo)
代替var data = Encoding.UTF8.GetByteCount(foo)
。注意保持原意,使语言更通俗易懂。 - luiscubalWarning SYSLIB0021 'SHA512Managed'已过时:派生的加密类型已过时。请改用基类型上的Create方法。'
如何在.NET 6中使用它? 在https://dev59.com/4lEG5IYBdhLWcg3wOXKV中有单独的问题。 - Andrus