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

时间:2022-10-21
本文介绍了如何在 Laravel 中对密码使用 MD5 哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在将旧版应用移植到 Laravel.旧的应用程序使用 MD5 来散列密码而不加盐,所以我需要在 Laravel 中复制它.作为记录,我们正在使用 salt 将密码更改为 bcrypt,但这不是一个简单的过程,需要用户登录才能这样做 - 同时我只需要使用旧哈希登录即可.

I'm porting over a legacy app into Laravel. The old app used MD5 to hash the passwords without a salt, so I need to replicate that within Laravel. For the record, we are changing the passwords to bcrypt with a salt, but it's not a simple process and requires a user login to do so - for the meantime I just need to get logins working with the legacy hashes.

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

I have followed this guide to convert Auth::hash to MD5: How to use SHA1 encryption instead of BCrypt in Laravel 4?

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

When I print out the password in plain text and the generated hash in my make method when registering an account:

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

我得到以下信息:

123456
e10adc3949ba59abbe56e057f20f883e

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

Great, that's what I need. However, when that is saved to the database I get a different hash entirely. My guess is that Laravel is salting the password elsewhere, but I can't find where and how to override this.

我在 app/libraries 中的 MD5Hasher.php 文件:

My MD5Hasher.php file inside app/libraries:

<?php
class MD5Hasher implements IlluminateContractsHashingHasher {

    /**
     * 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;
    }

}

我的MD5HashServiceProvider.php:

<?php
class MD5HashServiceProvider extends IlluminateSupportServiceProvider {

    /**
     * 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');
    }

}

我的 AuthController.php 如下所示:

<?php

namespace AppHttpControllersAuth;

use Hash;
use AppUser;
use Validator;
use Mail;
use AppHttpControllersController;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;

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 IlluminateContractsValidationValidator
     */
    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;
    }
}

推荐答案

检查用户模型中的密码修改器.在控制器中对密码进行散列后,它再次对密码进行散列.

Check out the password mutator in your User Model. It's hashing the password another time after hashing it in the controller.

我的建议是在您的创建() 和更新() 模型事件中散列一次密码,然后将其从增变器和控制器中删除.

My recommendation is hash the password once in your creating() and updating() model events, and remove it from the mutator and controller.

这篇关于如何在 Laravel 中对密码使用 MD5 哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一条:如何在 4 个表中使用 Laravel 的 hasManyThrough 下一条:在 GoDaddy 服务器上发送电子邮件时出错:Laravel 5.1

相关文章

最新文章