加密密码:$ 2y $ 10和$ 2y $ 13

时间:2017-05-27 19:37:21

标签: php authentication passwords

我有一个php(symphony)Web应用程序和一个访问同一个数据库的android应用程序。表中的密码字段使用带有Bcrypt的symphony加密,其值以$2y$13开头,我使用了这段代码php加密我的android应用程序输入的密码:

 if(isset($_POST['password'])){
$password = $_POST['password'];
$pas_hash= password_hash("$password", PASSWORD_BCRYPT);
$sql = 'SELECT * FROM tbl_auth WHERE   password = :pas_hash';
  $stmt = $conn->prepare($sql);

      $stmt->bindParam(':pas_hash', $pas_hash, PDO::PARAM_STR);
      $stmt->execute();
      if($stmt->rowCount())
      {
         $result="true";    
      }  
      elseif(!$stmt->rowCount())
      {
            $result="false";
      }

      // send result back to android
      echo $result;

}

问题是pas_hash的值以$2y$10开头,当我使用password_verify()时,此函数返回true结果。 我没有遇到什么问题,因为发送到我的Android应用程序的最终$resultfalse。 感谢。

1 个答案:

答案 0 :(得分:1)

好的,所以if($stmt->rowCount())正在检查行数是否为空,但它不为空,它为0,因为你的结果返回了0行。

将您的陈述更改为if ($stmt->rowCount() > 0),并且应该修复它。

就您验证用户(?)的方式而言,有点奇怪,最好是搜索用户的用户名,从数据库中获取密码并使用password_verify来获取用户的密码和你从数据库中获得的密码。这可能不适合您的功能,但如果您愿意,也可以从下面的代码中获取灵感。

示例:

 if(isset($_POST['username'])){

      $username = $_POST['username'] // Or however you get the username.
      $password = $_POST['password'];
      $sql = 'SELECT * FROM tbl_auth WHERE username = :username';
      $stmt = $conn->prepare($sql);
      $stmt->bindParam(':username', $username, PDO::PARAM_STR);
      $stmt->execute();
      $result = $stmt->fetch(PDO::FETCH_ASSOC);
      if(password_verify($password, $result['password'])
      {
         $result="true";    
      }  
      else
      {
            $result="false";
      }
      // send result back to android
      echo $result;

}