Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use SHA256 in Laravel

I’m current working on a Laravel project about QR code in a residency. Everytime the home owner fills the details about a guest, the system will generate a unique 6-digit code. Now, I only want to hash the unique code in SHA256, but it’s not working. May I get some help?

This is my GuestController:

<?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'));

    } 

}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are creating a sha256 hash and then hash that hash with bcrypt throught Laravel’s Hash facade. Laravel’s Hash facade only supports bcrypt and Argon2. That explains why you’re getting different results.

Just use the vanilla PHP hash() function and remove the Laravel Hash.

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading