How to encrypt password in Magento 2?
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?
magento-2.1 password encryption-key
add a comment |
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?
magento-2.1 password encryption-key
add a comment |
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?
magento-2.1 password encryption-key
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?
magento-2.1 password encryption-key
magento-2.1 password encryption-key
edited Dec 7 '17 at 21:19
Rafael Corrêa Gomes
4,25722962
4,25722962
asked Dec 29 '16 at 7:16
SeefanSeefan
194212
194212
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Firstly my usual comment would be, do not encrypt a password!
- Encryption = A two-way process, scrambling and then unscrambling data at a later date.
- Hashing = A one-way process, create an encrypted looking string from a given input.
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
use MagentoFrameworkEncryptionEncryptorInterface
$encrypt = $this->encryptor->encrypt($data);
$decrypt = $this->encryptor->decrypt($data);
To hash a password
use MagentoFrameworkEncryptionEncryptorInterface
$hash = $this->encryptor->hash($password);
#Hash is persisted in the database when you next login use
#provided password variable and compare with stored hash
$bool = $this->encryptor->validateHash($password, $hash);
For more thorough example usage of password, hashing have a look a
MagentoCustomerModelCustomer::setPassword
MagentoCustomerModelCustomer::validatePassword
add a comment |
I searched a little and found out that magento2 uses EncryptorInterface class to encrypt and decrypt the password.
You can use it this way:
use MagentoFrameworkEncryptionEncryptorInterface as Encryptor;
in the construct function :
$this->encryptor = $encryptor;
then call encrypt function to encrypt:
$encrypt = $this->encryptor->encrypt($password);
and to decrypt:
$decrypt = $this->encryptor->decrypt($password);
add a comment |
Here is my working script, creating hashed and/or encrypted passwords from cleartext passwords:
<?php
// create a "tools" directory inside the "pub" directory and copy this script into pub/tools
require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['pass'])) {
define('PASS', $_GET['pass']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the password you want to hash / encrypt as a pass parameter (?pass=myB1rthDate)');
} elseif (!isset($argv[1])) {
die('Please add the password you want to hash / encrypt enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('PASS', $argv[1]);
}
class MyEncryptor extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
{
private $encryptor;
public function __construct(
MagentoFrameworkAppState $state,
MagentoFrameworkEncryptionEncryptorInterface $encryptor,
MagentoFrameworkAppResponseHttp $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
echo 'Hashed value: ' . $this->encryptor->hash(PASS) . PHP_EOL;
echo 'Encrypted value: ' . $this->encryptor->encrypt(PASS) . PHP_EOL;
return $this->_response;
}
}
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
Run it like this from the cli:
php ./encryptPassword.php "myCleartextPassword"
This script should not be used on a server, because it requires the password as a command line argument, which is then stored in the cli history.
I want to use it for writing credentials directly encrypted into the magento database during deployments.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f152297%2fhow-to-encrypt-password-in-magento-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Firstly my usual comment would be, do not encrypt a password!
- Encryption = A two-way process, scrambling and then unscrambling data at a later date.
- Hashing = A one-way process, create an encrypted looking string from a given input.
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
use MagentoFrameworkEncryptionEncryptorInterface
$encrypt = $this->encryptor->encrypt($data);
$decrypt = $this->encryptor->decrypt($data);
To hash a password
use MagentoFrameworkEncryptionEncryptorInterface
$hash = $this->encryptor->hash($password);
#Hash is persisted in the database when you next login use
#provided password variable and compare with stored hash
$bool = $this->encryptor->validateHash($password, $hash);
For more thorough example usage of password, hashing have a look a
MagentoCustomerModelCustomer::setPassword
MagentoCustomerModelCustomer::validatePassword
add a comment |
Firstly my usual comment would be, do not encrypt a password!
- Encryption = A two-way process, scrambling and then unscrambling data at a later date.
- Hashing = A one-way process, create an encrypted looking string from a given input.
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
use MagentoFrameworkEncryptionEncryptorInterface
$encrypt = $this->encryptor->encrypt($data);
$decrypt = $this->encryptor->decrypt($data);
To hash a password
use MagentoFrameworkEncryptionEncryptorInterface
$hash = $this->encryptor->hash($password);
#Hash is persisted in the database when you next login use
#provided password variable and compare with stored hash
$bool = $this->encryptor->validateHash($password, $hash);
For more thorough example usage of password, hashing have a look a
MagentoCustomerModelCustomer::setPassword
MagentoCustomerModelCustomer::validatePassword
add a comment |
Firstly my usual comment would be, do not encrypt a password!
- Encryption = A two-way process, scrambling and then unscrambling data at a later date.
- Hashing = A one-way process, create an encrypted looking string from a given input.
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
use MagentoFrameworkEncryptionEncryptorInterface
$encrypt = $this->encryptor->encrypt($data);
$decrypt = $this->encryptor->decrypt($data);
To hash a password
use MagentoFrameworkEncryptionEncryptorInterface
$hash = $this->encryptor->hash($password);
#Hash is persisted in the database when you next login use
#provided password variable and compare with stored hash
$bool = $this->encryptor->validateHash($password, $hash);
For more thorough example usage of password, hashing have a look a
MagentoCustomerModelCustomer::setPassword
MagentoCustomerModelCustomer::validatePassword
Firstly my usual comment would be, do not encrypt a password!
- Encryption = A two-way process, scrambling and then unscrambling data at a later date.
- Hashing = A one-way process, create an encrypted looking string from a given input.
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
use MagentoFrameworkEncryptionEncryptorInterface
$encrypt = $this->encryptor->encrypt($data);
$decrypt = $this->encryptor->decrypt($data);
To hash a password
use MagentoFrameworkEncryptionEncryptorInterface
$hash = $this->encryptor->hash($password);
#Hash is persisted in the database when you next login use
#provided password variable and compare with stored hash
$bool = $this->encryptor->validateHash($password, $hash);
For more thorough example usage of password, hashing have a look a
MagentoCustomerModelCustomer::setPassword
MagentoCustomerModelCustomer::validatePassword
edited Dec 7 '17 at 21:19
Rafael Corrêa Gomes
4,25722962
4,25722962
answered Dec 29 '16 at 11:55
Luke RodgersLuke Rodgers
3,08211639
3,08211639
add a comment |
add a comment |
I searched a little and found out that magento2 uses EncryptorInterface class to encrypt and decrypt the password.
You can use it this way:
use MagentoFrameworkEncryptionEncryptorInterface as Encryptor;
in the construct function :
$this->encryptor = $encryptor;
then call encrypt function to encrypt:
$encrypt = $this->encryptor->encrypt($password);
and to decrypt:
$decrypt = $this->encryptor->decrypt($password);
add a comment |
I searched a little and found out that magento2 uses EncryptorInterface class to encrypt and decrypt the password.
You can use it this way:
use MagentoFrameworkEncryptionEncryptorInterface as Encryptor;
in the construct function :
$this->encryptor = $encryptor;
then call encrypt function to encrypt:
$encrypt = $this->encryptor->encrypt($password);
and to decrypt:
$decrypt = $this->encryptor->decrypt($password);
add a comment |
I searched a little and found out that magento2 uses EncryptorInterface class to encrypt and decrypt the password.
You can use it this way:
use MagentoFrameworkEncryptionEncryptorInterface as Encryptor;
in the construct function :
$this->encryptor = $encryptor;
then call encrypt function to encrypt:
$encrypt = $this->encryptor->encrypt($password);
and to decrypt:
$decrypt = $this->encryptor->decrypt($password);
I searched a little and found out that magento2 uses EncryptorInterface class to encrypt and decrypt the password.
You can use it this way:
use MagentoFrameworkEncryptionEncryptorInterface as Encryptor;
in the construct function :
$this->encryptor = $encryptor;
then call encrypt function to encrypt:
$encrypt = $this->encryptor->encrypt($password);
and to decrypt:
$decrypt = $this->encryptor->decrypt($password);
edited Dec 7 '17 at 21:20
Rafael Corrêa Gomes
4,25722962
4,25722962
answered Dec 29 '16 at 10:57
SeefanSeefan
194212
194212
add a comment |
add a comment |
Here is my working script, creating hashed and/or encrypted passwords from cleartext passwords:
<?php
// create a "tools" directory inside the "pub" directory and copy this script into pub/tools
require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['pass'])) {
define('PASS', $_GET['pass']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the password you want to hash / encrypt as a pass parameter (?pass=myB1rthDate)');
} elseif (!isset($argv[1])) {
die('Please add the password you want to hash / encrypt enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('PASS', $argv[1]);
}
class MyEncryptor extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
{
private $encryptor;
public function __construct(
MagentoFrameworkAppState $state,
MagentoFrameworkEncryptionEncryptorInterface $encryptor,
MagentoFrameworkAppResponseHttp $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
echo 'Hashed value: ' . $this->encryptor->hash(PASS) . PHP_EOL;
echo 'Encrypted value: ' . $this->encryptor->encrypt(PASS) . PHP_EOL;
return $this->_response;
}
}
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
Run it like this from the cli:
php ./encryptPassword.php "myCleartextPassword"
This script should not be used on a server, because it requires the password as a command line argument, which is then stored in the cli history.
I want to use it for writing credentials directly encrypted into the magento database during deployments.
add a comment |
Here is my working script, creating hashed and/or encrypted passwords from cleartext passwords:
<?php
// create a "tools" directory inside the "pub" directory and copy this script into pub/tools
require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['pass'])) {
define('PASS', $_GET['pass']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the password you want to hash / encrypt as a pass parameter (?pass=myB1rthDate)');
} elseif (!isset($argv[1])) {
die('Please add the password you want to hash / encrypt enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('PASS', $argv[1]);
}
class MyEncryptor extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
{
private $encryptor;
public function __construct(
MagentoFrameworkAppState $state,
MagentoFrameworkEncryptionEncryptorInterface $encryptor,
MagentoFrameworkAppResponseHttp $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
echo 'Hashed value: ' . $this->encryptor->hash(PASS) . PHP_EOL;
echo 'Encrypted value: ' . $this->encryptor->encrypt(PASS) . PHP_EOL;
return $this->_response;
}
}
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
Run it like this from the cli:
php ./encryptPassword.php "myCleartextPassword"
This script should not be used on a server, because it requires the password as a command line argument, which is then stored in the cli history.
I want to use it for writing credentials directly encrypted into the magento database during deployments.
add a comment |
Here is my working script, creating hashed and/or encrypted passwords from cleartext passwords:
<?php
// create a "tools" directory inside the "pub" directory and copy this script into pub/tools
require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['pass'])) {
define('PASS', $_GET['pass']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the password you want to hash / encrypt as a pass parameter (?pass=myB1rthDate)');
} elseif (!isset($argv[1])) {
die('Please add the password you want to hash / encrypt enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('PASS', $argv[1]);
}
class MyEncryptor extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
{
private $encryptor;
public function __construct(
MagentoFrameworkAppState $state,
MagentoFrameworkEncryptionEncryptorInterface $encryptor,
MagentoFrameworkAppResponseHttp $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
echo 'Hashed value: ' . $this->encryptor->hash(PASS) . PHP_EOL;
echo 'Encrypted value: ' . $this->encryptor->encrypt(PASS) . PHP_EOL;
return $this->_response;
}
}
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
Run it like this from the cli:
php ./encryptPassword.php "myCleartextPassword"
This script should not be used on a server, because it requires the password as a command line argument, which is then stored in the cli history.
I want to use it for writing credentials directly encrypted into the magento database during deployments.
Here is my working script, creating hashed and/or encrypted passwords from cleartext passwords:
<?php
// create a "tools" directory inside the "pub" directory and copy this script into pub/tools
require '../../app/bootstrap.php';
if (php_sapi_name() !== 'cli' && isset($_GET['pass'])) {
define('PASS', $_GET['pass']);
} elseif (php_sapi_name() !== 'cli') {
die('Please add the password you want to hash / encrypt as a pass parameter (?pass=myB1rthDate)');
} elseif (!isset($argv[1])) {
die('Please add the password you want to hash / encrypt enclosed IN DOUBLE QUOTES as a parameter.' . PHP_EOL);
} else {
define('PASS', $argv[1]);
}
class MyEncryptor extends MagentoFrameworkAppHttp implements MagentoFrameworkAppInterface
{
private $encryptor;
public function __construct(
MagentoFrameworkAppState $state,
MagentoFrameworkEncryptionEncryptorInterface $encryptor,
MagentoFrameworkAppResponseHttp $response
) {
$this->_response = $response;
$this->encryptor = $encryptor;
$state->setAreaCode('adminhtml');
}
function launch()
{
echo 'Hashed value: ' . $this->encryptor->hash(PASS) . PHP_EOL;
echo 'Encrypted value: ' . $this->encryptor->encrypt(PASS) . PHP_EOL;
return $this->_response;
}
}
$bootstrap = MagentoFrameworkAppBootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('MyEncryptor');
$bootstrap->run($app);
Run it like this from the cli:
php ./encryptPassword.php "myCleartextPassword"
This script should not be used on a server, because it requires the password as a command line argument, which is then stored in the cli history.
I want to use it for writing credentials directly encrypted into the magento database during deployments.
edited yesterday
answered Dec 11 '18 at 22:20
wherewhere
383112
383112
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f152297%2fhow-to-encrypt-password-in-magento-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown