# 版本
# 前言
我喜歡使用 Laravel 開發的感覺, 除了開發快速, 程式碼簡潔且優雅之外, Laravel 框架本身也是一個很好的學習參照物。 本篇主要將官方文件重點整理成 Q&A 的形式呈現, 原子化的概念, 這方式並不適用於每個人, 但若對你有幫助, 我會很開心。
# 目錄
Introduction
Laravel Encryption 中, 使用了哪個套件提供 Encryption?
OpenSSL
Laravel Encryption 中, 使用了 OpenSSL 提供哪兩種 Encryption?
- AES-256
- AES-128
Laravel Encryption 中, 所有 encrypted 的 value 都使用什麼來簽名?
message authentication code
Encryption 中, MAC 的全寫是?
message authentication code
Encryption 中, MAC 的簡單解釋?
傳送方使用雙方約定好的金鑰以及演算法對傳送內容加密, 加密後會得到一串驗證碼, 與傳送內容一同傳送給接收方, 接收方收到後, 會使用雙方約定好的金曜以及演算法對傳送內容加密, 看加密後得到的驗證碼與傳送方傳過來的驗證碼是否相同, 若相同, 則傳送內容為可信
Configuration
Laravel Email Verification 中, 如何使用 CLI 來產生 key?
php artisan key:generate
Using The Encryption
Encrypting A Value
Laravel Encryption 中, encrypt helper 使用了什麼來加密?
- OpenSSL
- AES-256-CBC cipher
以下的 Laravel example code 的意思是?
- Example:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function storeSecret(Request $request, $id)
{
$user = User::findOrFail($id);
$user->fill([
'secret' => encrypt($request->secret),
])->save();
}
}
- Answer: 使用 encrypt() 來加密, 並使用 fill() insert 到 $user model
Encrypting Without Serialization
Laravel Encryption 中, 當我使用 encrypt method 時, 會通過 serialize, 為什麼?
為了可以 encrypt objects 以及 arrays
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Support\Facades\Crypt;
$encrypted = Crypt::encryptString('Hello world.');
$decrypted = Crypt::decryptString($encrypted);
- Answer: Laravel 預設會使用 serialize 來 encrypt, 因為對象可能是 array 或 object, 但當 encrypt string 時, 可以使用 encryptString() 或 decryptString(), 這樣就不會使用 serialize
Decrypting A Value
以下的 Laravel example code 的意思是?
- Example:
<?php
use Illuminate\Contracts\Encryption\DecryptException;
try {
$decrypted = decrypt($encryptedValue);
} catch (DecryptException $e) {
//
}
- Answer: 嘗試 decrypt $encryptedValue, 如果失敗, 會 throw 一個 DecryptException, 使用 cache 捕捉它