如何让 MD5 更安全?或者说真的有必要吗?
我将用户名和密码存储在 MySQL 数据库中,并使用 MD5 对它们进行哈希处理。然而,我只使用标准的 PHP 函数,没有任何修改。现在,我读到 MD5 已损坏。你做得怎么样?您是否通过不同的哈希机制运行多次或添加某种形式的盐?
I'm storing username and password in a MySQL database and have them hashed using MD5. However, I'm only using the standard PHP function without any modification. Now, I read that MD5 is broken. How are you doing it? Do you run it several times through a different hash mechanism or add some form of salt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
为每个存储的密码添加盐,但每个密码的盐值不相同
Add a salt to each password stored that's not equal for every password
只需使用
MD5("yoursite.com".$string);
MD5 不可解密。破解它的唯一可能的方法是通过暴力破解所有内容的哈希表。如果你添加一个只有你知道的随机字符串,他们就无法破解它。
Simply use
MD5("yoursite.com".$string);
MD5 is not decryptable. The only possible way to crack it is through hash tables that brute force everything. If you add a random string that only you know they cant crack it.
如果您担心密码安全,那么您应该使用 SHA1() (或替代方案)而不是 MD5()。虽然 MD5 不可解密,但它可以通过彩虹表或匹配哈希来破解。
盐将针对彩虹表起作用,但不适用于匹配通过 MD5 实现的哈希值。
If you're worried about password security then you should use SHA1() (or alternative) rather than MD5(). Whilst MD5 is not decryptable, it can be beaten by either rainbow tables or matching the hash.
Salts will work against rainbow table but not against matching the hash which has been achieved with MD5.
您应该做几件事。
:
现在您应该处于良好状态!
There are a couple of things you should do.
:
Now you should be in good shape!
您可能最好使用 使用 bcrypt 进行密码存储,以防止彩虹表攻击,以防坏人控制您的数据库。
至少,转储 MD5(尽管计算速度很快,但现在不太安全)并使用更安全的东西,例如带有长盐的 SHA256。
You might be better off using using bcrypt for password storage to prevent rainbow-table attacks in case the bad guys get hold of your DB.
At the very least, dump MD5 (although computationally fast, not very secure these days) and use something a little more secure like SHA256 with a long salt.
切换到不同的哈希机制(您可以在人们登录时增量执行)并且一定要使用(每个用户不同的)盐!
Switch to a different hash mechanism (you can do it incrementally as people log in) and definitely use a (different for each user) salt!
你可以使用一种叫做盐的东西。这意味着您还将这些盐保存到数据库中。它是一个或多或少长的随机字符串,并且对于每个用户来说都是唯一的。
然后,要检查密码,请执行以下操作:
You can use a thing called a salt. It means that you also save this salt into you database. It's a random string which is more or less long and is unique for each user.
Then, to check the password, you do something like this:
您可以通过我写过的名为“循环散列”的方法使 MD5 或任何散列函数变得更强大,请在此处阅读,加密数据的好方法,,使用循环“for”或“while”使用随机生成的密钥数字多次加密密码,真的很强大而且很简单,所以不会再次害怕破解者,目前没有人可以利用可用的数据库破解加密的“循环哈希”。
You can make
MD5
or any hashing function more strong by a method called "loop-hashing" i wrote about , read it here ,Good method to encrypte data, , using a loop "for" or "while" to encrypte password a lot of times with a random generated key number , really it's strong and so easy , so won't be scare from crackers again , no one can crack an encrypted "loop-hash" at the moment with the available databases .我很惊讶人们如何跟上“该死的,它坏了,我不会用它!”的潮流,不要犯同样的错误。
你无法让 MD5 变得更好。即使使用 SHA-1 也容易受到与 MD5 相同类型的攻击。
使用 bcrypt 将比 MD5 和 SHA 算法使用更多的 CPU。
MD5 的设计目标是速度快,与 SHA 相同。 bcrypt 不是,它允许更多排列,这使得某人更难尝试解密原始字符串。
您需要知道为什么 MD5 被认为是“损坏的”。
利用当今的计算能力,人们可以创建一组字符和所有排列的 MD5,并将它们映射到原始字符串。这就是你获得彩虹桌的方式。如果有人下载您的数据库,然后将密码与彩虹表进行比较 - 他们可以获得用户的原始密码。这之所以危险,是因为人们在许多事情上使用相同的密码 - 包括贝宝和其他货币处理服务。这就是为什么你使用所谓的盐。这使得获取原始字符串变得更加困难,因此对用户的密码加盐(比方说,通过反转密码并对反转的输入进行 MD5 处理)将使攻击者更难将哈希值恢复为原始字符串。
什么是碰撞?如果你给散列函数两个不同的字符串,它返回相同的散列 - 这就是冲突。它如何转换为用于登录的网络密码和哈希密码?如果您的 user1/password1 和 user2/password2 有相同的哈希值 - 他们可以以其他人的身份登录。这就是碰撞在安全中发挥作用的地方。
MD5 被认为损坏的原因是,MD5 对于差异较小的字符串返回相同的哈希值。而且要计算该字符串可能是什么并不容易!
从数学的角度来看 - 是的,它是“损坏的”,因为如果你的字符串有 100 个字符,并且它与其他字符串有 10 个字符不同(10% 的差异) - 你会得到相同的哈希值。
适用于 MD5 的内容也适用于所有哈希算法。最后,它们都不具有无限数量的可能的哈希值。
然而,其中一些(例如 MD5)的哈希值可能较少并且执行速度更快。
最后,如果有人访问您的数据库 - 您将面临比使用 MD5 而不是 bcrypt 或 SHA1 更大的问题。
I'm amazed how people jump on the bandwagon of "damn, it's broken, I won't use it!", don't do the same mistake.
You can't make the MD5 better. Even using SHA-1 is vulnerable to same type of attacks as MD5.
Using bcrypt will use A LOT more CPU than MD5 and SHA algorithms.
MD5 is designed to be fast, same as SHA. bcrypt isn't and it allows for more permutations, which makes it harder for someone to try to decrypt the original string.
You need to know why MD5 is considered "broken".
Using today's computing power, one can create an array of characters and MD5 all permutations and map them to the original string. That's how you get a rainbow table. If someone downloads your database and then compares passwords to their rainbow table - they can obtain users' original password. Reason why this is dangerous is because people use same passwords for many things - including paypal and other money processing service. That's why you use so-called salt. That makes it even harder to obtain the original string, so salting your users' passwords (let's say by reversing them and MD5-ing the reversed input) will make it harder for the attacker to revert the hash to original string.
What's a collision? If you give hashing function two different strings and it returns the same hash - that's a collision. How does it translate to web and hashing passwords for logins? If you have the same hash for user1/password1 and user2/password2 - they could log on as someone else. That's where collisions play the role in security.
Reason why MD5 is considered broken is because MD5 returns same hash for strings that differ in small percentage. And it's not easy to calculate what that string might be!
From mathematical point of view - yes, it's "broken" because if your string has 100 chars and it differs from other string in 10 chars (10% difference) - you get the same hash.
What applies for MD5 applies for ALL hashing algorithms. In the end, all of them don't have infinite number of possible hashes.
However, some of them (like MD5) have less possible hashes and execute faster.
In the end, if someone got to your database - you have a bigger problem than using MD5 instead of bcrypt or SHA1.