在 MySQL 表中存储用户密码的最佳 PHP 哈希方法?
我已经阅读 Stack Overflow 问题大约 15 分钟了,每一个问题似乎都与我之前读到的问题相矛盾。 Bcrypt、SHA1、MD5 等。我目前对我的密码进行 MD5,但我想让我的数据库在发生泄露时更加安全。
我知道这个问题已经被问了一百万次,但我似乎在其他地方找不到合适的答案。
谢谢。
I've been reading Stack Overflow questions for about 15 minutes now and every single one seems to contradict the previous one I read. Bcrypt, SHA1, MD5, and so on. I currently MD5 my passwords, but I want to make my database more secure in case of a breach.
I know this has been asked a million times, but I can't seem to find a decent answer anywhere else.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你之所以会看到相互矛盾的答案,是因为没有正确的答案。您应该使用您的应用程序可以支持的最安全的方法。更安全=更多开销。
MD5 已被破坏和破解。
根据这篇文章,SHA1 已被破坏。然而它还没有被破解。
(据我所知)bcrypt 尚未被发现被破坏。
如果有足够的 CPU 周期,任何散列或加密算法最终都可以被绕过。您的决定应该平衡数据的安全性和应用程序的性能。
考虑到这些警告,bcrypt 是目前事实上的标准。它是为强度而不是速度而设计的,并且不会被损坏。有关 bcrypt 的信息索引,请参阅维基百科上的 bcrypt 文章。
The reason you see contradictory answers is because there is no right one. You should use the most secure method that your application can support. More secure = more overhead.
MD5 has been broken and cracked.
According to this article, SHA1 is broken. However it has not yet been cracked.
bcrypt has not (to the best of my knowledge) been found to be broken.
Given enough CPU cycles, any hashing or encryption algorithm can eventually be circumvented. Your decision should balance the security of your data with the performance of your application.
Given those caveats, bcrypt is the defacto standard at this time. It is designed for strength, not speed, and is not known to be broken. For an index of information about bcrypt, see the bcrypt article on Wikipedia.
我会选择 bcrypt。它大大降低了生成彩虹表的能力。
http://codahale.com/how-to-safely-store-a-密码/
I'd go with bcrypt. It drastically reduces the ability to generate rainbow tables.
http://codahale.com/how-to-safely-store-a-password/
首先,MD5 如今并不是一个很好的选择。如果攻击者能够访问您的数据库并获取 MD5 哈希值,那么几乎可以肯定他也能够破解它们。弱密码的 MD5 哈希值甚至可以被普通计算机暴力破解。
您应该在谷歌上搜索一些有关对哈希值加盐的文章,并将该方法与更强的哈希算法(至少是 SHA1)结合使用,并且可能重复该过程几次。
我不打算写关于加盐的文章,因为已经有很多关于它的文章了,而且在 Stack Overflow 上你可以找到很多关于这个问题的很好的讨论。
例如
为什么盐使字典攻击“不可能”?
或者
密码盐如何帮助抵御彩虹表攻击?
First of all, MD5 isn't a very good option nowadays. If an attacker would get to your database, and get the MD5 hashes, it is almost certain that he will be also able to crack them. MD5 hashes of weak passwords can be cracked even bruteforce by a casual computer.
You should google some articles about salting your hashes, and use that method combined with a stronger hashing algorithm (at least SHA1), and maybe repeat the process few times.
I am not going to write about salting, as many articles have been already written about it, and also here on Stack Overflow you can find many good discussions about the problem.
E.g.
Why do salts make dictionary attacks 'impossible'?
or
How does password salt help against a rainbow table attack?
使用 MD5、SHA1 或您想要的任何加密方式以及
SALT
。对于这个例子,我只是为了解释而使用 MD5。
因此,用户选择一个密码,例如将其存储在 $password 中。
现在创建一个特定于您的应用程序的盐。
然后这样做
,这样人们就不能通过谷歌搜索您的 MD5 字符串来使用字典攻击(如果它以某种方式受到损害)。
Use MD5, SHA1 or whatever encryption you want with a
SALT
.For this example, I'm just going to use MD5 for explanation sake.
So user chooses a password, store that in $password for instance.
Now create a salt that's specific to your application.
Then do
This way people can't use dictionary attacks by just googling your MD5 string if it ever got compromised somehow.
当用户注册时,使用例如以下函数创建随机
salt
:将其存储在数据库表中。最好的方法是将其存储在外部数据库中。之后,创建一个随机代码并将其与您的盐一起存储到外部数据库中。与将随机代码存储在您的用户表中相比,攻击者几乎不可能找到您的盐。
之后,存储您的密码,例如,这样:
当用户登录时,从外部数据库中获取盐并检查盐和密码是否匹配。
When a user registers, create a random
salt
using, for example, the following function:Store this in a database table. The best is to store it in an external database. After this, create a random code and store it together with your salt into the external database. Than store the random code in your users table and it will almost be impossible for an attacker to find your salt.
After this, store your password in, for example, this way:
When a user logs in, get the salt out of the external database en check if the salt and the password match.
您可以使用您网站的密钥和每个用户的特定盐与您的密码。您网站的密钥应保存在数据库中,然后提取并使用。
组合就变成了。
现在这个组合将存储在数据库中。
登录时再次使用该组合进行匹配。
我认为它可以更好地帮助保护您的应用程序。
干杯..!!
You can use secret key of your website and particular salt of every user with your password. Your secret key of your website should be saved in your database and then fetch it and use.
The combination becomes.
Now this combinations will store in database.
At the time of login, use again this combination to match.
I think it can help more to secure your application.
Cheers..!!