Laravel - Hashing

Laravel - Hashing

Laravel - Hashing

Hashing is the process of converting a string of characters into a shorter fixed value or key that represents the original string. Laravel uses the Hash facade, which provides a secure way to hash passwords.

Main use

The following screenshot shows how to create a controller named passwordController that is used to store and update passwords.

The following lines of code explain the functionality and usage of passwordController −

<? php

namespace App \Http\Controllers ; 

use Illuminate \Http\Request ; 
use Illuminate \Support\Facades\Hash ;
use App \Http\Controllers\Controller
class passwordController extends Controller { /** * Updating the password for the user. * * @param Request $request * @return Response */ public function update ( Request $request ) { // Validate the new password length... $request -> user ()-> fill ([ 'password' => Hash :: make ( $request -> newPassword ) // Hashing passwords ])-> save (); } }

Hashed passwords are stored using the make method . This method allows you to control the working factor of the bcrypt hashing algorithm , which is widely used in Laravel.

Checking password against hash

You have to hash the password to verify the string that was used for the conversion. You can use the validate method for this . This is shown in the below code −

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

Note that the check method compares the plain text against the hashedPassword variable and returns true if the result is true.