2015-05-19 66 views
0

我添加了一個散列函數在Laravel 5到註冊商使用PHP散列函數:Laravel 5與第二散列函數登錄問題

$password = $data['password']; 
    $salt = uniqid(mt_rand(), true); 
    $saltpass = $password.$salt; 
    $hashed = hash('sha256', $saltpass, false); 
    $b_hashed = bcrypt($hashed); 

    return User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => $b_hashed, 
     'salt' => $salt, 
    ]); 

該登記功能工作,最終密碼($ b_hashed)和所述鹽是保存到數據庫中。但是,問題在於登錄。我嘗試用保存的salt對用戶輸入密碼進行散列,然後將其傳遞到AthenticatesAndRegistersUsers.php中的postLogin函數的憑證中,但它不起作用。下面是代碼:

{ 
    $request->only('email', 'password'); 

    $record = \DB::table('users')->where('email', $credentials['email']->get(); 

    $record = $record[0]; 

    $pass = $credentials['password']; 

    $salt = $record->salt; 

    $pass = $pass.$salt; 

    $hashed = hash("sha256", $pass, false); 

    $credentials = array(
     'email' => $email, 
     'password' => $hashed); 


    $this->validate($request, [ 
     'email' => 'required|email', 'password' => 'required', 
    ]); 

    if ($this->auth->attempt($credentials, $request->has('remember'))) 
    { 
     return redirect()->intended($this->redirectPath()); 
    } 

    return redirect($this->loginPath()) 
       ->withInput($request->only('email', 'remember')) 
       ->withErrors([ 
        'email' => $this->getFailedLoginMessage(), 
       ]); 
} 
+0

爲什麼要實現自己的散列方案? Laravel將默認計算一個BCrypt哈希,並自動創建一個鹽並將其與哈希存儲在一起。沒有理由將SHA256與自己的salt一起使用,SHA不適合散列密碼。 – martinstoeckli

+0

我沒有實現我自己的哈希方案,而是在Laravel中爲現有的哈希方案添加了一個哈希級別。 – user4741755

+0

@ user4741755爲什麼?哈希是哈希? –

回答

0

如果你的目的是要增加額外的安全性,有更好的方法來改善方案,他們會保護精確定義的威脅。 Laravel默認已經使用了BCrypt,這在大多數情況下都是有用的。

  1. 額外的鹽(SHA)不會幫助,因爲鹽的工作已經由BCrypt鹽完成。
  2. 額外的SHA-256不會使散列速度明顯變慢,SHA- *速度很快,慢度已經由BCrypt的成本因子解決。

這使得該方案的祕密,只有知道在計算BCrypt哈希之前使用SHA-256的攻擊者才能成功強制密碼。換句話說,他需要訪問服務器來讀取代碼(除非你正在構建一個代碼已知的框架)。

要添加服務器端密鑰,最好計算一個BCrypt哈希值,然後用強服務器端密鑰對哈希值進行加密(雙向)。這種方式的關鍵是祕密(而不是方案),你可以在必要時進行交換。我在我的tutorial中詳細描述了有關安全存儲密碼的更詳細信息。