ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-10-02 23:32:25  阅读:267  来源: 互联网

标签:cakephp-2-1 php cakephp


我有一个使用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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有