Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating a custom password encrypt for Auth in laravel 5.0

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 4.56k
    Comment on it

    Many times when we have an existing database with existing multiple records with set password, encrypted with a specific encrypt technique and then we try to immigrate to Laravel 5.0 and try to use Laravel Auth, it clashes.


    Since, Laravel Auth using its own encrypting technique, which is the combination of multiple hashing technique. To resolve that we can use the following procedure.


    In this example I am changing Laravel 5.0 Auth hashing technique with Md5. I am not recommending MD5 it is just an example you can use any encrypting technique. Just follow along to accomplish it:-


    Step 1st:- create folder named "libraries" under app folder as /app/libraries and add it in composer.json under "autoload" .

    "autoload": {
        "classmap": [
            // ...
    
            "app/libraries"
        ]
    }
    

    Step 2nd:- Create a class named MD5Hasher inside the libraries folder having three methods named make, check and needsRehash

    app/libraries/MD5Hasher.php

    <?php
    class MD5Hasher implements Illuminate\Contracts\Hashing\Hasher {
    
        /**
         * Hash the given value.
         *
         * @param  string  $value
         * @return array   $options
         * @return string
         */
        public function make($value, array $options = array()) {
            return hash('md5', $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;
        }
    
    }
    

    Step 3rd:- Create another class named MD5HashServiceProvider inside the libraries folder.

    app/libraries/MD5HashServiceProvider.php

    <?php
    
    class MD5HashServiceProvider extends Illuminate\Support\ServiceProvider {
    
        /**
         * Register the service provider.
         *
         * @return void
         */
        public function register() {
            $this->app['hash'] = $this->app->share(function () {
                return new MD5Hasher();
            });
        }
    
        /**
         * Get the services provided by the provider.
         *
         * @return array
         */
        public function provides() {
            return array('hash');
        }
    
    }
    

    Step 4 :- In your app.php available inside the config folder.

    app/config/app.php

    Comment line

     'Illuminate\Hashing\HashServiceProvider' 
    

    and add following line

    'MD5HashServiceProvider',
    

    It will look something like this

    'providers' => [
                    // ...
    
    //              'Illuminate\Hashing\HashServiceProvider',
                    'MD5HashServiceProvider',
    ]
    

    Step 5:- Running the following command in command line.

    $ composer dumpautoload 
    

    For more information, refer to this:- http://stackoverflow.com/a/17719586/4936092

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: