MySQL 查询中的 SHA 盐
我正在设置一个cookie。 比如:
$_COOKIE['test'] = SHA1('124'.'mysalt');
现在 124 是我想要的 id。 因此,在我的 MySQL 表中,我尝试运行如下查询:
$sql = ("SELECT * FROM users WHERE SHA1(`id`) = '".mysql_real_escape_string($_COOKIE['test'])."'");
如何将“mysalt”添加到 SQL 查询? 因为否则我想获得正确的 id。
I am setting a cookie. Something like:
$_COOKIE['test'] = SHA1('124'.'mysalt');
Now 124 is my id which I want. So in my MySQL table, I am trying to run a query like:
$sql = ("SELECT * FROM users WHERE SHA1(`id`) = '".mysql_real_escape_string($_COOKIE['test'])."'");
How to add the "mysalt" to the SQL query? Because else I want get the correct id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用可以使用 Concat() 来实现。
Use can use Concat() for that.
查询应该是:
如果我正确理解你想要做什么。
The query should be:
if I understand correctly what you're trying to do.
已经提供的解决方案可能会很好地工作,但是您确定要这样做吗? 如果字段“id”确实是一个独特的标识,您可以使用“LIMIT 1”来阻止 mysql 搜索您的所有项目。 另一件事是,为什么不为此使用单独的预计算字段呢? 我的意思是,在每个查询中,mysql 不必要地需要计算所有这些 sha1 值。最后一件事。 我不确定你为什么使用你的方法,但我最好的猜测是实现某种会话密钥。 我认为这是一个坏主意,原因有几个:如果有人掌握了你的盐,他就可以访问你的所有帐户。 如果有人嗅探了一个“会话”,他可以随时重复使用它。 选择弱盐可能会产生严重后果。 HTH。
The solutions already provided probably will work just fine, however are you certain you want to do this? If the field "id" is really a distinct identification you can use "LIMIT 1" to stop mysql from searching thru all your items. Another thing is, why don't you use a separate precomputed field for this? I mean in every query mysql unnecessarily needs to compute all these sha1 values.. One last thing. I'm uncertain why you are using your approach, but my best guess is to implement some sort of session key. I thing this is a bad idea for a couple of reasons: If someone gets holds on your salt, he has access to all your accounts. If someone sniffs one "session" he can reuse it whenever he wants to. Choosing a weak salt could have serious consequences. HTH.
使用 CONCAT:
Use CONCAT: