Coder Perfect

How to make a hashed password in Laravel

Problem

For Laravel, I’m attempting to build a hashed password. Someone suggested that I use the Laravel hash helper, but I can’t seem to find it, or I’m looking in the incorrect place.

How can I make a hashed password in Laravel? And where are you?

Edit: I know what the code is, but I’m not sure where or how to utilize it, thus the hashed password is returned. I can manually insert the hashed password into the database if I get the hashed password.

Asked by Graham

Solution #1

In Laravel, hashing a password with Bcrypt:

$password = Hash::make('yourpassword');

This will generate a password that has been hashed. You can utilize it in your controller or even a model. For example, if a user sends a password to your controller via a form using the POST method, you can hash it using something like this:

$password = Input::get('passwordformfield'); // password is form field
$hashed = Hash::make($password);

The hashed password will be stored in $hashed. Basically, you’ll do it when creating/registering a new user. For example, if a user submits information such as name, email, username, and password via a form, you’ll hash the password after validating the data before inserting it into the database. Read the manual for additional information.

Update:

$password = 'JohnDoe';
$hashedPassword = Hash::make($password);
echo $hashedPassword; // $2y$10$jSAr/RwmjhwioDlJErOk9OQEO7huLz9O6Iuf/udyGbHPiTNuB3Iuy

As a result, you’ll save the $hashedPassword to the database. If you’re still puzzled, I recommend reading some tutorials, watching some screencasts on laracasts.com and tutsplus.com, and reading a book on Laravel, which is a free ebook that you can download.

Update: Since the OP wants to encrypt passwords manually using Laravel Hash without using any classes or forms, here’s an alternative method using artisan tinker from the command prompt:

// Also one can use bcrypt
$password = bcrypt('JohnDoe');

Answered by The Alpha

Solution #2

Laravel 5 uses bcrypt. So, you can do this as well.

$hashedpassword = bcrypt('plaintextpassword');

You can store the output to the password field of your database table.

Fn Ref: bcrypt

Answered by Nagendra Rao

Solution #3

For storing user passwords, the Laravel Hash facade uses safe Bcrypt hashing.

Two things were required for basic usage:

Include the Facade in your file first.

use Illuminate\Support\Facades\Hash;

To generate a password, use the Make Method.

$hashedPassword = Hash::make($request->newPassword);

and you can use the following code to match the Hashed string:

Hash::check($request->newPasswordAtLogin, $hashedPassword)

More information about Hashing may be found in the Laravel page linked below: https://laravel.com/docs/5.5/hashing

Answered by Prashant Barve

Solution #4

$password = Input::get('password_from_user'); 
$hashed = Hash::make($password); // save $hashed value
// $user is database object
// $inputs is Input from user
if( \Illuminate\Support\Facades\Hash::check( $inputs['password'], $user['password']) == false) {
  // Password is not matching 
} else {
  // Password is matching 
}

Answered by Somnath Muluk

Solution #5

If you want to learn more about how Laravel works, check out the entire class on Github: https://github.com/illuminate/hashing/blob/master/BcryptHasher.php.

However, there are three PHP methods that are involved in this:

$pasword = 'user-password';
// To create a valid password out of laravel Try out!
$cost=10; // Default cost
$password = password_hash($pasword, PASSWORD_BCRYPT, ['cost' => $cost]);

// To validate the password you can use
$hash = '$2y$10$NhRNj6QF.Bo6ePSRsClYD.4zHFyoQr/WOdcESjIuRsluN1DvzqSHm';

if (password_verify($pasword, $hash)) {
   echo 'Password is valid!';
} else {
   echo 'Invalid password.';
}

//Finally if you have a $hash but you want to know the information about that hash. 
print_r( password_get_info( $password_hash ));

The hashed password is the same as the bcrypt password for Laravel 5.x. There is no need to provide salt or cost; it will use the default values.

Those methods have been implemented in the laravel class, however if you want to learn more, go to http://php.net/manual/en/function.password-hash.php.

Answered by Jathin Prasad

Post is based on https://stackoverflow.com/questions/22846897/how-to-create-a-laravel-hashed-password