From a27dbd6f806193266000091d7af75ca21d99e2ce Mon Sep 17 00:00:00 2001 From: jonhassall Date: Tue, 3 Oct 2023 01:22:08 +0100 Subject: [PATCH 1/2] Add feature to override salt (environment variable/config) for when the same HashIds are required for different projects --- README.md | 4 ++++ config/hashid.php | 5 +++++ src/Repository.php | 4 ++-- 3 files changed, 11 insertions(+), 2 deletions(-) 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..07aef88 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} */ From e4bc7ccb4195993be33e5e60282d3a165e1684e5 Mon Sep 17 00:00:00 2001 From: Rifki Alhuraibi Date: Tue, 3 Oct 2023 14:28:57 +0700 Subject: [PATCH 2/2] add trailing comma. --- config/hashid.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/hashid.php b/config/hashid.php index 07aef88..eb83c5d 100644 --- a/config/hashid.php +++ b/config/hashid.php @@ -14,5 +14,5 @@ /* * Override generated HashId salt. */ - 'hash_salt' => env('HASHID_SALT', null) + 'hash_salt' => env('HASHID_SALT', null), ];