如何在 Laravel 中使用 SHA256

How to use SHA256 in Laravel

我目前正在做一个 Laravel 项目,关于驻留中的二维码。房主每次填写有关客人的详细信息时,系统都会生成一个唯一的 6 位代码。现在,我只想对 SHA256 中的唯一代码进行哈希处理,但它不起作用。我可以得到一些帮助吗?

这是我的访客控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Guest;
use App\Illuminate\Support\Facades\Hash;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class GuestController extends Controller
{

    public function create()
    {
        return view('pages.guest.create');
    }

    public function store(Request $request)
    {
        $guest = new Guest;
        $guest->code = random_int(100000, 999999);
        $guest->hash = Hash::make(hash('sha256', $guest['code']));
        $guest->guestname = $request->input('guestname');
        $guest->guestphone = $request->input('guestphone');
        $guest->guestic = $request->input('guestic');
        $guest->guestcar = $request->input('guestcar');
        $guest->datevisit = $request->input('datevisit');
        $guest->timevisit = $request->input('timevisit');
        $guest->save();
        
        return redirect('show-pass')->with('status', 'Guest Added Successfully');
    }

    public function delete($id)
    {
        $guest = Guest::find($id);
        $guest->delete();
        return redirect('show-pass')->with('status', 'Guest Deleted Successfully');
    }

    public function generate($id)
    {
        $guest = Guest::findOrFail($id);
        $qrcode = QrCode::size(150)->generate($guest->code);
        return view('pages.guest.generate', compact('qrcode'));
    }

    public function search(Request $request)
    {
        //Get the search value from the request
        $search = $request->input('search');

        //Search in the code from the list
        $guest = Guest::query()
                ->where('code', 'LIKE', "%{$search}%")
                ->get();
        
        //Return the search view with the results compacted
        return view('pages.guest.search', compact('guest'));

    } 

}

您正在创建一个 sha256 散列,然后通过 Laravel 的 Hash 外观使用 bcrypt 对该散列进行散列。 Laravel的Hash门面只支持bcryptArgon2。这就解释了为什么您会得到不同的结果。

只需使用原版 PHP hash() 函数并删除 Laravel Hash.

$guest->hash = hash('sha256', $guest['code']);