2

我想知道密码哈希是如何生成的?

// This is my code:

$email="mail@example.net";
$password="mypassword";

// How to get password_hash variable?

$user = User::find()->where(['email'=>$email, 'password_hash'=>$password_hash])->one();
if(isset($user)){
   echo "there is";
} else {
 "Sorry!";  
}

谢谢你。

4

3 回答 3

2

http://www.yiiframework.com/doc-2.0/guide-security-passwords.html Yii 2 中如何处理密码。除非你是加密专家,否则不要尝试自己编写。

于 2015-06-30T19:57:31.340 回答
0
  公共函数验证密码($密码)
    {
        if(md5($password) === $this->password)
            返回真;
        别的
            返回错误;
        //return Yii::$app->security->validatePassword($password, $this->password);
    }

  公共函数 beforeSave($insert)
    {
        // 如果设置了哈希新密码
        if ($this->newPassword) {
            //$this->password = Yii::$app->security->generatePasswordHash($this->newPassword);
            $this->password = md5($this->newPassword);
        }

        // 将 ban_time 复选框转换为日期
        如果($this->ban_time){
            $this->ban_time = date("Ymd H:i:s");
        }

        // 确保字段为空,因此它们不会被设置为空字符串
        $nullAttributes = ["email", "username", "ban_time", "ban_reason"];
        foreach ($nullAttributes as $nullAttribute) {
            $this->$nullAttribute = $this->$nullAttribute ?$this->$nullAttribute : null;
        }

        返回父级::beforeSave($insert);
    }
于 2015-02-16T09:54:48.637 回答
-4

我将哈希与 md5 一起使用,但我在用户模型中创建函数

    public function validatePassword($password)
{
    return $this->PASSWORD === md5($password);
}
于 2014-12-28T19:02:48.090 回答