PHP Yii2密码加密

Wad*_*oob 3 php security password-hash yii2

需要帮助,因为我还是Yii2的新手.我想在将密码保存到数据库之前加密密码.所以我使用的是sha1,但问题是当我在下面显示的控制器中应用这行代码时,表单中的密码字段有内容.

$ model-> password = sha1($ model-> attributes ['password']);

这是Controller创建方法:

public function actionCreate()
{
    $model = new Employeeinformation();

    //$model->password = sha1($model->attributes['password']);

    $model->created_date = date('Y-m-d H:i:s');

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->employee_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是形式:

<div class="employeeinformation-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'employee_id')->textInput(['minlength' => true, 'maxlength' => true]) ?>

<?= $form->field($model, 'password')->passwordInput(['maxlength' => true]) ?>

<?= $form->field($model, 'last_name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'first_name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'hired_date')->widget(\yii\jui\DatePicker::classname(), [
    'language' => 'en',
    'dateFormat' => 'yyyy-MM-dd',
]) ?>



<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
Run Code Online (Sandbox Code Playgroud)

我的问题截图:

http://i.imgur.com/YTDW1Ud.png

先感谢您.

Sco*_*ski 14

我想在将密码保存到数据库之前加密密码.

不,你没有.好吧,你可能认为你想加密密码,但如果你想保护用户,你实际上想要哈希密码,而不是加密密码.

SHA1不提供加密,它是一个哈希函数.这是一种非常常见的误解.您可以在链接的博客文章中了解有关基本加密术语和概念的更多信息.

更重要的是:您不希望像SHA1这样的快速哈希用于密码.使用password_hash()password_verify(),您将拥有安全的密码存储.你甚至不需要特别关心这些函数在内部做什么来正确使用它们.

public function actionCreate()
{
    $model = new Employeeinformation();
    $post = Yii::$app->request->post();

    if ($model->load($post)) {
        $model->password = password_hash($model->password, PASSWORD_DEFAULT);
        $model->created_date = date('Y-m-d H:i:s');
        if ($model->save()) {
            return $this->redirect(['view', 'id' => $model->employee_id]);
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);
}
Run Code Online (Sandbox Code Playgroud)

员工登录时,您只需要这样做:

if (password_verify($request->password, $storedEmployeeData->hashed_password)) {
    // Success
}
Run Code Online (Sandbox Code Playgroud)


ank*_*itr 8

Yii2在高级设置中附带用户模块.了解它如何以加密方式存储用户密码.

您可以使用setPassword()用户模型中的方法获取散列密码.

public function setPassword($password)
{
    $this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
Run Code Online (Sandbox Code Playgroud)

并在保存模型数据之前调用此方法.

public function signup()
{
    if ($this->validate()) {
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if ($user->save()) {
            return $user;
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

另请查看Yii2 doc以获取密码身份验证.