这对于检查我的密码散列码,hashing.php登录问题 - 检查输入密码与mysql中的哈希值?
<?php
//compare password with hashed one
public static function check_password($hash,$password) {
$full_salt=substr($hash,0,29);
$new_hash=crypt($password,$full_salt);
return ($hash==$new_hash);
}
?>
这是login.php中
<?php
require("scripts/hashing.php");
$password=$_POST['txtPassword'];
//checking in database if password exists or not
$checkPassword=mysql_query("SELECT * from $tbl_name WHERE Password='".$password."'");
$resultPassword=mysql_fetch_array($checkPassword);
if(!hashing::check_password($resultPassword['Password'],$password)) {
//back to login
}
?>
的问题是,即使当用户输入错误的密码,它允许用户登录。
编辑
<?php
class hashing {
//blowfish
private static $algo='$2a';
//cost parameter
private static $cost='$10';
public static function unique_salt() {
return substr(sha1(mt_rand()),0,22);
}
//generate a hash
function myhash($password) {
return crypt($password,self::$algo.self::$cost.'$'.self::unique_salt());
}
//compare password with hashed one
public static function check_password($hash,$password) {
$full_salt=substr($hash,0,29);
$new_hash=crypt($password,$full_salt);
return ($hash==$new_hash);
}
}
?>
死你尝试只是你return语句之前呼应的结果? echo'hash:“'。$ hash。'”==“'。$ new_hash。'”'; –
如果用户填写不在数据库中的密码,会发生什么情况? '$ resultPassword'会是什么?为了这个价值,你的'check_password'会做什么? – Konerak
你的逻辑是有缺陷的。首先从数据库中获取所有行,其密码与您的密码相同(直接来自POST,而不是加密)。然后你检查它们是否相等?那么猜猜看 - 他们是!否则SELECT将无法工作。 – Konerak