Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to override salt (environment variable/config) for when t… #98

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class User extends Model {

```

#### Salt

The salt is generated automatically based on your app key and hash_alphabet. If you need to use the same salt between different projects, you can set the `HASHID_SALT` environment variable.

#### Route binding

When HashableId trait is used, base getRouteKey() and resolveRouteBinding() are overwritten to use the HashId as route key.
Expand Down
5 changes: 5 additions & 0 deletions config/hashid.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
* Determine HashId characters set.
*/
'hash_alphabet' => env('HASHID_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'),

/*
* Override generated HashId salt.
*/
'hash_salt' => env('HASHID_SALT', null),
];
4 changes: 2 additions & 2 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public function get(string $key): Hashids
if ($key === 'default') {
return $this->make(
$key,
substr(config('app.key', config('hashid.hash_alphabet')), 8, 4).substr(config('app.key', 'lara'), -4)
config('hashid.hash_salt') ? config('hashid.hash_salt') : substr(config('app.key', config('hashid.hash_alphabet')), 8, 4).substr(config('app.key', 'lara'), -4)
);
}

$key = strlen($key) > 4 ? $key : 'default'.$key;

return $this->make($key, substr($key, -4).substr(config('app.key', 'lara'), -4));
return $this->make($key, config('hashid.hash_salt') ? config('hashid.hash_salt') : substr($key, -4).substr(config('app.key', 'lara'), -4));
}

/** {@inheritdoc} */
Expand Down
Loading