CakePHP - 如何为密码实现blowfish哈希?

Bad*_*sie 5 cakephp blowfish cakephp-2.0 cakephp-2.4

努力寻找关于在Cake 2.4中使用Blowfish的几个基本问​​题的答案.

AppController.php

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array(
                    'username' => 'email'
                ),
                'passwordHasher' => 'Blowfish'
            )
        )
    ),
    'Cookie',
    'Session'
);
Run Code Online (Sandbox Code Playgroud)

现在怎么办?我如何登录?

UsersController.php

public function login() {

    if (!empty($this->request->data)) {

        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirectUrl());
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我还需要添加什么?如果我尝试登录,我收到以下错误:

警告(512):无效盐:对于河豚请访问http://www.php.net/crypt并阅读有关建造河豚盐的相应部分.[CORE/Cake/Utility/Security.php,第285行]

在尝试登录之前是否需要对密码进行加密?如果是,我使用哪种方法以及使用盐最好的方法是什么?Cake会自动尝试为所有用户使用core.php配置文件中的salt 吗?

我很困惑主要是因为我不知道哪些部分使用标准PHP方式使用blowfish,CakePHP正在尝试自动为我做.

Mar*_*ean 9

如果您已经使用其他方法填充了密码哈希数据库,则无法使用Blowfish.如果是这样,它们将不是有效的Blowfish哈希密码,您将收到上述错误.

在CakePHP应用程序中实现Blowfish密码散列方面,Cookbook有一个关于在身份验证中使用bcrypt(Blowfish)的专门章节:http://book.cakephp.org/2.0/en/core-libraries/components/authentication. HTML#使用-bcrypt换密码

您可以像以下一样设置组件数组:

<?php
class AppController {

    public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

然后要生成密码,您将在模型中使用密码hasher类.例如,一个User模型:

<?php
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');

class User extends AppModel {

    public function beforeSave($options = array()) {
        // if ID is not set, we're inserting a new user as opposed to updating
        if (!$this->id) {
            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
        }
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后要进行身份验证,您不需要做任何事情,因为CakePHP的身份验证处理程序会为您进行比较:

<?php
class UsersController extends AppController {

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirectUrl());
            } else {
                $this->Session->setFlash( __('Username or password incorrect'));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是它的全部内容.