创建随机生成的安全令牌并存储在数据库中
我正在尝试为我网站上的每个用户生成一个唯一的令牌。该令牌是在用户注册时生成的,并且最好尽可能安全。让我向用户显示令牌的最佳方法是什么?
该令牌不是密码,用户不会自己创建它。如果我在注册时对它进行哈希和加盐,那么我显然将无法检索它,因为它是经过哈希和加盐的。我想要一种简单的方法通过 PHP 来做到这一点,并且能够轻松地将其显示给用户。
I am trying to generate a unique token for every user on my site. The token is generated when the user registers, and will ideally be as secure as possble. What is the best method of doing so to allow me to display the token to the user?
This token is NOT a password, and the user is not going to create it themselves. If I hash and salt it upon registration, I will not be able to retrieve it obviously, because it's hashed and salted. I want a simple way to do so via PHP and be able to display it to the user easily.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哈希是单向的,因此反转哈希的唯一方法是存储原始值,从而使哈希变得冗余。
如果您只需要显示该值一次,然后将该值存储到变量中,将其打印在页面上,并且不保存它,这对您来说是安全的。
另一种选择是仅使用数据库加密来存储令牌,然后在向用户显示之前解密。假设您使用 MySQL,您可能会发现以下内容有帮助:MySQL 加密
Hashes are one-way so the only way to reverse a hash is to store the original value making the hash redundant.
If you only need to display the value once, then store the value into a variable, print it on the page, and don't save it, that's secure for you.
Another option is to just use database encryption to store the token, then decrypt before displaying to the user. Assuming you use MySQL you might find this helpul: MySQL Encryption
怎么样:
What about:
不要贬低zerkms的评论。但我会推荐 crypt()。
非哈希值对用户有什么作用?
not to degrade zerkms' comment. but i would recommend crypt().
what does the non-hashed value do for the user?
用户将如何使用这个令牌?
真正的问题是安全是什么意思。我假设你的意思是“不可猜测”,这只是意味着它必须很长和/或复杂。
然而,如果用户需要记住它,那就是一个问题。
所以真正的问题是你想让它猜出有多难,以及你想让它被回忆起来有多容易?
How will the user use this token?
The real question is what do you mean by secure. I am assuming you mean "unguessable", which just means that it has to be long and/or complex.
However that is a problem if the user needs to remember it.
So the real issue is how hard do you want to make it to guess, and how easy do you want it to be recalled?