drgovyk64676 2013-10-14 09:27
浏览 28
已采纳

登录类返回bol(false)

I have the fallowing class that I have written for a login application using this tutorial :

class passHash{

    private static $algo='$2y$';
    private static $cost ='12$';

    private function generateSalt(){
       $salt=substr(sha1(mt_rand()),0,22);
       return $salt;
    }

    public function hashPassword($password){
       $hashpassword=crypt($password,self::$algo.self::$cost.self::generateSalt());
       return $hashpassword;
    }

    public function checkPassword($hash, $password){
        $fullsalt=substr($hash,0,29);
        $newhash=crypt($password,$fullsalt);

        if ($newhash==$password){
           return true;
        }else{
           return false;
       }

   }
}

I think the code is self explanatory and i saw that there are a lot of questions regarding this login class.
Now the problem that i have encountered is with checking the password. If I do something like:

$a=passHash::hashPassword('1234');

$b=passHash::checkPassword($a,'1234');
var_dump($b);

I gate the result of bool(false)

Where is the problem with this code ?


EDIT 1 if I modifay the checkPassword like this:

public function checkPassword($hash, $password){
    $fullsalt=substr($hash,0,29);
    $newhash=crypt($password,$fullsalt).'<br>';
    return $newhash;
}

and then i do :

$a=passHash::hashPassword('1234');
echo 'hashPassword: '.$a.'<br>';

$b=passHash::checkPassword($a,'1234');
echo 'checkPassword: '.$b.'<br>';

i get
hashPassword:$2y$12$6e29c2bbdacad854b1a63O8aty2a/.MQN0wbdmClnhXMbH3/tfQfG
checkPassword: $2y$12$6e29c2bbdacad854b1a63O8aty2a/.MQN0wbdmClnhXMbH3/tfQfG

they are identical .. so where is the problem ?

  • 写回答

2条回答 默认 最新

  • duanhan9334 2013-10-14 10:06
    关注

    The test should be this:

        if ($newhash==$hash){
           return true;
        }else{
           return false;
       }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写