Php 如何在Laravel中对密码使用MD5哈希?
我正在将一个遗留应用程序移植到Laravel。旧的应用程序使用MD5对密码进行无盐散列,所以我需要在Laravel中复制。作为记录,我们正在用salt将密码更改为bcrypt,但这不是一个简单的过程,需要用户登录才能做到这一点——同时,我只需要让登录名使用遗留哈希 我按照此指南将Php 如何在Laravel中对密码使用MD5哈希?,php,hash,laravel-5,md5,laravel-5.1,Php,Hash,Laravel 5,Md5,Laravel 5.1,我正在将一个遗留应用程序移植到Laravel。旧的应用程序使用MD5对密码进行无盐散列,所以我需要在Laravel中复制。作为记录,我们正在用salt将密码更改为bcrypt,但这不是一个简单的过程,需要用户登录才能做到这一点——同时,我只需要让登录名使用遗留哈希 我按照此指南将Auth::hash转换为MD5: 当我在注册帐户时以纯文本打印密码和在我的make方法中生成的哈希时: public function make($value, array $options = array()) {
Auth::hash
转换为MD5:
当我在注册帐户时以纯文本打印密码和在我的make
方法中生成的哈希时:
public function make($value, array $options = array()) {
echo $value.'<br>'.hash('md5', $value);
exit;
return hash('md5', $value);
}
太好了,这就是我需要的。然而,当它被保存到数据库中时,我得到了一个完全不同的散列。我的猜测是Laravel在其他地方添加了密码,但我找不到在哪里以及如何覆盖它
我的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;
}
}
我的AuthController.php
如下所示:
123456
e10adc3949ba59abbe56e057f20f883e
<?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;
}
}
检查您的用户模型中的密码转换器。在控制器中对密码进行哈希运算后,它将再次对密码进行哈希运算
我的建议是在creating()和updateing()模型事件中对密码进行一次散列,然后将其从mutator和controller中删除 步骤1:创建app/libraries文件夹并将其添加到composer的autoload.classmap
"autoload": {
"classmap": [
// ...
"app/libraries"
]
},
步骤2:在app/libraries中创建两个php文件MD5Hasher.php和MD5HashServiceProvider
MD5Hasher.php
<?php
namespace App\Libraries;
use Illuminate\Contracts\Hashing\Hasher;
class MD5Hasher implements Hasher {
/**
* Hash the given value.
*
* @param string $value
* @return array $options
* @return string
*/
public function make($value, array $options = array()) {
return 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;
}
}
@aldrin27-感谢您的富有洞察力的评论。我没有使用SHA1,我使用的是纯MD5(更糟糕),但它是salted bcrypt迁移过程的一部分。密码可以在用户模型或AuthController以及相关特性中散列。您需要在那里查找额外的哈希,或者请提供能够提供帮助的文件。@MinaYoussef我已将我的AuthController.php添加到问题中。我在调用User::create()
时使用了dd
哈希::make
,哈希与保存的哈希不同。@mikemike对此表示抱歉。哈哈。我以为你会因为这个而想使用SHA1->如何在Laravel4中使用SHA1加密而不是BCrypt?你使用的是标准用户模型吗?
<?php
namespace App\Libraries;
use Illuminate\Contracts\Hashing\Hasher;
class MD5Hasher implements Hasher {
/**
* Hash the given value.
*
* @param string $value
* @return array $options
* @return string
*/
public function make($value, array $options = array()) {
return 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;
}
}
<?php
namespace App\Libraries;
use Illuminate\Support\ServiceProvider;
class MD5HashServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register() {
// $this->app['hash'] = $this->app->share(function () {
// return new MD5Hasher();
// });
$this->app->singleton('hash', function () {
return new MD5Hasher();
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array('hash');
}