laravel 如何使用 md5 而不是 bcrypt?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42176866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:18:37  来源:igfitidea点击:

How do I use md5 instead of bcrypt?

laravelauthentication

提问by Sinan Samet

I need to use md5()instead of bcrypt()for storing passwords. But when I just do this:

我需要使用md5()而不是bcrypt()用于存储密码。但是当我这样做时:

protected function create(array $data)
{
    return Account::create([
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => md5($data['password']),
        'datetoday' => Carbon::now(),
        'lastip' => request()->ip(),
        'confirmation' => bcrypt($data['password']),
    ]);
}

When I try to login it says the credentials are wrong.

当我尝试登录时,它说凭据错误。

回答by Sanzeeb Aryal

Using md5() over bcrypt() is not recommended.

不建议在 bcrypt() 上使用 md5()。

However you can manually authenticate user. Override login()method in LoginController

但是,您可以手动验证用户。覆盖login()LoginController 中的方法

  public function login(Request $request)
  {
     $user = User::where('username', $request->username)
                  ->where('password',md5($request->password))
                  ->first();
     Auth::login($user);
     return redirect('/');
  }

回答by arku

You have to create new service provider.

您必须创建新的服务提供者。

app/providers/md5hashprovier.php

应用程序/提供者/md5hashprovier.php

namespace App\Providers;
class MD5HashProvider extends \Illuminate\Hashing\HashServiceProvider
{
    public function boot()
    {
    \App::bind('hash', function () {
        return new \App\Classes\MD5Hasher;
    });
}}

Next you have to create the MD5Hasher class. I'd suggest to locate it to app/classes/MD5Hasher.php

接下来,您必须创建 MD5Hasher 类。我建议将它定位到 app/classes/MD5Hasher.php

class MD5Hasher extends BcryptHasher
{
public function check($value, $hashedValue, array $options = array())
   {
      $user = User::wherePassword(md5($value))->first();
      return $user ? true : false
    }
}

and register your new service provider to config/app.php in providers array

并将您的新服务提供者注册到 providers 数组中的 config/app.php

\App\Providers\MD5HashProvider::class,

This would enable auth with md5 password

这将使用 md5 密码启用身份验证

回答by Serdar De?irmenci

Using MD5 is not a good idea anymore.

使用 MD5 不再是一个好主意。

To get rid of the old MD5 records you can use the second trick here: http://john.cuppi.net/migrate-from-md5-to-bcrypt-password-hashes/

要摆脱旧的 MD5 记录,您可以在这里使用第二个技巧:http: //john.cuppi.net/migrate-from-md5-to-bcrypt-password-hashes/