Posted by Arjun on Tuesday 16th February 2016

Laravel 5 - SHA1 encryption instead of BCrypt

Laravel 5 Laravel 5.1 Laravel 5.x

Recently I have migrated core PHP application to the Laravel5 framework. The old application used SHA1 encryption so I have implemented Hashing contracts of laravel5 to use SHA1 instead of BCrypt.

SHA1 encryption instead of BCrypt

Create Directories in app called "Libraries\ShaHash" and create a file called SHAHasher.php and place below code in it.

<?php

namespace App\Libraries\ShaHash;

use Illuminate\Contracts\Hashing\Hasher as HasherContract;

class SHAHasher implements HasherContract {

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

}

Then create a service provider class(App\Providers) file called ShaHashServiceProvider.php and register your service as shown below.

<?php
namespace App\Providers;

use Illuminate\Hashing\HashServiceProvider;
use App\Libraries\SHAHash\SHAHasher as SHAHasher;

class ShaHashServiceProvider extends HashServiceProvider
{
	public function register()
	{
		$this->app->singleton('hash', function () {
			return new SHAHasher;
		});
	}
}

Then add our new service provider to providers array in config/app.php. and also remove default hash provider.

//'Illuminate\Hashing\HashServiceProvider',
App\Providers\ShaHashServiceProvider::class,

That is it.