How do I set the salt?
A random is generated? how/where can I see it? how can I set/change it? the salt it's important! thanks!
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.
How can I use different salts?
@plutus78 How do you mean?
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?
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'));
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!
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 =(
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'
],
]