hashing passwords in yii 1
To hash passwords in Yii 1, you can use the CPasswordHelper
class. This class provides methods to hash and verify passwords securely. Here's an example of how you can use it:
// Import the CPasswordHelper class
Yii::import('system.security.CPasswordHelper');
// Hashing a password
$password = 'my_password';
$hash = CPasswordHelper::hashPassword($password);
// Verifying a password
$hashedPassword = '$2a$13$8B3cZ0hV3TCb3LkM8H4MwOqg2G2xAVI2Ff7HRdceL1lM1D4jRlN7O';
$isPasswordValid = CPasswordHelper::verifyPassword($password, $hashedPassword);
In the example above, we first import the CPasswordHelper
class using the Yii::import()
method. Then, we hash a password using the hashPassword()
method, which returns the hashed password. To verify a password, we use the verifyPassword()
method, which takes the plain password and the hashed password as arguments and returns a boolean indicating whether the password is valid or not.
It's important to note that Yii 1 uses the bcrypt algorithm for password hashing, which provides a secure way of hashing passwords. The CPasswordHelper
class takes care of the details of using bcrypt and ensures that the passwords are hashed and verified correctly.