Rud*_*udy 5 c# security passwords cryptography
我正在编写一个哈希密码的类,它通过使用System.Security.Cryptography.Rfc2898DeriveBytes类来生成用于计算哈希值的键来实现密钥拉伸.
代码基本上是这样的:
// Higher iterations value results in better key strength
var db = new Rfc2898DeriveBytes(password + salt + secretKeyFromConfigFile,
saltAsBytes,
iterations);
byte[] hashKey = db.GetBytes(64); // 64 bytes is the recommended size for the HMACSHA512 class
var sha512 = new System.Security.Cryptography.HMACSHA512(hashKey);
byte[] hashInput = System.Text.Encoding.UTF8.GetBytes(password + salt + secretKeyFromConfigFile);
byte[] hash = sha512.ComputeHash(hashInput);
return System.Convert.ToBase64String(hash);
Run Code Online (Sandbox Code Playgroud)
System.Security.Cryptography.Rfc2898DeriveBytes的文档表明它使用"...基于System.Security.Cryptography.HMACSHA1的伪随机数生成器"来派生密钥.
由于SHA512优于SHA1,所以简单地应用SHA512散列函数会更好吗?
byte[] hash;
for (int i = 0; i < iterations; i++)
{
hash = sha512.ComputeHash(hash ?? hashInput);
}
return System.Convert.ToBase64String(hash);
Run Code Online (Sandbox Code Playgroud)
加强密钥(如第一个代码块所示)和加强哈希(如第二个代码块所示)之间有什么区别?
散列密码时进行密钥扩展的首选方法是什么?
除非您是加密专家,否则请坚持使用 Rfc2898DeriveBytes,它很可能已正确实现。抵制将自定义解决方案拼凑在一起的诱惑,这会破坏系统的安全性。
没有理由对 Rfc2898DeriveBytes 的输出进行哈希处理。
直接使用 GetBytes(),它就是为此目的而设计的。
你的关键拉伸的目的是什么?密码还是密码哈希?对于密码哈希,您可能想使用不同的算法。
| 归档时间: |
|
| 查看次数: |
2208 次 |
| 最近记录: |