-1

How to encrypt the database password ?

return [
  'class' => 'yii\db\Connection',
  'dsn' => 'pgsql:host=localhost;port=5432;dbname=database',
  'username' => 'user',
  'password' => 'abcPassword',
  'charset' => 'utf8',
];
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
kasmawati
  • 51
  • 2
  • 9

2 Answers2

0

Yii2 comes with user module in advanced setup. See how it store user passwords in encrypted way.

You can use setPassword() method in User Model to get hashed passwords.

public function setPassword($password)
{
    $this->password_hash = Yii::$app->security->generatePasswordHash($password);
}

and call this method before saving model data.

public function signup()
{
    if ($this->validate()) {
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        if ($user->save()) {
            return $user;
        }
    }
    return null;
}

Also look at the Yii2 doc for passwords =>http://www.yiiframework.com/doc-2.0/guide-security-passwords.html and authentication.=> http://www.yiiframework.com/doc-2.0/guide-security-authentication.html

vijay nathji
  • 1,608
  • 13
  • 23
  • Actually what I mean is to encrypt the password that lets you connect to the database. – kasmawati Jun 06 '16 at 05:31
  • http://stackoverflow.com/questions/5089841/two-way-encryption-i-need-to-store-passwords-that-can-be-retrieved This link might be helful to you – vijay nathji Jun 06 '16 at 05:39
0

Try Via This way.. I didn't tasted it.. but might be helpful to you.

$database = 'mydb';
    $username = 'abc';
    $password = '1234';

$config['components'] = [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=192.168.*.*;dbname=' . $database,
        'username' => $username,

        'password' => md5($password);    // Try this 



        'password' => Yii::$app->security->generatePasswordHash($password); // Or Else try this

        'charset' => 'utf8',
    ]

    return config;
vijay nathji
  • 1,608
  • 13
  • 23