イントロダクションIntroduction
Laravelのエンクリプタ(encrypter)はOpenSSLを使い、AES-256とAES-128暗号化を提供しています。Laravelに組み込まれている暗号化機能を使用し、「自前」の暗号化アルゴリズムに走らないことを強く推奨します。Laravelの全暗号化済み値は、メッセージ認証コード(MAC)を使用し署名され、一度暗号化されると値を変更できません。Laravel's encrypter uses OpenSSL to provide AES-256 and AES-128 encryption. You are strongly encouraged to use Laravel's built-in encryption facilities and not attempt to roll your own "home grown" encryption algorithms. All of Laravel's encrypted values are signed using a message authentication code (MAC) so that their underlying value can not be modified once encrypted.
設定Configuration
Laravelのエンクリプタを使用する準備として、config/app.php
設定ファイルのkey
オプションをセットしてください。php
artisan
key:generate
コマンドを使用し、このキーを生成すべきです。このArtisanコマンドはPHPの安全なランダムバイトジェネレータを使用し、キーを作成します。この値が確実に指定されていないと、Laravelにより暗号化された値は、すべて安全ではありません。Before using Laravel's encrypter,
you must set a key
option in your
config/app.php
configuration file. You
should use the php artisan key:generate
command to generate this key since this Artisan
command will use PHP's secure random bytes generator
to build your key. If this value is not properly
set, all values encrypted by Laravel will be
insecure.
エンクリプタの使用Using The Encrypter
値の暗号化Encrypting A Value
Crypt
ファサードのencryptString
ヘルパを使用し、値を暗号化できます。OpenSSLとAES-256-CBC
アルゴリズムが使用され、すべての値は暗号化されます。さらに、全暗号化済み値はメッセージ認証コード(MAC)を使用し署名されますので、暗号化済み値の変更は感知されます。You may encrypt a value using the
encryptString
method of the
Crypt
facade. All encrypted values are
encrypted using OpenSSL and the
AES-256-CBC
cipher. Furthermore, all
encrypted values are signed with a message
authentication code (MAC) to detect any
modifications to the encrypted string:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
class UserController extends Controller
{
/**
* ユーザーの秘密のメッセージを保存
*
* @param Request $request
* @param int $id
* @return Response
*/
public function storeSecret(Request $request, $id)
{
$user = User::findOrFail($id);
$user->fill([
'secret' => Crypt::encryptString($request->secret),
])->save();
}
}
値の復号Decrypting A Value
Crypt
ファサードのdecryptString
ヘルパにより、値を復号できます。MACが無効な場合など、その値が正しくない時はIlluminate\Contracts\Encryption\DecryptException
が投げられます。You may decrypt values using the
decryptString
method of the
Crypt
facade. If the value can not be
properly decrypted, such as when the MAC is invalid,
an
Illuminate\Contracts\Encryption\DecryptException
will be thrown:
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Facades\Crypt;
try {
$decrypted = Crypt::decryptString($encryptedValue);
} catch (DecryptException $e) {
//
}