LoginSignup
22
25

More than 5 years have passed since last update.

Laravel5でSHA1のHasherを作成する

Last updated at Posted at 2015-03-03

Laravel5でのデフォルトの認証はpassword_hashメソッドを用いたBcryptHasherなので、それをSHA1のHasherに変更する。

作成するファイル

  • SHAHasher.php
  • SHAHashServiceProvider.php

場所はお好みで。今回はこんな感じ。

app/Libraries/SHAHasher.php
app/Providers/SHAHashServiceProvider.php

SHAHasher.php

まずはHasherクラスを作成します。

app/Libraries/SHAHasher.php
<?php namespace App\Libraries;

use RuntimeException;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;

class SHAHasher implements HasherContract {

        const SALT = 'hogehoge';

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

}

HasherContractはHasherのInterfaceです。
needsRehashは本来Laravel5で使用されているHasherはBcryptHasherなので存在しますが、SHA1の場合不要なのでfalseを返しておきます。

SHAHashServiceProvider.php

Laravelに読み込ませるため、ServiceProviderを作成します。

app/Providers/SHAHashServiceProvider.php
<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Libraries\SHAHasher;

class SHAHashServiceProvider extends ServiceProvider {

        /**
         * Indicates if loading of the provider is deferred.
         *
         * @var bool
         */
        protected $defer = true;

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

}

通常使用されているHashServiceProviderのregisterで返却されているのがBcryptHasherからSHAHasherに変更されているだけです。簡単ですね。

最後にconfig/app.phpに書かれているHashServiceProviderを上で作ったSHAHashServiceProviderに変更します。

config/app.php
+ //'Illuminate\Hashing\HashServiceProvider',
+ 'App\Providers\SHAHashServiceProvider',

これでLaravel5でSHA1のHasherが使用可能です。
CakePHPではデフォルトがSHA1でSALTとつなぎ合わせてHashしているのでこれで認証が可能になります。

22
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
25