[英]Using Md5 for password hash in Auth component of Cakephp 2.x
我有一個使用CakePhp 1.3構建的現有網站。 在該網站中,我使用MD5算法進行密碼哈希處理。
現在,我想將CakePhp版本升級到2.3.5,但是我無法使用MD5進行密碼哈希處理。
我想知道為什么我不能在CakePhp 2.x中使用MD5。 ?
md5不是用於哈希密碼的適當哈希算法,請不要使用它。 有很多參考資料解釋了原因-包括php手冊 :
為什么常見的哈希函數(例如md5()和sha1())不適合密碼?
諸如MD5,SHA1和SHA256之類的哈希算法被設計為非常快速和高效。 隨着現代技術和計算機設備的出現,“蠻力”將這些算法的輸出確定原始輸入變得微不足道了。
由於現代計算機能夠快速“逆向”這些哈希算法,因此許多安全專業人員強烈建議不要將其用於密碼哈希。
您可以使用setHash更改默認的哈希算法, 建議的密碼哈希算法是河豚:
Security::setHash('blowfish');
如果確實需要,可以將setHash
更改為使用md5。
但這不是一個好主意。
不要僅僅為了適應舊應用程序的不良安全性而損害新/更新應用程序的安全性。 除了使用與以前的應用程序相同的哈希算法(和鹽)之外,您可以使用以下邏輯(偽ish代碼):
$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.
這種邏輯並不復雜,並且避免了需要繼續使用錯誤的哈希算法。
我不建議在任何情況下都使用此代碼。 MD5是一種用於安全性的可怕的哈希算法,因為它的資源太少而無法阻止破解。 它還具有已知的漏洞。 使用bcrypt或SHA-512。
為此,您可以像這樣編輯AppController.php
:
<?php
// AppController.php
public function beforeFilter()
{
Security::setHash('md5');
}
?>
但是, 不建議這樣做,因為MD5是一種非常差的密碼哈希算法 。 您最好添加一個功能,允許用戶使用現有的md5
密碼登錄,鼓勵他們升級到新的哈希值,並且不允許任何新用戶設置MD5密碼。
相反,如果您想使用像bcrypt
這樣的安全功能,則可以執行以下操作:
<?php
// AppController.php
public function beforeFilter()
{
Security::setHash('blowfish');
}
?>
在將純文本值與哈希值進行比較時,必須保留原始哈希值作為鹽值,以保留成本參數等:
$newHash = Security::hash($newPassword, 'blowfish', $storedPassword);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.