您给出的示例确实使用了存储在数据库中的盐。只要盐存储在名为“盐”的字段中的每一行中,它就会起作用。如果盐不在数据库中而是在 PHP 变量中,则代码将更像:
->setCredentialTreatment("SHA1(CONCAT(?, '$salt'))")
至于使用 SHA512,这可能有点棘手。假设您使用的是 MySQL,在这种情况下 SHA1() 是一个 MySQL 函数,据我所知,MySQL 没有 SHA512 的函数,PHP 也没有(编辑:我对后者是错误的,请参阅评论)。因此,您必须实现自己的 PHP SHA512 函数,首先从数据库中为用户加载盐,对结果进行哈希处理,并且不对 setCredentialTreatment 中的变量做任何事情。
正如其他答案所建议的那样,您可能想为此编写自己的 Zend_Auth_Adapter 。auth 适配器是一个处理身份验证的类,大概在您使用 Zend_Auth_Adapter_DbTable 的那一刻。您可以在手册中找到有关身份验证适配器的更多信息:http: //framework.zend.com/manual/en/zend.auth.introduction.html
这是一个例子:
class My_Auth_Adapter extends Zend_Auth_Adapter_DbTable
{
public function authenticate()
{
// load salt for the given identity
$salt = $this->_zendDb->fetchOne("SELECT salt FROM {$this->_tableName} WHERE {$this->_identityColumn} = ?", $this->_identity);
if (!$salt) {
// return 'identity not found' error
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $this->_identity);
}
// create the hash using the password and salt
$hash = ''; // SET THE PASSWORD HASH HERE USING $this->_credential and $salt
// replace credential with new hash
$this->_credential = $hash;
// Zend_Auth_Adapter_DbTable can do the rest now
return parent::authenticate();
}
}