【问题标题】:Has the hash password function changed in magento? If so, to what?magento中的哈希密码功能是否已更改?如果是这样,对什么?
【发布时间】:2015-10-28 03:33:52
【问题描述】:

我使用的是 magento 版本 1.9.0.1。

为了切换到 magento 的目的,我需要在 magento 框架之外为客户创建一个登录功能。

我查看了 magento 用于散列和验证密码的方法,但该方法似乎不再有效。

在我用来验证 magento 之外的用户登录的代码下方。这段代码只是为了尝试概念验证,并没有在实际环境中使用,原因很明显:)。

function checkPassword($entity,$passwordInput){
    $query = mysql_query("SELECT value FROM customer_entity_varchar WHERE entity_id = '$entity' AND attribute_id = '12' LIMIT 1");
    $fetch = mysql_fetch_object($query);
    $fetch_data = explode(':',$fetch->value);
    $hashed_password = $fetch_data['0'];
    $salt = $fetch_data['1'];

    $hashInput = md5($passwordInput . $salt);
    if($hashInput == $hashed_password){
        return 'Success';
    }
    else{
        return 'Failure';
    }
}

$entity是邮箱验证后传递的entity_id,

$passwordInput是登录表单中输入的密码。

它返回失败。我对此并不感到惊讶,因为当我返回 $hashInput 并将其与 $hashed_password 进行比较时,情况就不一样了。

Magento 散列密码的方式是否已更改?还是我的代码有错误?

【问题讨论】:

  • 你解决了这个问题吗?如果是这样,您可以发送解决方案吗?
  • 是的,已经有一段时间了,但上面的代码确实有效(仅适用于客户)。我犯了一个愚蠢的错误并且发布了错误的数据($passwordInput 不正确)因此它显然返回了失败。不过不要在实际环境中使用此代码。

标签: validation magento login md5 saltedhash


【解决方案1】:

如果您签入\app\code\core\Mage\Customer\Model\Customer.php,您会发现类似这样的内容(靠近 430 行)

/**
 * Encrypt password
 *
 * @param   string $password
 * @return  string
 */
public function encryptPassword($password)
{
    return Mage::helper('core')->encrypt($password);
}

helper('core')\app\code\core\Mage\Core\Helper\Data.php

\app\code\core\Mage\Core\Helper\Data.php,你会发现:

/**
 * Encrypt data using application key
 *
 * @param   string $data
 * @return  string
 */
public function encrypt($data)
{
    if (!Mage::isInstalled()) {
        return $data;
    }
    return $this->getEncryptor()->encrypt($data);
}

getEncryptor() 函数是:

/**
 * @return Mage_Core_Model_Encryption
 */
public function getEncryptor()
{
    if ($this->_encryptor === null) {
        $encryptionModel = (string)Mage::getConfig()->getNode(self::XML_PATH_ENCRYPTION_MODEL);
        if ($encryptionModel) {
            $this->_encryptor = new $encryptionModel;
        } else {
            $this->_encryptor = Mage::getModel('core/encryption');
        }

        $this->_encryptor->setHelper($this);
    }
    return $this->_encryptor;
}

$this->_encryptor\app\code\core\Mage\Core\Model\Encryption.php 中,您可以在此文件中找到:

/**
 * Encrypt a string
 *
 * @param string $data
 * @return string
 */
public function encrypt($data)
{
    return base64_encode($this->_getCrypt()->encrypt((string)$data));
}

/**
 * Instantiate crypt model
 *
 * @param string $key
 * @return Varien_Crypt_Mcrypt
 */
protected function _getCrypt($key = null)
{
    if (!$this->_crypt) {
        if (null === $key) {
            $key = (string)Mage::getConfig()->getNode('global/crypt/key');
        }
        $this->_crypt = Varien_Crypt::factory()->init($key);
    }
    return $this->_crypt;
}

(string)Mage::getConfig()->getNode('global/crypt/key');/app/etc/local.xml 文件中。

您的变量$hashed_password 通过最后一种方法。

你的变量$hashInput也通过了?


因此,您可以更改 checkPassword() 函数:

$hashInput = md5($passwordInput . $salt);

$hashInput = encryptPassword($passwordInput);

因此,$hashInput$hashed_password 将遵循相同的方式。

【讨论】:

  • 我的函数需要在magento框架之外运行。所以它需要在不使用任何 magento 类/函数的情况下运行。
  • 如果你试试这个:$hashInput = md5($salt . $passwordInput); // 见:Mage_Core_Model_Encryption at line 74
  • 迟到总比不到好,感谢您的帮助。最终结果是我这边犯了一个愚蠢的错误,发布了错误的数据,因此返回了失败。谢谢你给我更多关于magento代码的见解!
猜你喜欢
  • 2020-12-22
  • 2013-03-24
  • 2010-12-08
  • 2012-05-06
  • 1970-01-01
  • 2015-08-12
  • 2020-12-17
  • 2012-04-08
  • 2012-08-02
相关资源
最近更新 更多