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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器