crypt() 未按需要运行
我使用 crypt 如下:
$pass = crypt($pass, 'd4');
针对 mysql 表插入和验证密码。问题是,如果密码相似,则会生成相似的结果。是否有一种算法可以保证不同的密码得到不同的结果?
I'm using crypt as follows:
$pass = crypt($pass, 'd4');
for both insertion and validation of a password against a mysql table. Problem is that if the passwords are similar it generates a similar result. Is there an algorithm that guarantees different results for different passwords?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
hash()
并选择哈希算法非常适合您(如果可能的话,比 MD5 更强,但也不要一直使用 SHA512)在
crypt()
的手册页你会发现这个:这应该可以解释为什么你会得到相同的结果。
Use
hash()
and choose hashing algorithm that suits you well (if possible something stronger than MD5, but don't go all the way to SHA512 either)On
crypt()
's manual page you will find this:which should explain why you get same results.
使用
crypt()
就可以了,因为只要您不使用旧的基于 DES 的模式 (CRYPT_STD_DES
)。使用它的唯一有效原因是为了与使用此类密码哈希的旧版软件进行互操作。请改用
CRYPT_BLOWFISH
、CRYPT_SHA256
或CRYPT_SHA512
模式。这些是现代密码哈希算法,接受任意长的密码短语,使用长盐并支持通过多次迭代进行密钥强化。不幸的是,PHP
crypt()
接口有点尴尬:显式选择所需算法的唯一方法是提供格式正确的$salt
参数,这意味着您还可以必须自己生成实际的盐。不过,这可能仍然比滚动您自己的密码哈希代码更容易、更安全。Using
crypt()
is fine, as long as you don't use the old DES-based mode (CRYPT_STD_DES
). The only valid reason to use that is for interoperability with legacy software that uses such password hashes.Instead, use the
CRYPT_BLOWFISH
,CRYPT_SHA256
orCRYPT_SHA512
modes. These are modern password hashing algorithms that accept arbitrarily long passphrases, use long salts and support key strengthening via multiple iterations.Unfortunately, the PHP
crypt()
interface is somewhat awkward: the only way to explicitly choose the algorithm you want is by supplying a correctly formatted$salt
parameter, which means you also have to generate the actual salt yourself. That's probably still easier and safer than rolling your own password hashing code, though.你可以加点盐。通常,如果您要存储密码,您会希望对它们进行哈希处理,而不是对其进行加密。如果您搜索(例如在 Google 上),您可以了解很多有关此内容的内容。
You could add a salt. Typically though if you're storing passwords you'll want to hash them, not encrypt them. There's load of stuff you can learn about this if you search for it (like on Google).
从 php crypt() 页面:
您可能还想使用不同的加密方法,例如 MD5 或 SHA256,因为这些方法通常比 DES 更好。
from the php crypt() page:
You may also want to use a different method of crypt such as MD5 or SHA256 as these are often preferable to DES.