イントロダクションIntroduction
LaravelのHashファサードは保存するユーザーパスワードに対し、安全なBcryptとArgon2ハッシュを提供します。Laravelアプリケーションに組み込まれている、LoginControllerとRegisterControllerを使用していれば、登録と認証で自動的にBcrypt使用します。The Laravel Hash facade[/docs/{{version}}/facades] provides secure Bcrypt and Argon2 hashing for storing user passwords. If you are using the built-in LoginController and RegisterController classes that are included with your Laravel application, they will use Bcrypt for registration and authentication by default.
">Tip!! Bcryptは「ストレッチ回数」が調整できるのでパスワードのハッシュには良い選択肢です。つまりハードウェアのパワーを上げればハッシュの生成時間を早くすることができます。{tip} Bcrypt is a great choice for hashing passwords because its "work factor" is adjustable, which means that the time it takes to generate a hash can be increased as hardware power increases.
設定Configuration
アプリケーションのデフォルトハッシュドライバーは、config/hashing.php設定ファイルで指定します。現在、BcryptとArgon2(Argon2iとArgon2id)、3つのドライバーをサポートしています。The default hashing driver for your application is configured in the config/hashing.php configuration file. There are currently three supported drivers: Bcrypt[https://en.wikipedia.org/wiki/Bcrypt] and Argon2[https://en.wikipedia.org/wiki/Argon2] (Argon2i and Argon2id variants).
Note: Argon2iドライバーはPHP7.2.0以上、Argon2idドライバーはPHP7.3.0以上が必要です。{note} The Argon2i driver requires PHP 7.2.0 or greater and the Argon2id driver requires PHP 7.3.0 or greater.
基本的な使用法Basic Usage
Hashファサードのmakeメソッドを呼び出し、パスワードをハッシュできます。You may hash a password by calling the make method on the Hash facade:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
class UpdatePasswordController extends Controller
{
/**
* ユーザーパスワードを更新
*
* @param Request $request
* @return Response
*/
public function update(Request $request)
{
// 新しいパスワードの長さのバリデーション…
$request->user()->fill([
'password' => Hash::make($request->newPassword)
])->save();
}
}
BcryptのWork Factorの調整Adjusting The Bcrypt Work Factor
Bcryptアルゴリズムを使用する場合、makeメソッドでroundsオプションを使用することにより、アルゴリズムのwork factorを管理できます。しかし、ほとんどのアプリケーションではデフォルト値で十分でしょう。If you are using the Bcrypt algorithm, the make method allows you to manage the work factor of the algorithm using the rounds option; however, the default is acceptable for most applications:
$hashed = Hash::make('password', [
'rounds' => 12
]);
Argon2のWork Factorの調整Adjusting The Argon2 Work Factor
Argon2アルゴリズムを使用する場合、memoryとtime、threadsオプションを指定することにより、アルゴリズムのwork factorを管理できます。しかし、ほとんどのアプリケーションではデフォルト値で十分でしょう。If you are using the Argon2 algorithm, the make method allows you to manage the work factor of the algorithm using the memory, time, and threads options; however, the defaults are acceptable for most applications:
$hashed = Hash::make('password', [
'memory' => 1024,
'time' => 2,
'threads' => 2,
]);
">Tip!! これらのオプションの詳細情報は、PHP公式ドキュメントをご覧ください。{tip} For more information on these options, check out the official PHP documentation[https://secure.php.net/manual/en/function.password-hash.php].
パスワードとハッシュ値の比較Verifying A Password Against A Hash
checkメソッドにより指定した平文文字列と指定されたハッシュ値を比較確認できます。しかしLaravelに含まれているLoginControllerを使っている場合は、これを直接使用することはないでしょう。このコントローラがこのメソッドを自動的に呼び出します。The check method allows you to verify that a given plain-text string corresponds to a given hash. However, if you are using the LoginController included with Laravel[/docs/{{version}}/authentication], you will probably not need to use this directly, as this controller automatically calls this method:
if (Hash::check('plain-text', $hashedPassword)) {
// パスワード一致
}
パスワードの再ハッシュが必要か確認Checking If A Password Needs To Be Rehashed
パスワードがハシュされてからハッシャーのストレッチ回数が変更されているかを調べるには、needsRehashメソッドを使います。The needsRehash function allows you to determine if the work factor used by the hasher has changed since the password was hashed:
if (Hash::needsRehash($hashed)) {
$hashed = Hash::make('plain-text');
}