dongmiao520892 2017-08-25 17:31
浏览 55
已采纳

PHP password_verify false negative [关闭]

Background - I have spent the last hour or so looking through various posts sites and so on to see why my password checking function has suddenly stopped working.

Every time I try to use it, it returns false, including when the inputted password is correct.

I have echoed my input and my hash, both of which match fine (by comparing the hash in the table to the one echoed).

However, when these variables are put through password_verify, it returns false 100% of the time.

What do I think is wrong? - Based on a post from an external site, I think this is due to the way that my hashedPassword variable is being parsed. When is compares the two, something goes wrong in the variable type and returns false.

What does my code and database look like? -

users table. Contains other columns.

userId | username | password
----------------------------
  1    | example  |  temp1

Class containing the password checker function. (Have trimmed away try/catch)

public function checkPasswordCorrect($username, $password) {  // Returns true is the entered password is correct and false if it isn't.

   // This creates a mysqli context to run the connection through.
   $mysqli = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbPlusPlus);

   // If there is an error connecting to the database, it will be printed and the process will close.
   if (mysqli_connect_errno()) {
       printf("Connect failed: %s
", mysqli_connect_error());
       exit();
   }

   $sql = "SELECT password FROM users WHERE username='$username'";
   $result = $mysqli->query($sql);
   $row = $result->fetch_array(MYSQLI_ASSOC);

   $hashedPassword = $row["password"];

   $mysqli->close();

   echo $password."<br>".$hashedPassword."<br>"; // The input is correct and the hash matches that in the table.

   // We return a value of true or false, depending on the outcome of the verification.
   if(password_verify($password, $hashedPassword)) {
       echo "return true";
       return true;
   }          
   else {
       echo "return false";
       return false;
   }
}

By echoing $hashedPassword just before using password_verify, the hash matches by manually checking it, as you can see by the output below.

temp0022
$2y$10$9TgjJzSaqOB
return false

What am I asking? -

  1. Is there a better way of checking password inputs, more specifically, the way I'm pulling the information from the table (there must be some simpler way of doing this).

  2. Is the variable parsing to blame? Is there any documentation or articles available for this topic that I haven't seen?

What am I aware of? - My usage of mysqli is shocking, I'm new to it. I am also aware that there are similar answers out there, but seem to be down to some kind of syntax error more than anything else.

Thank you for taking the time to help! :D

For all of those of you who are worried that I'm going to be hit by SQL injection attacks: I have since modified this example code massively and am using prepared, statements. Thank you for your concern :)

  • 写回答

1条回答 默认 最新

  • douhushen3241 2017-08-25 17:38
    关注

    Check your table structure. Your hash field needs to be of length 60 (for PASSWORD_BCRYPT) or 255 (for PASSWORD_DEFAULT) (ref) to save the whole hash. What you got from the database is 18 characters long. You probably have it as VARCHAR(18), so it's getting truncated upon saving, which is why you're not getting a match.

    Edit: Added length for the PASSWORD_DEFAULT algorithm. Thanks @Don'tPanic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行