2015-11-06 74 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 ::類」