Domanda

I want to implement another hasher in Laravel 4 (here Whirlpool, but my problem isn't Hash related) following this post.

The difference in my project is that I've created my services in folders and namespaces:

Laravel
|-- app
|   |-- config
|   |   +-- app.php
|   |
|   |-- controllers
|   |   +-- UserController.php
|
|-- lib
|   +-- Max13
|       +-- Services
|           +-- Hash
|               |-- WhirlpoolHasher.php
|               +-- WhirlpooHashServiceProvider.php
|
|-- start
|   +-- global.php
|
+-- composer.json

Here are the relevant extracts of each files:

composer.json:

[...]
"autoload": {
    "classmap": [
        [...]
        "app/lib",
    ]
}
[...]

app/config/app.php:

<?php
return array(
[...]
    'providers' => array(
        [...]
        'Illuminate\Workbench\WorkbenchServiceProvider',
        'Max13\Services\Hash\WhirlpoolHashServiceProvider',
    ),
[...]
);

app/controllers/UserControllers.php:

<?php
class UserController extends BaseController
{
    /**
     * Treat GET methods.
     *
     * @return Response
     */
    public function getUser()
    {
        if (Input::has('username', 'password')) {
            $username = Input::get('username');
            $password = Hash::make(Input::get('password'));

            Response::json($password);
        } else {
            return 'unknown';
        }
    }
}

lib/Max13/Services/Hash/WhirlpoolHasher.php:

<?php namespace Max13\Services\Hash;

class WhirlpoolHasher implements \Illuminate\Hashing\HasherInterface
{
    public function make($value, array $options = array())
    {
        $hash = hash('whirlpool', $value);

        if ($hash === null) {
            throw new \RuntimeException("Wirlpool hashing not supported.");
        }

        return $hash;
    }

    public function check($value, $hashedValue, array $options = array())
    {
        return strcasecmp($this->make($value), $hashedValue) === 0;
    }

    public function needsRehash($hashedValue, array $options = array())
    {
        return ctype_alnum($hashedValue) && strlen($hashedValue) === 128;
    }
}

lib/Max13/Services/Hash/WhirlpoolHashServiceProvider.php:

<?php namespace Max13\Services\Hash;

class WhirlpoolHashServiceProvider extends \Illuminate\Support\ServiceProvider
{
    public function register()
    {
        $this->app['hash'] = $this->app->share(function () {
            return new WhirlpoolHasher();
        });
    }

    public function provides()
    {
        return array('hash');
    }
}

I think I've done everything correctly (I didn't forget php composer dump-autoload), I don't have any errors BUT when I query my website on /user?username=foo&password=bar I get a Bcrypt hash (60 chars, Whirlpool is 128). Does anyone know what I'm missing?

È stato utile?

Soluzione

Remove laravel's default 'Illuminate\Hashing\HashServiceProvider', provider (around line 93) from your config/app.php and you should be good to go.

Altri suggerimenti

I have tried to implement my own hasher and I would say that your description is great guide for it. Unfortunately I spent a lot time on

'Class 'FooHasherServiceProvider' not found'
'Call to undefined method Illuminate\Support\Facades\Hash::check()'

and could't figure it out. So for people solving it in future, you must run

composer dump-autoload

after editing the classmap in composer.json.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top