我使用sha1加密来加密我的密码,但我遇到了这个问题。对于某些用户来说,登录不起作用。
我的代码,(在注册中)
// all validation is done here
$password = sha1($_POST['password']);
// inserting data is here
登录时,我的查询是
$email = $_POST['email'];
$password = sha1($_POST['password']);
select * from users where email = $email and password = $password and status = 1 and deleted = 0;
面临密码问题的用户之一,
im$$man
我做错什么了吗。
请帮帮我。
我正在使用sha1加密来加密我的密码,
没有。SHA1不是加密,它是一个散列函数。理解加密和哈希之间的区别对于安全地实现这一点至关重要:
- 正确操作:使用
password_hash()
和password_verify()
- 做错了:用TripleDES ECB加密
此外,您编写查询的方式让我相信它很容易受到SQL注入的攻击。
问题就在这里,
sha1将im$$man
中的$man
视为变量,因此它将被计算为null,因为您没有任何值。
像这样的东西,
sha1("im$$man");// will echo 17cf5ec2752a9a7f0077c904f60b64f23ba2534d
也等于
sha1("im$");// will echo 17cf5ec2752a9a7f0077c904f60b64f23ba2534d
$man is evaluated to null.
两个输入的输出相同(但输入不同)
为了得到预期的结果,避免出现双引号,
sha1('im$$man');// this will give correct output
输出为
850caa44549443778fe005f466766d5d6d413692// correct output.
此处的md5()存在类似问题