Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devondahon's avatar

How to deal with Hash::make(md5('password')) ?

I'm inheriting from a users table where passwords are encrypted like this Hash::make(md5('password')) and I need to merge it to other tables where it's encrypted like the standard way Hash::make('password').

To avoid forcing the user to reset his password, I'd like to check during the login process if the password has an additional md5() hash and, in this case, automatically replace it by standard Hash::make('password') before logging in.

How should I do it ?

0 likes
9 replies
Tray2's avatar

I would force them to change the password.

Inform them by mail tht the need to do a password reset and link to the reset password page where they fill in their email and send them the reset link.

2 likes
MichalOravec's avatar

@devondahon Add this to your LoginController

/**
 * Attempt to log the user into the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return bool
 */
protected function attemptLogin(Request $request)
{
    $isPassed = $this->guard()->attempt($this->credentials($request), $request->filled('remember'));

    if (! $isPassed) {
        $user = User::where($username = $this->username(), $request->{$username})
            ->where('password', Hash::make(md5($request->password)))
            ->first();

        if ($user) {
            $user->password = Hash::make($request->password);

            $user->save();

            $this->guard()->login($user, $request->filled('remember'));

            return true;
        }
    }

    return $isPassed;
}
1 like
martinbean's avatar

PSA: md5 is a hashing algorithm and not an “encryption” algorithm.

devondahon's avatar

@michaloravec

Thank you very much for this answer, it looks great... but it doesn't seem to work. I made a test by generating a md5+bcrypt password with php artisan tinker:

>>> Hash::make(md5('foobar'))
=> "y$rE2iI4WBs6EsQqZ3SASlKezdH4FqskyYiCip3mJUhulyKWpws4ak."

And setting it directly to my PostgreSQL table:

UPDATE public.users SET
password = 'y$rE2iI4WBs6EsQqZ3SASlKezdH4FqskyYiCip3mJUhulyKWpws4ak.'::character varying WHERE
id = '12345';

but no user is found in if (!$isPassed) { }, I get an empty value for $user in my logs.

So, I was wondering how could this actually work:

$user = User::where($username = $this->username(), $request->{$username})
            ->where('password', Hash::make(md5($request->password)))
            ->first();

while Hash::make() always gives a different result for the same string ?

@martinbean I corrected my question, thanks for the clarification.

kima's avatar
kima
Best Answer
Level 2

what about

    protected function attemptLogin(\Illuminate\Http\Request $request)
    {
        $isPassed = $this->guard()->attempt($this->credentials($request), $request->filled('remember'));

        if (! $isPassed) {
            $isPassed = $this->guard()->attempt(array_merge($this->credentials($request), ["password" => md5($request->input("password"))]), $request->filled('remember'));

            if ($isPassed) {
                $user = Auth::user();
                $user->password = Hash::make($request->password);

                $user->save();

                return true;
            }
        }

        return $isPassed;
    }
1 like
MichalOravec's avatar

@devondahon You said that you store password as Hash::make(md5('password'))

@kima Thanks for copying my post...

If it's only md5('password') then

/**
 * Attempt to log the user into the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return bool
 */
protected function attemptLogin(Request $request)
{
    $isPassed = $this->guard()->attempt($this->credentials($request), $request->filled('remember'));

    if (! $isPassed) {
        $user = User::where($username = $this->username(), $request->{$username})
            ->where('password', md5($request->password))
            ->first();

        if ($user) {
            $user->password = Hash::make($request->password);

            $user->save();

            $this->guard()->login($user, $request->filled('remember'));

            return true;
        }
    }

    return $isPassed;
}

I just changed Hash::make(md5('password')) to md5($request->password) from my previous post.

devondahon's avatar

@michaloravec I tried this last method, but I could not sign in neither with bcrypt nor with md5+bcrypt password (I didn't check why). The method from @kima works perfectly fine, so I will stick to it for the moment. And again, thanks a lot for your answer which helped me a lot to understand the login process.

Please or to participate in this conversation.