Laravel - Hashing

散列是将字符串转换为较短的固定值或表示原始字符串的键的过程. Laravel使用 Hash Facade,它提供了一种以散列方式存储密码的安全方式.

基本用法

以下屏幕截图显示了如何创建名为 passwordController 的控制器,该控制器用于存储和更新密码 :

密码

以下代码行解释了 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();
   }
}

使用 make 方法存储散列密码.此方法允许管理 bcrypt 散列算法的工作因素,该算法在Laravel中广泛使用.

验证密码对抗哈希

您应该根据哈希验证密码以检查用于转换的字符串.为此,您可以使用检查方法.这显示在下面给出的代码中 :

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

请注意检查方法比较带有 hashedPassword 变量的文本,如果结果为true,则返回true值.