繁体   English   中英

使用CakePHP的更安全的散列算法

[英]Using a more secure hashing algorithm with CakePHP

默认情况下,CakePHP似乎使用SHA1算法来散列密码,并且似乎只提供SHA256作为替代:

http://api.cakephp.org/view_source/security#line-86

在我将应用程序公开之前,我希望切换到更安全的密码散列解决方案,以便在切换到更安全的散列算法时节省未来的麻烦。 我已经四处寻找使用bcrypt或类似内容的一些指南,但它们似乎都适用于旧版本的Cake,或者很难实现散列。

是否有某个指南可以告诉我如何在不更改模型或控制器中的任何代码的情况下集成更好的密码哈希?

另外,还有一个小问题,为什么Cake devs在发布时只包含SHA密码哈希? 众所周知,SHA是一种破解的密码哈希算法,在我看来,这样一个声誉良好的框架不会忽略这一点。

这张票中, CakePHP的贡献者Mark Story提到在CakePHP 2.3中将支持bcrypt(尚未发布)并将成为3.0中的标准/默认值。

此外,在这篇博客文章中, Mark谈到了在CakePHP 2.0中使用bcrypt需要进行哪些更改。 看起来相对较小,但需要更改您的用户模型。

借用该帖子中的代码,Mark所做的是创建了FormAuthenticate的子类:

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

class BcryptFormAuthenticate extends FormAuthenticate {

/**
 * The cost factor for the hashing.
 *
 * @var integer
 */
    public static $cost = 10;

/**
 * Password method used for logging in.
 *
 * @param string $password Password.
 * @return string Hashed password.
 */
    protected function _password($password) {
        return self::hash($password);
    }

/**
 * Create a blowfish / bcrypt hash.
 * Individual salts could/should used to be even more secure.
 *
 * @param string $password Password.
 * @return string Hashed password.
 */
    public static function hash($password) {
        $salt = substr(Configure::read('Security.salt'), 0, 22);
        return crypt($password, '$2a$' . self::$cost . '$' . $salt);
    }
}

然后对控制器组件数组进行了更新:

<?php
public $components = array(
    'Auth' => array(
        'authenticate' => 'BcryptForm',
        // other keys.
    )
);

最后更新用户模型的beforeSave回调:

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

class User extends AppModel {
    function beforeSave() {
        if (isset($this->data['User']['password'])) {
            $this->data['User']['password'] = BcryptFormAuthenticate::hash($this->data['User']['password']);
        }
        return true;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM