I'm working on a manufacturer extension. I added a password field to the form in admin side. How to encrypt that password to save it to the DB.
In Magento 1 we encrypt it this way:
Mage::getModel('core/encryption')->encrypt($data['password'])
How to do the same on Magento 2?
Best Answer
Firstly my usual comment would be, do not encrypt a password!
Hashing is not encryption, once you have something hashed it cannot be unhashed. Think of it like this you can
md5
hash the entire contents of "Harry Potter and the Philosopher's Stone" and end up with a 32 character string, there is no way to get from that 32 characters back to the full book.Hashing is usually preferable when storing passwords as it means you don't actually ever store the password but merely the result of its hash meaning if you're ever compromised your users can feel a little safer about the whole ordeal.
Anyway, to encrypt data
To hash a password
For more thorough example usage of password, hashing have a look a
Magento\Customer\Model\Customer::setPassword
Magento\Customer\Model\Customer::validatePassword