簡體   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