Published in
4 min readDec 31, 2020
# 版本
# 前言
我喜歡使用 Laravel 開發的感覺, 除了開發快速, 程式碼簡潔且優雅之外, Laravel 框架本身也是一個很好的學習參照物。 本篇主要將官方文件重點整理成 Q&A 的形式呈現, 原子化的概念, 這方式並不適用於每個人, 但若對你有幫助, 我會很開心。
# 目錄
Configuration
Laravel Hashing 中, 支援使用哪三種算法來 hash password?
- Argon2i
- Argon2id
- Bcrypt
Basic Usage
以下的 Laravel example code 的意思是?
- Example:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class UpdatePasswordController extends Controller
{
public function update(Request $request)
{
// Validate the new password length...
$request->user()->fill([
'password' => Hash::make($request->newPassword)
])->save();
}
}
- Answer: 取得登入的 User model, 並將 password attribute 替換成 hash 過的 newPassword, 然後儲存
Adjusting The Bcrypt Work Factor
以下的 Laravel example code 的意思是?
- Example:
<?php
$hashed = Hash::make('password', [
'rounds' => 12
]);
- Answer: hash password, 並指定 iteration 次數
Adjusting The Argon2 Work Factor
以下的 Laravel example code 的意思是?
- Example:
<?php
$hashed = Hash::make('password', [
'memory' => 1024,
'time' => 2,
'threads' => 2,
]);
- Answer: 指定 Hash 時, memory 用量, time 以及 thread 數量
Verifying A Password Against A Hash
以下的 Laravel example code 的意思是?
- Example:
<?php
if (Hash::check('plain-text', $hashedPassword)) {
// The passwords match...
}
- Answer: 使用
Hash::check
, arg1 為尚未 hash 過的, arg2 為 hashed 過的, 比較是否一致
Checking If A Password Needs To Be Rehashed
以下的 Laravel example code 的意思是?
- Example:
<?php
if (Hash::needsRehash($hashed)) {
$hashed = Hash::make('plain-text');
}
- Answer: 使用
Hash::needsRehash()
來確認 arg 是否與目前 Application 的 hash factor 一致, 若不一致則 return true