如何在 Laravel 中比較兩個加密 (bcrypt) 密碼?
在 Laravel 中,您可以使用 Hash facade 模組來處理密碼。它使用 bcrypt 來幫助您安全地儲存密碼。
Hash facade 的 bcrypt() 方法是雜湊密碼的強大方法。它可以防止惡意使用者破解使用 bcrypt() 生成的密碼。
雜湊細節可在 config/hashing.php 中找到。預設驅動程式使用 bcrypt() 作為雜湊演算法。
密碼雜湊
要使用 Hash Facade,您需要包含該類
Illuminate\Support\Facades\Hash
示例
要雜湊密碼,您可以使用 make() 方法。這是一個雜湊密碼的示例
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\Hash; class StudentController extends Controller { public function index() { echo $hashed = Hash::make('password', [ 'rounds' => 15, ]); } }
輸出
上述程式碼的輸出是
$2y$15$QKYQhdKcDSsMmIXZmwyF/.sihzQDhxtgF5WNiy4fdocNm6LiVihZi
驗證密碼是否與雜湊密碼匹配
要驗證明文(即在 Hash::make 中使用的文字)是否與雜湊密碼匹配,可以使用 check() 方法。
如果明文與雜湊密碼匹配,則 check() 方法返回 true;如果不匹配,則返回 false。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\Hash; class StudentController extends Controller { public function index() { $hashed = Hash::make('password', [ 'rounds' => 15, ]); if (Hash::check('password', $hashed)) { echo "Password matching"; } else { echo "Password is not matching"; } } }
輸出
上述程式碼的輸出是
Password matching
使用 check() 方法
現在讓我們嘗試使用錯誤的明文,看看 check() 方法的響應。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\Hash; class StudentController extends Controller { public function index() { $hashed = Hash::make('password', [ 'rounds' => 15, ]); if (Hash::check('password123', $hashed)) { echo "Password matching"; } else { echo "Password is not matching"; } } }
我們在雜湊中使用的明文是“password”。在 check 方法中,我們使用了“password123”,由於文字與雜湊文字不匹配,因此輸出為“密碼不匹配”。
輸出
在瀏覽器中執行時,輸出將是:
Password is not matching
兩次雜湊密碼
現在讓我們對同一文字進行兩次雜湊,並在 check() 方法中進行比較:
$testhash1 = Hash::make('mypassword'); $testhash2 = Hash::make('mypassword'); if (Hash::check('mypassword', $testhash1) && Hash::check('mypassword', $testhash2)) { echo "Password matching"; } else { echo "Password not matching"; }
您可以在瀏覽器中測試完整的程式碼,如下所示:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\Hash; class StudentController extends Controller { public function index() { $testhash1 = Hash::make('mypassword'); $testhash2 = Hash::make('mypassword'); if (Hash::check('mypassword', $testhash1) && Hash::check('mypassword', $testhash2)) { echo "Password matching"; } else { echo "Password not matching"; } } }
輸出
上述程式碼的輸出是:
Password matching
使用 bcrypt() 方法
您也可以嘗試使用 bcrypt() 方法,並使用 Hash::check() 測試明文與雜湊密碼。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\Hash; class StudentController extends Controller { public function index() { $hashedtext = bcrypt('mypassword'); if (Hash::check('mypassword', $hashedtext)) { echo 'Password matches'; } else{ echo 'Password not matching'; } } }
輸出
上述程式碼的輸出是:
Password matches
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP