Skip to content

Commit

Permalink
Add feature to override salt (environment variable/config) for when t… (
Browse files Browse the repository at this point in the history
#98)

* Add feature to override salt (environment variable/config) for when the same HashIds are required for different projects

* add trailing comma.

---------

Co-authored-by: Rifki Alhuraibi <[email protected]>
  • Loading branch information
jonhassall and veelasky authored Oct 3, 2023
1 parent 29fb6be commit 539597d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
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

0 comments on commit 539597d

Please sign in to comment.