0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravelでハッシュ化したいんですけどっ!!?

Last updated at Posted at 2022-06-18

表題の通りです。

本ページではLaravelドキュメントの「ハッシュ」についてのページを最低限理解できるようにかみ砕いたものです!

ハッシュについての簡単な解説はこちら↓

イントロダクション

Laravelが提供するHashファサードを使用することで、ユーザーのパスワードを安全に保存することができます。

基本的な使い方

Hashファサードのmakeメソッドを使うことで、パスワードをハッシュすることができる。

<?php
namespace App\Http\controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; // こいつを読み込む

class RegisterController extends Controller
{
    // ユーザーを登録する
    public function register(Request $request)
    {
        $password = $request->input('password'); // 生のパスワード
        // ここでパスワードをハッシュする
        $hashed_password = Hash::make($password);
    }
}

生の文字列がハッシュされた文字列と一致するかの確認

Hashファサードが提供するcheckメソッドを使用すると、生の文字列がハッシュ化された文字列と一致するかどうかを確認できる。

<?php
/**
 * 'raw text'が$hashed_textと一致するかを検証
 */
$hashed_text = Hash::make('raw text');

// この場合の検証は成功する
if (Hash::check('raw text', $hashed_text)) {
    echo "一致しています!";
} else {
    echo "不一致です、、、。";
}

余談

ハッシュなんて概念、エンジニアしてないと知る機会ないよなぁ。

大参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?