如何在Laravel中使用MD5哈希密码?

Mik*_*ike 6 php hash md5 laravel-5 laravel-5.1

我正在将一个遗留应用程序移植到Laravel中.旧应用程序使用MD5在没有盐的情况下散列密码,因此我需要在Laravel中复制它.为了记录,我们正在使用salt将密码更改为bcrypt,但这不是一个简单的过程,需要用户登录才能这样做 - 同时我只需要登录使用遗留哈希.

我已按照本指南转换Auth::hash为MD5:如何在Laravel 4中使用SHA1加密而不是BCrypt?

当我make在注册帐户时以纯文本打印密码并在我的方法中生成的哈希:

public function make($value, array $options = array()) {
    echo $value.'<br>'.hash('md5', $value);
    exit;
    return hash('md5', $value);
}
Run Code Online (Sandbox Code Playgroud)

我得到以下内容:

123456
e10adc3949ba59abbe56e057f20f883e
Run Code Online (Sandbox Code Playgroud)

太好了,这就是我需要的.但是,当它保存到数据库时,我完全得到一个不同的哈希.我的猜测是Laravel正在其他地方输入密码,但我找不到在哪里以及如何覆盖它.

我的MD5Hasher.php文件里面app/libraries:

<?php
class MD5Hasher implements Illuminate\Contracts\Hashing\Hasher {

    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @return array   $options
     * @return string
     */
    public function make($value, array $options = array()) {
        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 = array()) {
        return $this->make($value) === $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 = array()) {
        return false;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的MD5HashServiceProvider.php:

<?php
class MD5HashServiceProvider extends Illuminate\Support\ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        $this->app['hash'] = $this->app->share(function () {
            return new MD5Hasher();
        });

    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides() {
        return array('hash');
    }

}
Run Code Online (Sandbox Code Playgroud)

AuthController.php看起来如下:

<?php

namespace App\Http\Controllers\Auth;

use Hash;
use App\User;
use Validator;
use Mail;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    //protected $redirectTo = '/account';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $this->redirectTo = '/register/step-1';

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        // email the user
        Mail::send('emails.register', ['user' => $user], function($message) use ($user)
        {
            $message->to($user->email, $user->name)->subject('Edexus - Welcome');
        });

        // email the admin
        Mail::send('emails.register-admin', ['user' => $user], function($message) use ($user)
        {
            $message->to('admins@***.com', 'Edexus')->subject('Edexus - New user sign up');
        });

        return $user;
    }
}
Run Code Online (Sandbox Code Playgroud)

Min*_*dir 4

检查您的用户模型中的密码修改器。在控制器中对密码进行哈希处理后,它会再次对密码进行哈希处理。

我的建议是在创建()和更新()模型事件中对密码进行一次哈希处理,然后将其从变异器和控制器中删除。