Skip to content

Commit

Permalink
feat: add shuffle key
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 28, 2023
1 parent 264c4ba commit 613f3fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions config/sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
11 changes: 11 additions & 0 deletions src/Support/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 613f3fd

Please sign in to comment.