Zend_Auth setCredentialTreatment 处理

发布于 2024-09-24 07:50:23 字数 563 浏览 7 评论 0原文

我使用 Zend_AuthsetCredentialTreatment 来设置哈希方法和盐。我看到所有示例都在执行类似的操作,其中 salt 似乎是作为文本插入的。

->setCredentialTreatment('SHA1(CONCAT(?,salt))'

但我的盐存储在数据库中。我可以先检索它,然后在 setCredentialTreatment 中使用它,但有没有办法可以定义它直接作为字段名称,因此 setCredentialTreatment 会知道从该字段获取它,就像我们为用户名或密码定义字段名称的方式一样

->setCredentialColumn('password')

我遇到的一个附带问题是我我想使用 SHA512 而不是 SHA1。这可能吗?我看到的所有示例都使用 SHA1,

我应该说我对 zend 还很陌生,并且正在移植现有的应用程序,所以请放心使用。答案。

I'm using Zend_Auth with setCredentialTreatment to set the hash method and salt. I see all examples doing something like this, where the salt seems to be inserted as a text.

->setCredentialTreatment('SHA1(CONCAT(?,salt))'

but my salt is stored in the database. I could retrieve it first then use it in setCredentialTreatment but is there a way I could define it directly as a field name, so setCredentialTreatment would know to get it from that field? sort of like the way we define the field name for the username or password

->setCredentialColumn('password')

A side issue I'm having is that I'd like to use SHA512 not SHA1. Is this possible or is it not available? All the examples I see using SHA1.

I should say I'm fairly new to zend and am porting an existing application, so please go easy on me with the answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反话 2024-10-01 07:50:23

您给出的示例确实使用了数据库中存储的盐。只要盐存储在名为“盐”的字段中的每一行中,它就可以工作。如果盐不在数据库中而是在 PHP 变量中,则代码将更像:

->setCredentialTreatment("SHA1(CONCAT(?, '$salt'))")

至于使用 SHA512,这可能有点棘手。假设你使用 MySQL,在这种情况下 SHA1() 是一个 MySQL 函数,据我所知,MySQL 没有 SHA512 函数,PHP 也没有(编辑:我对后者错了,请参阅评论)。因此,您必须实现自己的 PHP SHA512 函数,首先从数据库中加载用户的盐,对结果进行哈希处理,并且不对 setCredentialTreatment 中的变量执行任何操作。

正如另一个答案所建议的,您可能想为此编写自己的 Zend_Auth_Adapter 。身份验证适配器是一个处理身份验证的类,大概在您使用 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();
    }
}

The example you've given does use the salt as stored in the database. It will work as long as the salt is stored in each row in a field called 'salt'. If the salt was not in the DB and in a PHP variable instead, the code would be something more like:

->setCredentialTreatment("SHA1(CONCAT(?, '$salt'))")

As for using SHA512, this might be a little trickier. Assuming you're using MySQL, SHA1() in this case is a MySQL function, and MySQL does not have a function for SHA512 as far as I can tell, and neither does PHP (edit: I was wrong about the latter, see comments). So you'll have to implement your own PHP SHA512 function, load the salt for the user out of the DB first, hash the result and not do anything to the variable in setCredentialTreatment.

As the other answer suggested you might want to write your own Zend_Auth_Adapter for this. An auth adapter is a class that handles authentication, presumably at the moment you're using Zend_Auth_Adapter_DbTable. You can find some more info about auth adapters in the manual: http://framework.zend.com/manual/en/zend.auth.introduction.html

Here's an example:

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();
    }
}
风追烟花雨 2024-10-01 07:50:23

您可以编写自己的Zend_Auth_Adapter

You can write your own Zend_Auth_Adapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文