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条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)