Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save enniosousa/19660d5180e7cf62ef5feeb8a2904e86 to your computer and use it in GitHub Desktop.
Save enniosousa/19660d5180e7cf62ef5feeb8a2904e86 to your computer and use it in GitHub Desktop.
Laravel Authentication with MD5 Hasher instead of Bcrypt

Tested with Laravel 5.8

1. Create the class Md5Hasher into folder app/Hashers

<?php

namespace App\Hashers;

use Illuminate\Contracts\Hashing\Hasher;

class Md5Hasher implements Hasher
{
    /**
     * Get information about the given hashed value.
     *
     * @param  string  $hashedValue
     * @return array
     */
    public function info($hashedValue)
    {
        return $hashedValue;
    }
    
    /**
     * Hash the given value.
     *
     * @param  string $value
     * @param  array  $options
     * @return string
     */
    public function make($value, array $options = [])
    {
        return hash('md5', $value);
    }

    /**
     * Check the given plain value against a hash.
     *
     * @param  string $value
     * @param  string $hashedValue
     * @param  array  $options
     * @return bool
     */
    public function check($value, $hashedValue, array $options = [])
    {
        return $this->make($value, $options) === $hashedValue;
    }

    /**
     * Check if the given hash has been hashed using the given options.
     *
     * @param  string $hashedValue
     * @param  array  $options
     * @return bool
     */
    public function needsRehash($hashedValue, array $options = [])
    {
        return false;
    }
}

2. Updating login controller

2.1. Import MD5 Hasher Class

Open the login controller class file

App\Http\Controllers\Auth\LoginController

And import the hasher class App\Hashers\Md5Hasher that you had created.

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Hashers\Md5Hasher; // <-------------- import here

class LoginController extends Controller
// (...)

2.2. Overtite the method attemptLogin

		/**
     * Attempt to log the user into the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function attemptLogin(Request $request)
    {
        $provider = $this->guard()->getProvider();
        $provider->setHasher(new Md5Hasher);
        $this->guard()->setProvider($provider);

        return $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
        );
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment