mcrypt is deprecated, what is the alternative?

前端 未结 10 2319
青春惊慌失措
青春惊慌失措 2020-11-22 08:05

The mcrypt-extension is deprecated will be removed in PHP 7.2 according to the comment posted here. So I am looking for an alternative way to encrypt passwords.

Righ

10条回答
  •  名媛妹妹
    2020-11-22 08:57

    I am using this on PHP 7.2.x, it's working fine for me:

    public function make_hash($userStr){
            try{
                /** 
                 * Used and tested on PHP 7.2x, Salt has been removed manually, it is now added by PHP 
                 */
                 return password_hash($userStr, PASSWORD_BCRYPT);
                }catch(Exception $exc){
                    $this->tempVar = $exc->getMessage();
                    return false;
                }
            }
    

    and then authenticate the hash with the following function:

    public function varify_user($userStr,$hash){
            try{
                if (password_verify($userStr, $hash)) {
                     return true;
                    }
                else {
                    return false;
                    }
                }catch(Exception $exc){
                    $this->tempVar = $exc->getMessage();
                    return false;
                }
            }
    

    Example:

      //create hash from user string
    
     $user_password = $obj->make_hash2($user_key);
    

    and to authenticate this hash use the following code:

    if($obj->varify_user($key, $user_key)){
          //this is correct, you can proceed with  
        }
    

    That's all.

提交回复
热议问题