2010-06-14 40 views
3

我是Zend框架的新手,並且希望構建一個密碼安全性非常高的應用程序。我一直在嘗試遵循用戶指南中有關密碼醃製的內容,但迄今爲止還沒有任何運氣。我安裝我的數據庫和表適配器(如在Zend框架的網站上的文檔中描述,但它似乎沒有結束的例子(或我沒有遵守不夠好)我已經開始用!Zend框架中的醃製示例

$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter, 
      'users', 
      'username', 
'password',           "MD5(CONCAT('".Zend_Registry::get('staticSalt')."', ?, password_salt))" 
    ); 

但是從這裏,什麼是與密碼鹽呢?我只是需要一個例子,我會離開!沒有人有一個例子或點我在正確的方向?

非常感謝!

+0

Thewre是每個答案左側的投票選項下面這個小圖標。將答覆標記爲「正確答案」。這有助於找到已回答的帖子,如果您想回答,請找到需要答案的人。這兩個答案都很不錯。你可能想要「接受」其中的一個。這將是一件好事。 – 2011-12-22 13:55:44

回答

1

驗證方法:

/** 
* Authenticate user with specified identity and credential 
* 
* most used case is authenticate user inline in script 
* 
* @param string $identity 
* @param string $credential 
* @return Zend_Auth_Result 
*/ 
public function authenticate ($identity, $credential) 
{ 
    $auth = Zend_Auth::getInstance(); 
    $adapter = $this->getAdapter(); 
    $adapter->setIdentity($identity) 
      ->setCredential(self::passwordHash($credential)); 

    $config = Singular_Runtime::extract('config'); 
    $isActiveCol = $config->resources->auth->columns->is_active; 
    $isActiveAllowVal = $config->resources->auth->is_active->allow_value; 

    /** 
    * @see APPLICATION_PATH/configs/application.ini -> resources.auth 
    */ 
    if (null != $isActiveCol && null != $isActiveAllowVal) { 
     $adapter->getDbSelect()->where("{$isActiveCol} = ?", $isActiveAllowVal); 
    } 

    Singular_Event::dispatch('beforeAuth', array(
     'auth' => $auth, 'adapter' => $adapter 
    )); 

    $result = $auth->authenticate($adapter); 

    if ($result->isValid()) { 
     $auth->getStorage()->write($adapter->getResultRowObject()); 

     Singular_Event::dispatch('afterAuth', array(
      'auth' => $auth, 'adapter' => $adapter 
     )); 
    } 

    return $result; 
} 

和密碼哈希生成方法:

/** 
* Password hash generator 
* 
* @static 
* @param string $password 
* @return string 
*/ 
public static function passwordHash ($password) 
{ 
    $password = strtolower($password); 

    return md5(
     str_repeat(
      md5($password) . strrev($password) . sha1($password), 
      strlen($password) 
     ) 
    ); 
}