编程语言
首页 > 编程语言> > 在Cakephp 2.x的Auth组件中使用Md5进行密码哈希

在Cakephp 2.x的Auth组件中使用Md5进行密码哈希

作者:互联网

我有一个使用CakePhp 1.3构建的现有网站.在那个网站上,我使用MD5算法进行密码哈希.

现在我想将CakePhp版本升级到2.3.5,但我无法使用MD5作为密码哈希.

我想知道为什么我不能在CakePhp 2.x中使用MD5. ?

解决方法:

不要将md5用于密码

md5不是哈希密码的合适哈希算法,不要使用它.有很多很多参考资料可以解释为什么 – 包括the php manual

Why are common hashing functions such as md5() and sha1() unsuitable for passwords?

Hashing algorithms such as MD5, SHA1 and SHA256 are designed to be very fast and efficient. With modern techniques and computer equipment, it has become trivial to “brute force” the output of these algorithms, in order to determine the original input.

Because of how quickly a modern computer can “reverse” these hashing algorithms, many security professionals strongly suggest against their use for password hashing.

如何更改默认哈希算法

您可以使用setHash更改默认哈希算法,recommended hash algorithm for passwords是河豚:

Security::setHash('blowfish');

如何处理现有密码

如果你真的想,你可以改变setHash来使用md5.

但这不是一个好主意.

不要为了适应旧的应用程序的安全性而损害新的/更新的应用程序的安全性.您可以使用以下逻辑(伪代码),而不是使用与前一个应用程序相同的哈希算法(和盐):

$username = $this->data['User']['username'];
$plainText = $this->data['User']['password'];

$user = current($this->User->findByUsername($username));

Security::setHash('blowfish');
$blowfished = Security::hash($plainText, 'blowfish', $user['password']);

if ($blowfished === $user['password']) {
    return true; // user exists, password is correct
}

$oldSalt = Configure::read('configure.this');
$md5ed = Security::hash($plainText, 'md5', $oldSalt);

if ($md5ed === $user['password']) {
    $this->User->id = $user['id'];

    $blowfished = Security::hash($plainText);
    $this->User->saveField('password', $blowfished);

    return true; // user exists, password now updated to blowfish
}

return false; // user's password does not exist.

这种逻辑并不复杂,并且无需继续使用错误的哈希算法.

标签:cakephp-2-1,php,cakephp
来源: https://codeday.me/bug/20191002/1845017.html