From 613f3fd56eef40b00e81283c1cbd8cf553f1bf01 Mon Sep 17 00:00:00 2001 From: Ben Sherred Date: Tue, 28 Nov 2023 08:32:40 +0000 Subject: [PATCH] feat: add shuffle key --- README.md | 2 +- config/sqids.php | 13 +++++++++++++ src/Sqids.php | 11 ++++++----- src/Support/Config.php | 11 +++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f48bbc1..32bd66c 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Laravel Sqids (pronounced "squids") allows you to easily generate Stripe/YouTube looking IDs for your Laravel models. These IDs are short and are guaranteed to be Collision free. -For more information on Sqids, we recommend checking out the official Sqids (formerly Hashids) webiste: [https://sqids.org](https://sqids.org). +For more information on Sqids, we recommend checking out the official Sqids (formerly Hashids) website: [https://sqids.org](https://sqids.org). ## Installation diff --git a/config/sqids.php b/config/sqids.php index f2b3b3e..1957c44 100644 --- a/config/sqids.php +++ b/config/sqids.php @@ -4,6 +4,19 @@ return [ + /* + |-------------------------------------------------------------------------- + | Shuffle Key + |-------------------------------------------------------------------------- + | + | This option is used to control the "shuffle key" for your Sqids. This + | ensures that your Sqids are unique to your application. Changing + | this value will result in all Sqids having a new value. + | + */ + + 'shuffle_key' => env('APP_KEY'), + /* |-------------------------------------------------------------------------- | Alphabet diff --git a/src/Sqids.php b/src/Sqids.php index f19d432..db0c40a 100644 --- a/src/Sqids.php +++ b/src/Sqids.php @@ -79,18 +79,19 @@ public static function encoder(string $model): SqidsCore public static function alphabetForModel(string $model): string { $alphabet = Config::alphabet(); - $modelLength = mb_strlen(string: $model); + $shuffle = $model . Config::shuffleKey(); + $shuffleLength = mb_strlen(string: $shuffle); - if (!$modelLength) { + if (!$shuffleLength) { return Config::alphabet(); } $alphabetArray = static::multiByteSplit(string: Config::alphabet()); - $modelArray = static::multiByteSplit(string: $model); + $shuffleArray = static::multiByteSplit(string: $shuffle); for ($i = mb_strlen($alphabet) - 1, $v = 0, $p = 0; $i > 0; $i--, $v++) { - $v %= $modelLength; - $p += $int = mb_ord($modelArray[$v], 'UTF-8'); + $v %= $shuffleLength; + $p += $int = mb_ord($shuffleArray[$v], 'UTF-8'); $j = ($int + $v + $p) % $i; $temp = $alphabetArray[$j]; diff --git a/src/Support/Config.php b/src/Support/Config.php index bd75d9d..9dec0fa 100644 --- a/src/Support/Config.php +++ b/src/Support/Config.php @@ -18,6 +18,17 @@ class Config protected static string $defaultPrefixCase = 'lower'; + public static function shuffleKey(): ?string + { + $shuffleKey = config(key: 'sqids.shuffle_key'); + + if (!is_string($shuffleKey)) { + return null; + } + + return $shuffleKey; + } + public static function alphabet(): string { $alphabet = config(key: 'sqids.alphabet');