ASP.NET 成员资格使用的默认哈希算法是什么?
ASP.NET 成员资格使用的默认哈希算法是什么? 我该如何改变它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
ASP.NET 成员资格使用的默认哈希算法是什么? 我该如何改变它?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
编辑:不要按原样使用会员提供程序,因为它在保护用户密码方面严重不足,
因为谷歌搜索“会员提供商哈希算法” 将此答案作为第一个结果,并且将推断出福音,我有必要警告人们不要使用会员提供商,例如并使用 SHA-1、MD5 等哈希值来混淆数据库中的密码。
tl;dr
使用密钥派生函数,例如 bcrypt、scrypt 或(如果您需要 FIPS 合规性) ) PBKDF2 的工作因子足以使单个密码的哈希时间接近 1000 毫秒或更长。
如今,哈希很容易被暴力破解,最近历史上有大量数据泄露的例子。 为了防止您的用户密码在下一次黑客攻击中最终出现在 Pastebin 上,请确保使用需要足够长的时间来计算的函数对密码进行哈希处理!
而不是 Membership Provider,请尝试 IdentityReboot 或 特洛伊亨特至少谈到了微软的新实现。
同样有趣的是,在上面提到的相同谷歌结果中,我发现 教程向人们展示了使用 JtR 或 Hashcat 等流行工具来暴力破解这些密码哈希是多么容易。 在定制 GPU 装备上,SHA1 可以以每秒 48867 百万哈希值的惊人速度破解。 em>! 拥有像 rockyou 之类的免费词典,一个积极主动的人,拥有您的数据库将很快获得您的大多数用户的密码。 作为开发人员,您有道德责任采取必要措施来保护用户密码的安全。
默认散列是 SHA1,但他们也加盐和对其进行 base64:
如果您想了解更多有关如何更改它的信息,我仍然需要找出(除非使用自定义提供程序,请参见下文),但是 SHA-1 目前相当不错。 如果您想反转它或从中查找,这些人做了一些工作: http: //forums.asp.net/p/1336657/2899172.aspx
如果可能需要的话,这个问题将有助于逆转或复制此技术。 在 Ruby 中重新实现 ASP.NET 成员身份和用户密码哈希< /a>
如果您正在创建自定义提供程序,您可以创建您的散列和加密算法和方法。
http://msdn.microsoft.com/en-us/library/aa479048.aspx
EDIT: Do not use the Membership Provider as-is because it is horridly inadequate in terms of protecting user's passwords
In light of the fact that googling "membership provider hashing algorithm" turns up this answer as the first result, and the gospel that will be inferred, it behoves me to warn folks about using the Membership Provider like this and using hashes like SHA-1, MD5 etc to obfuscate passwords in databases.
tl;dr
Use a key-derivation function like bcrypt, scrypt or (if you need FIPS compliance) PBKDF2 with a work factor sufficient to necessitate the hashing time for a single password to be as close to 1000ms or more.
Hashes are easy to brute force these days with ample examples of data breaches in recent history. To prevent your user's passwords from ending up on pastebin in the next hack, ensure that passwords are hashed with a function that takes a sufficiently long time to compute!
Instead of Membership Provider, try IdentityReboot or the newer implementations from Microsoft that Troy Hunt talks about at the least.
It's also interesting that on the same google results mentioned above I find a tutorial showing folks preciously how easy it is to brute force these password hashes using popular tools like JtR or Hashcat. On a custom GPU rig, SHA1 can be cracked at a staggering rate of 48867 million hashes per second! With a free dictionary like rockyou or the like, a motivated person with your database will very quickly have most of your users passwords. As a developer, it's your ethical responsibility to do what is necessary to protect the security of your users' passwords.
The default hashing is SHA1 but they also salt it and base64 it:
If you want to know more about how to change it I still need to find out (unless using custom provider see below) however SHA-1 is pretty good for now. If you are looking to reverse it or lookup from this these guys did some work on that: http://forums.asp.net/p/1336657/2899172.aspx
This SO question will help in reversing or duplicating this technique if that is what might be needed. Reimplement ASP.NET Membership and User Password Hashing in Ruby
If you are making a custom provider you can create your hashing and encryption algorithms and methods.
http://msdn.microsoft.com/en-us/library/aa479048.aspx
Ryan Christensen 的上述答案并不完整。 将盐转换为 byte[] 的部分不正确。
这是我在客户解决方案中实现的一个工作示例:
The above answer by Ryan Christensen isn't complete. The part where it converts the salt to a byte[] isn't correct.
This is a working example that I've implemented in a solution for a client:
默认哈希算法类型是 SHA1。 有两种方法可以改变这种情况。
1) 如果您使用的是 IIS 7,您可以使用“机器密钥”配置进行更新(如下所示)。 这允许您从可用选项列表中选择加密方法并指定密钥或密钥生成选项。
2) 如果您使用的是 IIS 6,您可以使用以下命令更改哈希算法类型web.config 文件中的成员资格元素:
根据文档 hashAlgorithmType 属性可以是任何提供的 .Net 哈希算法类型。 经过一番挖掘,发现 ASP.Net 2、3 和 3.5 的有效值为
MD5
、RIPEMD160
、SHA1
、SHA256
、SHA384
、SHA512
。 这里重要的部分是所有这些类都继承自HashAlgorithm
。hashAlgorithmType 属性的值也可以是machine.config 文件中 cryptoNameMapping 元素中的条目。 如果您需要第三方哈希算法,您可以使用它。 如果您使用的是 ASP.Net 2 或更高版本,则 machine.config 文件通常可以在
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
中找到。 您可以在此处了解有关设置这些值的更多信息。The default hash algorithm type is SHA1. There are two ways that you can change this.
1) If you are working with IIS 7 you can update this using the "Machine Key" configuration (shown below). This allows you to choose the encryption method from a list of available options and specify the keys or the key generation options.
2) If you are working with IIS 6 you can change the hash algorithm type using the membership element in the web.config file:
According to the documentation the string value of the hashAlgorithmType attribute can be any of the provided .Net hashing algorithm types. A bit of digging shows that the valid values for ASP.Net 2, 3 and 3.5 are
MD5
,RIPEMD160
,SHA1
,SHA256
,SHA384
,SHA512
. The important part here is that all these classes inherit fromHashAlgorithm
.The value of the hashAlgorithmType attribute can also be an entry from the cryptoNameMapping element in the machine.config file. You could use this if you require a 3rd party hashing algorithm. The machine.config file can typically be found in
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
if you are using ASP.Net 2 or later. You can read more about setting these values here..NET 4.0 Framework 中的默认哈希算法更改为 HMACSHA256。
请注意,与 SHA-1 不同,HMAC SHA-256 是密钥哈希。 如果您的散列行为具有不确定性,则您可能没有设置密钥,从而强制它使用随机密钥。 类似于以下内容的内容将是罪魁祸首(这是我刚刚花了一个小时弄清楚的:p)。
如果您希望让它与现有的提供程序一起使用,您可以使用 本指南。
The default hash algorithm changed to HMACSHA256 in the .NET 4.0 Framework.
Note that unlike SHA-1, HMAC SHA-256 is a keyed hash. If your hashes are behaving non-deterministically, you probably haven't set a key, forcing it to use a random one. Something similar to the following would be the culprit (which is what I just spent an hour figuring out :p ).
If you wish to have it work with an existing provider you can revert it back to the former defaults using this guide.
哈希算法有一个更正,您必须使用:
而不是
阅读文章 http://svakodnevnica.com.ba/index.php?option=com_kunena&func=view&catid=4&id=4&Itemid=5& ;lang=en#6
There is one correction in hashing algorithm, you must use:
instead of
Read article http://svakodnevnica.com.ba/index.php?option=com_kunena&func=view&catid=4&id=4&Itemid=5&lang=en#6
让我们讨论这个问题的安全且经过时间考验的答案:
Zetetic 只需两行代码即可完成! 哈希算法 PBKDF2 比 SHA1 或 SHA256-SHA512 等要好得多PBKDF2、SCRYPT 或 ARGON2 等最新算法在哈希方面处于领先地位。 但在这种情况下使用 PBKDF2 很有用,因为它是由 .NET 在
Rfc2898DeriveBytes
类中实现的。 到目前为止,使用这个库非常棒,但有一些小问题,例如:a. Zetetic 默认使用 5000 次迭代。 如果您使用
Pbkdf2Hash256K
,则可自定义
b. Zetetic 使用
Rfc2898DeriveBytes
和Rfc2898DeriveBytes
由于某种原因基于HMACSHA1
,无法自定义。好消息! 我已自定义
Rfc2898DeriveBytes
以使用HMACSHA512
进行 128,000 次迭代,以便 SQLMembershipProvider 可以使用迄今为止尚不可用的 PBKDF2。 为了实现这一目标,我将 Zetetic 代码与我的Rfc2898DeriveBytes
如下图:using System.Security.Cryptography;
创建此类后,执行
将以下行添加到 Global.asax 或项目的相应启动文件的 Application_Start 事件中:
System.Security.Cryptography.CryptoConfig.AddAlgorithm(typeof(custom.hashing.keyderivation.PBKDF2Hash), "PBKDF2Hash_HB");
并将 web.config 更改为:
构建此答案的参考资料取自:
Let's discuss answers to this question which are secure and time-tested:
Zetetic Just two line of code and done! The hashing algorithm PBKDF2 is much better than having SHA1 or SHA256-SHA512 etc. Latest algorithms like PBKDF2, SCRYPT or ARGON2 are leaders when it comes to hashing. But using PBKDF2 is useful in this case as it is implemented by .NET in
Rfc2898DeriveBytes
class. Using this library was awesome till now but there are some minor issues like:a. Zetetic use 5000 iterations by default. Customizable if you use
Pbkdf2Hash256K
b. Zetetic use
Rfc2898DeriveBytes
andRfc2898DeriveBytes
is based onHMACSHA1
for some reason and can not be customized.Good news! I have customized
Rfc2898DeriveBytes
to useHMACSHA512
with 128,000 iterations so that SQLMembershipProvider can use PBKDF2 which was not available so far. To achieve this, I have combined Zetetic's code with my implementation ofRfc2898DeriveBytes
as shown below:using System.Security.Cryptography;
After creating this class do this:
Add following line to Application_Start event of Global.asax or respective startup file of your project:
System.Security.Cryptography.CryptoConfig.AddAlgorithm(typeof(custom.hashing.keyderivation.PBKDF2Hash), "PBKDF2Hash_HB");
And change web.config as:
<membership defaultProvider="sitecore" hashAlgorithmType="PBKDF2Hash_HB">
References for constructing this answer are taken from:
我附加了一个片段,显示了上面在 F# 中 Rawbert 的答案中的代码,
这是来自活动应用程序的工作代码
I attach a snippet showing the code as in Rawbert's answer above in F#
This is working code from an active application