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