【问题标题】:CakePHP - How do I implement blowfish hashing for passwords?CakePHP - 如何为密码实现河豚散列?
【发布时间】:2014-01-14 12:41:32
【问题描述】:

努力寻找有关在 Cake 2.4 中使用 Blowfish 的一些基本问题的答案。

AppController.php

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

现在呢?如何登录?

UsersController.php

public function login() {

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

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

    }
}

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

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

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

我很困惑,主要是因为我不知道以标准 PHP 方式使用河豚的哪些部分 CakePHP 试图为我自动完成。

【问题讨论】:

  • 我也遇到了这个问题。你解决了吗?

标签: cakephp cakephp-2.0 blowfish cakephp-2.4


【解决方案1】:

如果您已经有一个数据库填充了使用另一种方法散列的密码,则不能使用 Blowfish。如果是这样,它们将不是有效的 Blowfish 哈希密码,您将收到上述错误。

关于在 CakePHP 应用程序中实现 Blowfish 以实现密码散列,Cookbook 有专门的部分介绍在身份验证中使用 bcrypt (Blowfish):http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-bcrypt-for-passwords

你已经设置了组件数组:

<?php
class AppController {

    public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            )
        )
    );
}

然后,要生成密码,您将在模型中使用密码哈希类。例如,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;
    }
}

然后验证你不需要做任何事情,因为 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'));
            }
        }
    }
}

仅此而已。

【讨论】:

  • if (!$this-&gt;id) {} 是非常规的。我不会推荐这种方法。特别是如果您还想为用户提供能够更改其密码的功能。使用 !empty() 更适合这里。见here
  • 您的示例并没有真正为我所拥有的添加任何内容,因为用户无法注册自己,因此他们没有创建用户。我应该如何将散列密码存储在数据库中?哪种数据类型/长度?
  • @mark 确实,但我遇到了一个问题,如果我获取 User 记录,然后保存它们,如果密码出现在结果集中(即使用 find('all') 调用)然后密码被重新散列,只是因为它存在,所以!empty()没有删掉它。
  • @BadHorsie 您需要实现控制器操作来添加用户。在表单中有一个密码字段 ($this-&gt;Form-&gt;input('User.password');),CakePHP 将完成剩下的工作。至于数据库列类型:CHAR(60).
【解决方案2】:

我为所有有同样问题的人补充了一句: 我将河豚哈希保存为 VARCHAR(50),在某些情况下它太短了。因此,我的登录无效,因为哈希错误。请确保使用足够长度的字段(对于 Blowfish 至少 VARCHAR(123) )。 Source

【讨论】:

    【解决方案3】:

    我在尝试将项目从 1.3.x 移植到 2.7.x 时遇到了类似的问题。我从完整的数据库开始(在同一过程中从 MySQL 移植到 PostgreSQL)但是一个非常空的 CakePHP 应用程序。 问题是我的用户表中的散列密码来自 CakePHP 1.x。所以表中的哈希值没有遵循 Blowfish 约定。正是这个触发了错误消息。河豚哈希遵循复杂的格式。顺便说一句,相同字符串的重复哈希值会有所不同。

    解决方案:

    1. 将用户表中的密码列更改为 VARCHAR(128) (或者更多)。
    2. 清空列。
    3. 为第一个生成有效哈希 登录。

    为了做后者,我在 UsersController.php 中插入了以下几行:

    public function login() {
      if ...
      // code for getting a start password:
      $passwordHasher = new BlowfishPasswordHasher();
      $mypasswd = $passwordHasher->hash('MyPasswordinClearText');
      debug($mypasswd);
      // end inserted code
      ...
    }
    

    非常粗糙,但有效。现在我可以登录以继续在同一个项目中进行开发。登录后删除代码。

    【讨论】:

      猜你喜欢
      • 2013-05-11
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 2014-09-05
      • 2011-03-25
      • 2016-02-29
      相关资源
      最近更新 更多