首页  >  文章  >  php框架  >  Laravel - 哈希

Laravel - 哈希

PHPz
PHPz原创
2024-08-27 10:51:191103浏览

Hashing is the process of transforming a string of characters into a shorter fixed value or a key that represents the original string. Laravel uses the Hash facade which provides a secure way for storing passwords in a hashed manner.

Basic Usage

The following screenshot shows how to create a controller named passwordController which is used for storing and updating passwords −

Laravel - 哈希

The following lines of code explain the functionality and usage of the 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([
         &#39;password&#39; => Hash::make($request->newLaravel - 哈希) // Hashing passwords
      ])->save();
   }
}

The hashed passwords are stored using make method. This method allows managing the work factor of the bcrypt hashing algorithm, which is popularly used in Laravel.

Verification of Laravel - 哈希 against Hash

You should verify the password against hash to check the string which was used for conversion. For this you can use the check method. This is shown in the code given below −

if (Hash::check(&#39;plain-text&#39;, $hashedLaravel - 哈希)) {
   // The passwords match...
}

Note that the check method compares the plain-text with the hashedLaravel - 哈希 variable and if the result is true, it returns a true value.

以上是Laravel - 哈希的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn