Laravel Hashing


Laravel Hashing – The Laravel Hashing is used to provide a secure Bcrypt hashing for storing user password. When we create authentication we use hash facade for secure the password.


Laravel Hashing.

Let us understand how to use laravel Hashing.

Function:-

There are some followings function available in laravel Hashing.

  • 1. Introduction.
  • 2. Basic Usage.

1. Introduction.

The Laravel hash facade provides secure authentication for storing user password. If you are using the built in LoginController and RegisterController classes which is included with laravel application. They will automatically use Bcrypt for registration and authentication.

2. Basic Usage.

You can hash a password by calling the make method on the hash facade.

Let’s look at a simple example.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;

class UpdatePasswordController extends Controller
{
    public function update(Request $request)
    {
        // Validate the new password length...

        $request->user()->fill([
            'password' => Hash::make($request->newPassword)
        ])->save();
    }
}

Verifying A Password Against A Hash

The check method is used to verify a given text-plain string correspond to a given hash. If you are using LoginController, you do not need to use this directory, as this controller automatically call this method.

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

Checking If A Password Needs To Be Rehashed

The needsRehash is used to determine if the work factor used by the hasher has changed since the password was hashed.

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

Advertisements

Add Comment

📖 Read More