在 PHP 中对密码加盐

发布于 2024-12-11 07:16:13 字数 410 浏览 0 评论 0原文

有人可以帮助我吗?我一直在阅读有关对密码加盐以使我的密码更安全的内容。我使用的格式是 salt:password_hashed,所以我的代码是 md5($salt.":".$password_hashed)$password_hashed 是原始密码的简单 m5d 字符串,哈希值取自 mktime()

我不明白盐是如何工作的,我是否也需要将其保存在数据库中?如果盐不断变化,这是如何工作的?

如果用户使用密码“password”注册,并且注册时间为 1234567890(作为 unix 时间戳)。生成的密码将是 md5(mktime().":".$_POST['password']) 或类似的内容。但是,如果用户尝试使用“密码”登录,新创建的盐会有所不同吗?

can someone help me. I've been reading about salting a password to make my passwords more secure. The format I was going with is salt:password_hashed, so my code is md5($salt.":".$password_hashed). $password_hashed is a simple m5d string of the original password, and the hash is taken from mktime().

I don't understand how the salt works, do i need to save this in the database as well? If the salt is ever changing, how does this work?

If a user registered with the password 'password' and the time they registered was 1234567890 (as a unix timestamp). The password generate would be md5(mktime().":".$_POST['password']) or something lik that. But if a user trys to then login with 'password' the newly created salt would be different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-12-18 07:16:13

您也必须存储盐,例如,您可以只存储注册日期并将其使用在盐中。

You have to store the salt too, you could for example just store the registrationdate and use that in your salt.

触ぅ动初心 2024-12-18 07:16:13

您的盐可以是用户的电子邮件或用户名。

我推荐这种方法:

hash_hmac('sha256', $password, $salt);

Your salt can be user's e-mail or username.

I recommend this approach:

hash_hmac('sha256', $password, $salt);
能否归途做我良人 2024-12-18 07:16:13

盐可以防御彩虹表。保存密码时、创建用户以及更改密码时都会生成盐。您可以将盐作为单独的字段存储在数据库中,但也可以将盐与加密密码连接起来,并将两者存储在一个字段中。由于盐通常具有固定长度,因此以后很容易将两者分开以验证登录密码。

值得注意的是,根据 US-CERT 的说法,md5“被认为在密码学上已被破坏,不适合进一步使用”。您应该改用 sha-256。

Salts defend against rainbow tables. The salt is generated when the password is saved, so when the user is created and whenever the password is changed. You can store the salt as a separate field in your database but you can also just concatenate the salt with the encrypted password, and storing both in one field. Because salts are usually of a fixed length, it's easy to separate the two later to verify a password for login.

It is worth noting that md5 "considered cryptographically broken and unsuitable for further use" according to US-CERT. You should use sha-256 instead.

冬天旳寂寞 2024-12-18 07:16:13

我一直在阅读有关对密码加盐以使我的密码更安全的内容。

这真是无稽之谈。没有盐可以使您的密码更安全。

我是否也需要将其保存在数据库中?

是的

如果盐不断变化,它是如何工作的?

这不起作用。

如果用户尝试使用“密码”登录,新创建的盐会有所不同吗?

你觉得怎么样?您不能自己将 mktime() 结果与“1234567890”进行比较(以及生成的哈希值)吗?

I've been reading about salting a password to make my passwords more secure.

That's quite nonsense. No salt can make your password more secure.

do i need to save this in the database as well?

yes

If the salt is ever changing, how does this work?

It doesn't work.

if a user tries to then login with 'password' the newly created salt would be different?

How do you think? Can't you compare mktime() result with '1234567890' yourself (and resulting hashes as well)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文