2013-08-20 27 views
0

我們有幾個使用相同數據庫的應用程序。用戶密碼由cakePHP應用程序散列。我們想要做的是比較由php服務哈希和由cakePHP哈希的密碼。訪問CakePHP散列PHP服務的密碼

是否有PHP函數可以模擬CakePHP的哈希以比較密碼?如果不是,最簡單的方法是什麼?

回答

1

我認爲CakePHP的使用功能hashlib\Cake\Utility\Security.php獲得用戶的散列數據,並將其與存儲在密碼字段的哈希比較:

https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/Security.php#L107

我還要說,它通常使用PHP的sha1函數默認使用用戶的passwordSecurity.salt值(在core.php中定義)作爲輸入字符串。 爲此,你可以做這樣的事情來獲得保存在users表的password字段中的值:

sha1('cce93fda02c7f3ebf1g46c583589f1fd257e9d5d'. 'mypassword'); 

這是CakePHP的全功能,使得sha1用途:

public static function hash($string, $type = null, $salt = false) { 
    if (empty($type)) { 
     $type = self::$hashType; 
    } 
    $type = strtolower($type); 

    if ($type === 'blowfish') { 
     return self::_crypt($string, $salt); 
    } 
    if ($salt) { 
     if (!is_string($salt)) { 
      $salt = Configure::read('Security.salt'); 
     } 
     $string = $salt . $string; 
    } 

    if (!$type || $type === 'sha1') { 
     if (function_exists('sha1')) { 
      return sha1($string); 
     } 
     $type = 'sha256'; 
    } 

    if ($type === 'sha256' && function_exists('mhash')) { 
     return bin2hex(mhash(MHASH_SHA256, $string)); 
    } 

    if (function_exists('hash')) { 
     return hash($type, $string); 
    } 
    return md5($string); 
} 

您可以在CakePHP documentation中閱讀更多內容。

+0

工作!非常感謝。 – Domas