2

I need to write php script to convert password value to password hash value.

for example convert the below password into password hash as per magento 2 format.

qwer@123 -> b3ef5674b8ae2a4ce0cefb10671fb5b2546475b020548275cad2ea070f2d435c:pQJccCQInyMFaDzVyqCbYCGE56o6avDo:1

any help would be much appreciated

1

hi you can try my code

$pwdObj = $this->_objectManager->get('Magento\Framework\Encryption\EncryptorInterface');
$string = 'qwer@123';
echo $pwdObj->getHash($string,true);
  • It does the job and answers the question @RamaChandranM – Timothy Frew Nov 16 '18 at 19:46
  • your answer is correct but please try to use without object manager. object manager is not good coding standard. :). and happy and wlcome to contribute MSE – Rama Chandran M Nov 16 '18 at 20:22
  • @RamaChandranM, then how else should we do it? – Black Oct 30 at 11:08
0

My Try

Custom Controller To Print Password Hash:

for testing I alter protected method to public, createPasswordHash in Magento\Customer\Model\AccountManagement

<?php


namespace Vendor\Helloworld\Controller\Index;

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

    protected $resultPageFactory;
    protected $_account;

    /**
     * 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="qwer@123";
        echo $this->_account->createPasswordHash($password);
        // return $this->resultPageFactory->create();
    }
}

it produce

11461f169755245382777f2cf8fcd344933851f302ad3fe4c225bb77f0988b37:BjBTkXlth0bU3rsLKGqqVRgIdo2sYULI:1 

when I update newly generated database hash value, it allow to login by password qwer@123.

References: link1, link2

If anyone have better answer, please post your answer.

  • It is throwing an error when generating the hash. Call to protected method Magento\Customer\Model\AccountManagement::createPasswordHash() – Vinay Sikarwar Oct 5 '17 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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