diff --git a/README.md b/README.md index b99fe67..2a87921 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/config/hashid.php b/config/hashid.php index 47f5eaa..eb83c5d 100644 --- a/config/hashid.php +++ b/config/hashid.php @@ -10,4 +10,9 @@ * Determine HashId characters set. */ 'hash_alphabet' => env('HASHID_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), + + /* + * Override generated HashId salt. + */ + 'hash_salt' => env('HASHID_SALT', null), ]; diff --git a/src/Repository.php b/src/Repository.php index dcc1fc0..3e1412a 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -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} */