At times, you might want to generate a random password for each user. Very simple! The following is about the simplest function in laravel which is how to generate random password in laravel. Then hash
- First we will see how to generate random string.
- Then how to hash it.
How to generate random string
For this, Laravel has a variety of global “helper” PHP functions. One of these functions are
$randString = str_random(10);
This will give you a random 10 digit string like the following:
iGed7JqIp0 qXP51g0hYH QP5b9Z71Kv Q017CEZmoG
How To Hash
Finally, how can we hash the randomly generated strings. Hashing a string in laravel has been made too easy. Laravel has as secure Bcrypt hashing. It is found in laravel
Hash facade. Here is how to use:
Before all don’t forget to import
Then hash the randomly generated string like this:
$hashedString = Hash::make($randString);
This will generate something like the following:
As a plus, What if you want to check a string may be a password against a hash. Also this is very simple. Just do it as follows using the
$result = Hash::check('your string', $hashedString)
check function verifies that whether a given string corresponds to a given hash or not.