gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:22:30 24 4
gpt4 key购买 nike

我正在将旧版应用程序移植到 Laravel 中。旧应用程序使用 MD5 不加盐地对密码进行哈希处理,因此我需要在 Laravel 中复制它。作为记录,我们正在将密码更改为 bcrypt with a salt,但这不是一个简单的过程,需要用户登录才能这样做——与此同时,我只需要让登录使用旧哈希。

我已按照本指南将 Auth::hash 转换为 MD5: How to use SHA1 encryption instead of BCrypt in Laravel 4?

当我在注册帐户时在 make 方法中以明文形式打印出密码和生成的哈希时:

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

我得到以下信息:

123456
e10adc3949ba59abbe56e057f20f883e

太好了,这就是我需要的。然而,当它被保存到数据库时,我得到了一个完全不同的哈希值。我的猜测是 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;
}

}

我的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');
}

}

我的 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;
}
}

最佳答案

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

我的建议是在您的 creating() 和 updating() 模型事件中将密码散列一次,然后将其从修改器和 Controller 中删除。

关于php - 如何在 Laravel 中对密码使用 MD5 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33562285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com