下面是我正在重写的应用程序中用于散列密码的代码片段:
internal string GenerateSalt()
{
byte[] buf = new byte[16];
(new RNGCryptoServiceProvider()).GetBytes(buf);
return Convert.ToBase64String(buf);
}
internal string EncodePassword(string pass, string salt)
{
byte[] bIn = Encoding.Unicode.GetBytes(pass);
byte[] bSalt = Convert.FromBase64String(salt);
byte[] bAll = new byte[bSalt.Length + bIn.Length];
Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
HashAlgorithm s = HashAlgorithm.Create("SHA512");
byte[] bRet = s.ComputeHash(bAll);
return Convert.ToBase64String(bRet);
}
散列密码和盐稍后存储在数据库中。 base64 编码盐和密码的最大长度是多少?
最佳答案
由于 SHA-512 哈希始终是 512 位 = 64 字节并且 base64 需要 (n + 2 - ((n + 2) % 3) )/3 * 4
字节你总是需要 88 字节来存储数据。
关于c# - 此算法的 base64 编码盐/密码的最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6875631/