简体   繁体   English

如何在drupal8中解密和加密用户密码

[英]how to decrypt and encrypt the user password in drupal8

from mobile application when i will pass username and password if value will match with the db it should show all the details of user or else username & password invalid.For this i am generating the api. 从移动应用程序中,如果值将与数据库匹配,则我将传递用户名和密码,它将显示用户的所有详细信息,否则用户名和密码无效。为此,我正在生成api。 How to encrypt or decrypt the password in drupal8? 如何在drupal8中加密或解密密码?

class Userverifier extends ResourceBase
{
    public function get($username = 'NULL',$password = 'NULL') {

        $result = \Drupal::database()->select('users_field_data`', 'n')
            ->fields('n', array('uid', 'name', 'pass', 'mail'))
            ->condition('n.name', $username, '=')
            //->condition('n.pass', $decrypted_pass, '=')
            ->execute()->fetchAllAssoc('uid');

        $rows = array();
        foreach ($result as $row => $content) {
            $rows[] = array('data' => array($content->uid, $content->name, $content->pass, $content->mail));
        }
        return new ResourceResponse($rows);
    }
}

There is a password interface for this in Drupal: Drupal中有一个密码界面:

https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Password%21PasswordInterface.php/interface/PasswordInterface/8.2.x https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Password%21PasswordInterface.php/interface/PasswordInterface/8.2.x

There is a similar question on the Drupal StackExchange that gives examples of how to use that interface - not perfect for what you're asking but will help. Drupal StackExchange上有一个类似的问题,提供了有关如何使用该接口的示例-并非完美满足您的要求,但会有所帮助。

https://drupal.stackexchange.com/questions/195669/how-do-i-check-if-the-current-password-that-the-user-gives-in-the-password-reset https://drupal.stackexchange.com/questions/195669/how-do-i-check-if-the-current-password-that-the-user-gives-in-the-password-reset

This actually uses the user class as opposed to trying to drive the database yourself - something I'd recommend as it's more "future proof". 实际上,这是使用用户类,而不是尝试自己驱动数据库-我建议这样做,因为它更“面向未来”。

It is not possible to decrypt the password in a Drupal 8 database! 无法在Drupal 8数据库中解密密码!

Use code like this to verify the credentials: 使用如下代码来验证凭据:

$account = User::load($theUsersId);
$saltedPwd = $account->getPassword();
$match = $this->passwordService->check($givenPwd, $saltedPwd);

You should inject passwordService, or else use Drupal::service('password') . 您应该注入passwordService,否则使用Drupal::service('password')

This code helped me a lot. 这段代码对我有很大帮助。

$password_hasher = \Drupal::service('password');
$match = $password_hasher->check($currpwd, $user->getPassword());

$match will be bool (true or false). $match将是布尔值(对或错)。 Hope this will be helpful to you as well. 希望这对您也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM