目录

Laravel - Hashing( Hashing)

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

基本用法

以下屏幕截图显示了如何创建名为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方法存储散列密码。 该方法允许管理在Laravel中普遍使用的bcrypt散列算法的工作因子。

针对哈希的密码验证

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

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

请注意, check方法将纯文本与hashedPassword变量进行比较,如果结果为true,则返回true值。

↑回到顶部↑
WIKI教程 @2018