2015-11-06 73 views
5

我正在将旧版应用移植到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; 
    } 
} 
+0

SHA1已被弃用。 – aldrin27

+2

@ aldrin27 - 感谢您的有见地的评论。我没有使用SHA1,我使用普通的MD5(这更糟糕),但它是腌制bcrypt的迁移过程的一部分。 – Mike

+0

密码可以在用户模型或AuthController中进行散列,也可以在相关特征中进行散列。你需要在那里寻找额外的散列,或者请提供文件来帮助。 –

回答

3

查核在你的用户模型密码突变。它在哈希在控制器中之后又一次哈希了密码。

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

1

第一步:创建应用程序/库文件夹,并把它添加到作曲家的autoload.classmap

"autoload": { 
    "classmap": [ 
     // ... 
     "app/libraries" 
    ] 
}, 

第2步:在应用程序创建两个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; 
    } 
} 

MD5HashServiceProvider.php

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

第三步:隐藏或配置/ app.php删除 “照亮\散列\ HashServiceProvider ::类”,并增加 “应用程序\库\ MD5HashServiceProvider ::类”