gpt4 book ai didi

php - 在 CakePHP 中使用更安全的散列算法

转载 作者:可可西里 更新时间:2023-11-01 13:19:47 25 4
gpt4 key购买 nike

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

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

我想在公开我的应用程序之前切换到更安全的密码哈希解决方案,以免将来切换到更安全的哈希算法时出现麻烦。我四处寻找有关使用 bcrypt 或类似内容的一些指南,但它们似乎都适用于旧版本的 Cake,或者散列算法实现不佳。

有没有指南可以告诉我如何在不更改模型或 Controller 中的任何代码的情况下集成更好的密码散列?

另外,还有一个小问题,为什么 Cake 开发者在他们的版本中只包含 SHA 密码散列?众所周知,SHA 是一种损坏的密码哈希算法,在我看来,这样一个信誉良好的框架不会忽视这一点。

最佳答案

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

另外,在 this blog post 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);
}
}

然后更新 Controller 组件数组:

<?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;
}

}

关于php - 在 CakePHP 中使用更安全的散列算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11681535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com