Csharp/C#教程:关键加强。 我做得对吗?分享


关键加强。 我做得对吗?

我正在编写一个哈希密码的类,它通过使用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); 

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); 

加强密钥(如第一个代码块所示)和加强哈希(如第二个代码块所示)之间有什么区别?

散列密码时进行密钥扩展的首选方法是什么?

除非你是加密大师,否则只要坚持Rfc2898DeriveBytes,这很可能是正确实现的。 抵制攻击自定义解决方案的诱惑,这会破坏系统的安全性。

没有理由对Rfc2898DeriveBytes的输出进行散列。

直接使用GetBytes(),它就是为那个意图而设计的。

你的关键伸展的目的是什么? 密码或密码哈希? 对于密码哈希,您可能希望使用不同的算法。

这是相当古老的,但是当我在谷歌上搜索相同的问题时,我遇到了这个问题,解决方案确实是坚持Rfc2898DeriveBytes的输出。

Rfc2898DeriveBytes是PBKDF2的.NET实现。 它需要您的密码,盐和迭代次数。 这已经是整个实现,并且不需要使用另一个哈希算法重新结果。

事实上,强烈建议不要围绕此创建自己的实现,因为您可能会像其他人已经提到的那样破坏安全性。

这就是https://openid.stackexchange.com所做的,你可以在这里看到: https : //code.google.com/p/stackid/source/browse/OpenIdProvider/Current.cs#1135

上述就是C#学习教程:关键加强。 我做得对吗?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/998901.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐