MySQL 查询中的 SHA 盐

发布于 2024-07-25 20:00:49 字数 323 浏览 5 评论 0原文

我正在设置一个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 技术交流群。

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

发布评论

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

评论(4

回首观望 2024-08-01 20:00:49

使用可以使用 Concat() 来实现。

SELECT ... Sha1( Concat(`id`, 'mysalt') )=...

Use can use Concat() for that.

SELECT ... Sha1( Concat(`id`, 'mysalt') )=...
生活了然无味 2024-08-01 20:00:49

查询应该是:

$sql = ("SELECT * FROM users WHERE SHA1(CONCAT(`id`,`mysalt`)) = '".mysql_real_escape_string($_COOKIE['test'])."'");

如果我正确理解你想要做什么。

The query should be:

$sql = ("SELECT * FROM users WHERE SHA1(CONCAT(`id`,`mysalt`)) = '".mysql_real_escape_string($_COOKIE['test'])."'");

if I understand correctly what you're trying to do.

浪漫人生路 2024-08-01 20:00:49

已经提供的解决方案可能会很好地工作,但是您确定要这样做吗? 如果字段“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.

凉栀 2024-08-01 20:00:49

使用 CONCAT:

$sql = ("SELECT * FROM users WHERE SHA1(CONCAT(`id`,'mysalt')) = '".mysql_real_escape_string($_COOKIE[''test''])."'");

Use CONCAT:

$sql = ("SELECT * FROM users WHERE SHA1(CONCAT(`id`,'mysalt')) = '".mysql_real_escape_string($_COOKIE[''test''])."'");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文