2

how to convert Magento 1 password hash to Magento 2 password hash?

for example:

M1 password hash of qwer@123 (password)

547e98d8739f52b1ac5759d6b0ec6813:q9n6S6pewXbHANHML6TRujGOigDA5CSo

M2 password hash of qwer@123 (password)

b3ef5674b8ae2a4ce0cefb10671fb5b2546475b020548275cad2ea070f2d435c:pQJccCQInyMFaDzVyqCbYCGE56o6avDo:1

I need to convert

547e98d8739f52b1ac5759d6b0ec6813:q9n6S6pewXbHANHML6TRujGOigDA5CSo -> b3ef5674b8ae2a4ce0cefb10671fb5b2546475b020548275cad2ea070f2d435c:pQJccCQInyMFaDzVyqCbYCGE56o6avDo:1

any help would be much apreciated

2 Answers 2

4

My Try

I got reference from data-migration-tool vendor/magento/data-migration-tool/src/Migration/Step/Customer/Helper.php for changing M1 password hash to M2 password hash. I applied the same logic in my controller for testing purpose.

Custom controller:

<?php


namespace Vendor\Helloworld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;
    protected $_account;
        /**#@+
     * Exploded password hash keys
     */
    const PASSWORD_HASH = 0;
    const PASSWORD_SALT = 1;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Customer\Model\AccountManagement $account

    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_account=$account;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        //password hash from M1
        $recordAttributesData['password_hash']="b1999e4f386208d438134f3ad3d5ab21:MGPHnykFc2IJ9mxQ8tZ6vNwEghzXcezv";
        //print M2 password hash
        echo $this->upgradeCustomerHash($recordAttributesData);
    }

    /**
     * Upgrade customer hash according M2 algorithm versions
     *
     * @param array $recordAttributesData
     * @return array
     */
    private function upgradeCustomerHash($recordAttributesData)
    {
        if (isset($recordAttributesData['password_hash'])) {
            $hash = $this->explodePasswordHash($recordAttributesData['password_hash']);

            if (strlen($hash[self::PASSWORD_HASH]) == 32) {
                $recordAttributesData['password_hash'] = implode(
                    ':',
                    [$hash[self::PASSWORD_HASH], $hash[self::PASSWORD_SALT], '0']
                );
            } elseif (strlen($hash[self::PASSWORD_HASH]) == 64) {
                $recordAttributesData['password_hash'] = implode(
                    ':',
                    [$hash[self::PASSWORD_HASH], $hash[self::PASSWORD_SALT], '1']
                );
            }
        }

        return $recordAttributesData['password_hash'];
    }

    /**
     * @param string $passwordHash
     * @return array
     */
    private function explodePasswordHash($passwordHash)
    {
        $explodedPassword = explode(':', $passwordHash, 2);
        $explodedPassword[self::PASSWORD_SALT] = isset($explodedPassword[self::PASSWORD_SALT])
            ? $explodedPassword[self::PASSWORD_SALT]
            : ''
        ;
        return $explodedPassword;
    }
}

it print the password hash b1999e4f386208d438134f3ad3d5ab21:MGPHnykFc2IJ9mxQ8tZ6vNwEghzXcezv:0 after that I have updated the M2 Database, when I try to login, It allows. But after login it automatically update the password hash as 34bbb0468f2881c9f60964b66990bf26d9fb34d433b4df86bdb221148d775f5f:Oxrrm8cgdRf38fM5JzbPrI2E0VuFgZbp:1 in database, I have no issues in login.

I'm curious to know what is happening here? and which code part is responsible for this? if anyone know, please post your answer here

1
0

Might not be the exact same issues as you but I managed to fix my customer passwords by using this - https://magento.stackexchange.com/a/294312/46258

P.S. You will need to clear out generated/ and rebuild. I'm using:

cd /var/www/html
sudo rm -rf var/cache/* var/page_cache/* var/composer_home/* var/generation/* var/view_preprocessed/* pub/static/frontend/* pub/static/adminhtml/* generated/code/* generated/metadata/*
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy en_GB -f
php bin/magento setup:static-content:deploy en_US -f
sudo chown -R www-data:www-data .
php bin/magento cache:clean
sudo chown -R www-data:www-data .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.