hashids icon indicating copy to clipboard operation
hashids copied to clipboard

How do I set the salt?

Open punchi opened this issue 11 years ago • 8 comments

A random is generated? how/where can I see it? how can I set/change it? the salt it's important! thanks!

punchi avatar Jan 06 '15 05:01 punchi

Once you've installed the package, as in the installation guide, you should run the command below to publish the configuration file. Run the following in the root folder of your application:

php artisan config:publish mitch/hashids

This should publish the configuration file app/config/packages/mitch/hashids/config.php. This is where you set the alphabet key or salt as you asked.

vinkla avatar Jan 06 '15 09:01 vinkla

How can I use different salts?

mgleis avatar Jan 06 '15 10:01 mgleis

@plutus78 How do you mean?

vinkla avatar Jan 06 '15 10:01 vinkla

When I use it the laravel style: it always uses the same salt to calculate the hash. But I need differens Generators with different salts because I have different entities I want to secure.

Without your laravel implementation I can use:

$hashids1 = new Hashids\Hashids('this is my salt');
echo $hashids1->encode(1);
$hashids2 = new Hashids\Hashids('this is my second salt');
echo $hashids2->encode(1);

But how can I do it with your implementation?

mgleis avatar Jan 06 '15 10:01 mgleis

I don't think its possible out of the box with this package. You could set your own configuration and fetch them with the Config facade.

$hash = new Hashids(Config::get('myconfig.key'));

vinkla avatar Jan 06 '15 11:01 vinkla

the alphabet is not the same as salt, and as @plutus78 says, the salt can change depending the context, @vinkla thanks for a solution =)

hope someday could be like a method to change it, like Hashids::salt('my_salt');

Thanks!

punchi avatar Jan 07 '15 02:01 punchi

I have a solution!! :D

  • Change \vendor\mitch\hashids\src\Mitch\Hashids\HashidsServiceProvider.php

This

$app['config']['app.key']

To this

$app['config']['hashids::salt']
  • Change \vendor\mitch\hashids\src\config\config.php, add
'salt'     => 'THIS_IS_YOUR_SALT'

And on your config (\app\config\packages\mitch\hashids\config.php) change the values and you'r done!! with this, you can set the salt and you'r not forced to use the crypt one =) however, you can't change it in runtime =(

A variation, you can change the line to $app['config']['app.hashid_salt'] and on config, you can add a different key per environment and you can get it with Config::get('app.hashid_salt'); but you can't change it in runtime too =(

punchi avatar Jan 08 '15 05:01 punchi

I've created an Laravel 5 supported version of this package https://github.com/vinkla/hashids This version supports multiple salts and connections.

'connections' => [

    'main' => [
        'salt' => 'your-salt-string',
        'length' => 'your-length-integer',
        'alphabet' => 'your-alphabet-string'
    ],

    'alternative' => [
        'salt' => 'your-salt-string',
        'length' => 'your-length-integer',
        'alphabet' => 'your-alphabet-string'
    ],

]

vinkla avatar Feb 04 '15 07:02 vinkla