【发布时间】:2016-02-07 08:01:01
【问题描述】:
我正在将旧版应用程序移植到 Laravel。旧应用程序使用 MD5 对密码进行哈希处理,没有加盐,所以我需要在 Laravel 中复制它。作为记录,我们正在使用盐将密码更改为 bcrypt,但这不是一个简单的过程,需要用户登录才能这样做 - 同时我只需要使用旧哈希进行登录。
我已按照本指南将 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 正在其他地方加盐密码,但我找不到在哪里以及如何覆盖它。
app/libraries 中的我的MD5Hasher.php 文件:
<?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;
}
}
【问题讨论】:
-
@aldrin27 - 感谢您富有洞察力的评论。我没有使用 SHA1,我使用的是普通 MD5(更糟糕),但它是迁移到 salted bcrypt 过程的一部分。
-
密码可以在用户模型或 AuthController 和相关特征中进行哈希处理。您需要在那里寻找额外的哈希值,或者请提供文件以提供帮助。
-
@MinaYoussef 我已将我的 AuthController.php 添加到问题中。我在调用
User::create()时已经ddHash::make并且哈希与保存的不同。 -
@mikemike 对此感到抱歉。哈哈。我以为你会因为这个原因而使用 SHA1 -> 如何在 Laravel 4 中使用 SHA1 加密而不是 BCrypt?
-
您使用的是标准用户模型吗?
标签: php hash laravel-5 md5 laravel-5.1