该算法的 Base64 编码盐/密码的最大长度
下面是我正在重写的应用程序中用于哈希密码的代码片段:
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 编码盐和密码的最大长度是多少?
Below is the snippet of code that's used to hash passwords in an app I'm rewriting:
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);
}
Hashed password and salt are later being stored in a database. What's going to be the max length of base64 encoded salt and password?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 SHA-512 哈希值始终为 512 位 = 64 字节,并且 base64 需要
(n + 2 - ((n + 2) % 3)) / 3 * 4
字节您将始终需要 88 字节来存储数据。Since an SHA-512 Hash is always 512 bits = 64 bytes and base64 needs
(n + 2 - ((n + 2) % 3)) / 3 * 4
bytes you will always need 88 bytes to store the data.