Skip to main content

Cakephp 4: How to convert string to hash password


CakePHP 4: How to convert string to hash password




This tutorial works for both CakePHP  3 and CakePHP 4. Sometimes we want to add custom login or wants to add custom login  or we want client side password hashing using cakephp Hash functions , so for that we need to use DefaultPasswordHasher which provides inbuilt functionality to check two passwords , compare it or convert a string to Hash etc.


To use password hashing please follow below steps:-

Step 1
Call a class after the namespace.


use Cake\Auth\DefaultPasswordHasher;


Step 2

Now we can use functions of
DefaultPasswordHasher().  Use below code to change a simple string password to a secure string and its non reversible .

$password = "My Password String";
$hasher = new DefaultPasswordHasher();
$newPassword = $hasher->hash($password);

 Now if you print $newPassword to test its working or not, then you will get it an unique hash everytime on refresh, because CakePHP use different mechanism to handle hash that is more secure. So now you can check with a password or compare it based on your requirement.

Thanks for reading :)


 Related Links

How to change a simple string to a secure password hash

Cakephp 3 How to use DefaultPasswordHasher()


 

 

Comments