我正在试着写一个函数来接受一个字符串并像这样sha512它?
public string SHA512(string input)
{
string hash;
~magic~
return hash;
}
魔术应该是什么呢?
发布于 2012-07-07 02:33:09
您的代码是正确的,但是您应该处理SHA512Managed实例:
using (SHA512 shaM = new SHA512Managed())
{
hash = shaM.ComputeHash(data);
}
512位是64字节。
要将字符串转换为字节数组,需要指定编码。如果你想创建一个哈希码,UTF8是可以的:
var data = Encoding.UTF8.GetBytes("text");
using (...
发布于 2016-09-21 18:36:40
您也可以使用BouncyCastle来代替使用System.Security.Cryptography的WinCrypt-API:
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-version (向散列添加身份验证)
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;
}
发布于 2012-07-07 02:32:01
我不明白你为什么要期待128。
字节中的8位。64字节。8* 64 = 512位哈希。
https://stackoverflow.com/questions/11367727
复制相似问题