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


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 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 Nov 16 '18 at 20:22
  • @RamaChandranM, then how else should we do it?
    – Black
    Oct 30 '19 at 11:08

My Try

Custom Controller To Print Password Hash:

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


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;

     * Execute view action
     * @return \Magento\Framework\Controller\ResultInterface
    public function execute()
        echo $this->_account->createPasswordHash($password);
        // return $this->resultPageFactory->create();

it produce


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() 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.