我正在将旧版应用移植到Laravel中。旧的应用程序使用MD5来散列密码而不用盐,所以我需要在Laravel中复制密码。为了记录,我们正在将密码更改为使用salt进行加密,但这不是一个简单的过程,并且需要用户登录才能这样做 - 与此同时,我只需要使用传统哈希值登录即可。如何在Laravel中使用MD5哈希密码?
我按照本指南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('[email protected]***.com', 'Edexus')->subject('Edexus - New user sign up');
});
return $user;
}
}
SHA1已被弃用。 – aldrin27
@ aldrin27 - 感谢您的有见地的评论。我没有使用SHA1,我使用普通的MD5(这更糟糕),但它是腌制bcrypt的迁移过程的一部分。 – Mike
密码可以在用户模型或AuthController中进行散列,也可以在相关特征中进行散列。你需要在那里寻找额外的散列,或者请提供文件来帮助。 –