Hashing

Introduction

The Laravel Hash facade provides secure Bcrypt and Argon2 hashing for storing user passwords. If you are using one of the Laravel application starter kits, Bcrypt will be used for registration and authentication by default.

Bcrypt is a great choice for hashing passwords because its "work factor" is adjustable, which means that the time it takes to generate a hash can be increased as hardware power increases. When hashing passwords, slow is good. The longer an algorithm takes to hash a password, the longer it takes malicious users to generate "rainbow tables" of all possible string hash values that may be used in brute force attacks against applications.

Configuration

The default hashing driver for your application is configured in your application's config/hashing.php configuration file. There are currently several supported drivers: Bcrypt and Argon2 (Argon2i and Argon2id variants).

exclamation

The Argon2i driver requires PHP 7.2.0 or greater and the Argon2id driver requires PHP 7.3.0 or greater.

Basic Usage

Hashing Passwords

You may hash a password by calling the make method on the Hash facade:

1<?php
2 
3namespace App\Http\Controllers;
4 
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\Hash;
8 
9class PasswordController extends Controller
10{
11 /**
12 * Update the password for the user.
13 *
14 * @param \Illuminate\Http\Request $request
15 * @return \Illuminate\Http\Response
16 */
17 public function update(Request $request)
18 {
19 // Validate the new password length...
20 
21 $request->user()->fill([
22 'password' => Hash::make($request->newPassword)
23 ])->save();
24 }
25}
1<?php
2 
3namespace App\Http\Controllers;
4 
5use App\Http\Controllers\Controller;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\Hash;
8 
9class PasswordController extends Controller
10{
11 /**
12 * Update the password for the user.
13 *
14 * @param \Illuminate\Http\Request $request
15 * @return \Illuminate\Http\Response
16 */
17 public function update(Request $request)
18 {
19 // Validate the new password length...
20 
21 $request->user()->fill([
22 'password' => Hash::make($request->newPassword)
23 ])->save();
24 }
25}

Adjusting The Bcrypt Work Factor

If you are using the Bcrypt algorithm, the make method allows you to manage the work factor of the algorithm using the rounds option; however, the default work factor managed by Laravel is acceptable for most applications:

1$hashed = Hash::make('password', [
2 'rounds' => 12,
3]);
1$hashed = Hash::make('password', [
2 'rounds' => 12,
3]);

Adjusting The Argon2 Work Factor

If you are using the Argon2 algorithm, the make method allows you to manage the work factor of the algorithm using the memory, time, and threads options; however, the default values managed by Laravel are acceptable for most applications:

1$hashed = Hash::make('password', [
2 'memory' => 1024,
3 'time' => 2,
4 'threads' => 2,
5]);
1$hashed = Hash::make('password', [
2 'memory' => 1024,
3 'time' => 2,
4 'threads' => 2,
5]);
lightbulb

For more information on these options, please refer to the official PHP documentation regarding Argon hashing.

Verifying That A Password Matches A Hash

The check method provided by the Hash facade allows you to verify that a given plain-text string corresponds to a given hash:

1if (Hash::check('plain-text', $hashedPassword)) {
2 // The passwords match...
3}
1if (Hash::check('plain-text', $hashedPassword)) {
2 // The passwords match...
3}

Determining If A Password Needs To Be Rehashed

The needsRehash method provided by the Hash facade allows you to determine if the work factor used by the hasher has changed since the password was hashed. Some applications choose to perform this check during the application's authentication process:

1if (Hash::needsRehash($hashed)) {
2 $hashed = Hash::make('plain-text');
3}
1if (Hash::needsRehash($hashed)) {
2 $hashed = Hash::make('plain-text');
3}

Comments

No Comments Yet

“Laravel” is a Trademark of Taylor Otwell.
The source documentation is released under MIT license. See laravel/docs on GitHub for details.
The translated documentations are released under MIT license. See cornch/laravel-docs-l10n on GitHub for details.