如何在Laravel 4中使用SHA1加密而不是BCrypt?

dyn*_*tem 16 php laravel

我正在为游戏开发一个所谓的AAC(自动帐户创建器),它基本上是一个具有为玩家创建帐户,玩家和更多东西的功能的网站.服务器只支持SHA1和plain - 这是完全不安全的.我无法深入了解源代码并进行更改.如果无论如何都要使用SHA1,我将不胜感激.我刚看过BCrypt,它很棒,但我无法真正改变源代码以适应BCrypt.我设法将SHA1放在注册上,如下所示:

$password = $input['password'];
$password = sha1($password);
Run Code Online (Sandbox Code Playgroud)

但我根本无法登录.我做错了吗?好像Laravel不会让我登录.

我有get_registerpost_register,还有我有get_loginpost_login.我是否需要在post_login中更改某些内容以使其登录或?任何提示?

我在WAMP上使用Laravel的php服务器(php artisan serve)和phpMyAdmin.我认为Laravel通过Auth::attempt方法laravel 检查数据库是否正在进行某种形式的哈希来检查当前的pw和登录的一个以检查对方.

rmo*_*bis 40

你必须重写Hash模块.感谢Laravel关于遵循IoC和依赖注入概念的想法,它会相对容易.

首先,创建一个app/libraries文件夹并将其添加到composer的autoload.classmap:

"autoload": {
    "classmap": [
        // ...

        "app/libraries"
    ]
},
Run Code Online (Sandbox Code Playgroud)

现在,是我们创建班级的时候了.创建一个SHAHasher类,实现Illuminate\Hashing\HasherInterface.我们需要实现它的3种方法:make,checkneedsRehash.

注意:在Laravel 5上,实现Illuminate/Contracts/Hashing/Hasher而不是Illuminate\Hashing\HasherInterface.

应用程序/库/ SHAHasher.php

class SHAHasher implements Illuminate\Hashing\HasherInterface {

    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @return array   $options
     * @return string
     */
    public function make($value, array $options = array()) {
        return hash('sha1', $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;
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我们完成了课程,我们希望Laravel默认使用它.为此,我们将创建SHAHashServiceProvider,扩展Illuminate\Support\ServiceProvider并将其注册为hash组件:

应用程序/库/ SHAHashServiceProvider.php

class SHAHashServiceProvider extends Illuminate\Support\ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register() {
        $this->app['hash'] = $this->app->share(function () {
            return new SHAHasher();
        });

    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides() {
        return array('hash');
    }

}
Run Code Online (Sandbox Code Playgroud)

很酷,现在我们所要做的就是确保我们的应用加载正确的服务提供商.在app/config/app.php,在providers,删除以下行:

'Illuminate\Hashing\HashServiceProvider',
Run Code Online (Sandbox Code Playgroud)

然后,添加这个:

'SHAHashServiceProvider',
Run Code Online (Sandbox Code Playgroud)


Ben*_*ird 5

对于这种情况,实际上有一个更简单(或更简单,至少)的解决方案。您可以通过在用户模型中使用此方法来“伪造”散列:

public function getAuthPassword() {
    return Hash::make($this->password);
}
Run Code Online (Sandbox Code Playgroud)

并使用您自己的哈希函数对输入进行哈希处理。例如,如果您的密码当前是用 sha1 散列的,您可以使用

Auth::attempt(array('email' => $email, 'password' => sha1($password))
Run Code Online (Sandbox Code Playgroud)

以这种方式进行编码感觉并不是很好的编码习惯,但它肯定比重写哈希模块更容易。

  • @trm42 实际上它确实有效。它使用 hash(X)==Y 将传入的密码 X 与数据库密码 Y 进行比较,因为它假定 Y 已经正确散列。我们正在做的是设置 X 以匹配 Y,然后对 Y 进行散列以使其匹配 X。然后运行的实际测试是 hash(sha1(X))==hash(sha1(Y))。看看守卫类的源代码,你会看到它是如何工作的。 (3认同)

Das*_*123 5

我花了很多时间才能在Laravel 5.6中发生类似的事情,但是这个线程非常宝贵。接受的答案可以使您非常接近,但是在进行过程中仍然存在一些伏击(从评论中可以看出),因此与其纠结于评论,我认为将其作为答案对其他人会有所帮助。

以我为例,我需要访问现有数据库,并且无法更改用户文件。密码以SHA256格式保存,并且还应用了哈希键。因此,对我来说,目标是仅使检查功能正常工作。

我真的是Laravel的新手,我知道解决这个问题会有更好的方法,但是我无法app\Libraries注册该地区,所以我将SHAHasher.phpSHAHashServiceProvider.php都放在了app\Providers其中,我认为这是某种Laravel牺牲,但这是我让它起作用的唯一方法。:)

我采取的步骤(劫持rmobis对于Laravel 4 出色回答)是:

Laravel需要访问原始应用中使用的哈希键,因此我将其添加到的底部.env

.env

...
HASH_KEY=0123_key_code_added_here_xyz
Run Code Online (Sandbox Code Playgroud)

app / Providers / SHAHasher.php

namespace App\Providers;

use Illuminate\Contracts\Hashing\Hasher;

class SHAHasher implements Hasher
{

  /**
   * Get information about the given hashed value.
   * TODO: This was added to stop the abstract method error.
   *
   * @param  string  $hashedValue
   * @return array
   */
  public function info($hashedValue)
  {
    return password_get_info($hashedValue);
  }

  /**
   * Hash the given value.
   *
   * @param  string $value
   * @return array   $options
   * @return string
   */
  public function make($value, array $options = array())
  {
    // return hash('sha1', $value);
    // Add salt and run as SHA256
    return hash_hmac('sha256', $value, env('HASH_KEY'));
  }

  /**
   * 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;
  }

}
Run Code Online (Sandbox Code Playgroud)

app / Providers / SHAHashServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class SHAHashServiceProvider extends ServiceProvider {

  /**
   * Register the service provider.
   *
   * @return void
   */
  public function register() {
    $this->app->singleton('hash', function() {
      return new SHAHasher();
    });
  }

  /**
   * Get the services provided by the provider.
   *
   * @return array
   */
  public function provides() {
    return array('hash');
  }

}
Run Code Online (Sandbox Code Playgroud)

app / config / app.php

删除或注释掉 // Illuminate\Hashing\HashServiceProvider::class,

App\Providers\SHAHashServiceProvider::class,

我不需要注册用户(仅允许他们使用其现有登录名进入),因此我仅对其进行了访问性测试。我不确定为什么应用程序/库区域不会使用。我遇到错误

Class 'SHAHashServiceProvider' not found
Run Code Online (Sandbox Code Playgroud)

当我运行composer dump-autoload命令直到将它们都移到应用程序/提供程序中时。

希望这可以帮助其他尝试使分析器在Laravel 5中工作的人。