├── src ├── HashidsIntegerDriver.php ├── Base64IntegerDriver.php ├── DriverInterface.php ├── HexDriver.php ├── HexIntegerDriver.php ├── Base64Driver.php ├── HashidsHexDriver.php ├── HashidsStringDriver.php ├── Base62IntegerDriver.php ├── Facades │ └── Hashid.php ├── helpers.php ├── Base62Driver.php ├── HashidsDriver.php ├── OptimusDriver.php ├── Console │ ├── OptimusGenerateCommand.php │ └── AlphabetGenerateCommand.php ├── HashidServiceProvider.php └── HashidManager.php ├── LICENSE.md ├── CHANGELOG.md ├── composer.json ├── config └── hashid.php └── README.md /src/HashidsIntegerDriver.php: -------------------------------------------------------------------------------- 1 | hashids->encodeHex($data); 16 | } 17 | 18 | /** 19 | * Decode the data. 20 | * 21 | * @param mixed $data 22 | * @return string 23 | */ 24 | public function decode($data) 25 | { 26 | return $this->hashids->decodeHex($data); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/HashidsStringDriver.php: -------------------------------------------------------------------------------- 1 | base62->encodeInteger($data); 16 | } 17 | 18 | /** 19 | * Decode the data. 20 | * 21 | * @param string $data 22 | * @return int 23 | */ 24 | public function decode($data) 25 | { 26 | return $this->base62->decodeInteger($data); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Facades/Hashid.php: -------------------------------------------------------------------------------- 1 | connection($name); 13 | } 14 | } 15 | 16 | if (! function_exists('hashid_encode')) { 17 | /** 18 | * Encode the given data using hashid. 19 | * 20 | * @param mixed $data 21 | * @param string|null $name 22 | * @return mixed 23 | */ 24 | function hashid_encode($data, $name = null) 25 | { 26 | return hashid($name)->encode($data); 27 | } 28 | } 29 | 30 | if (! function_exists('hashid_decode')) { 31 | /** 32 | * Decode the given data using hashid. 33 | * 34 | * @param mixed $data 35 | * @param string|null $name 36 | * @return mixed 37 | */ 38 | function hashid_decode($data, $name = null) 39 | { 40 | return hashid($name)->decode($data); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2017-2022 Elf Sundae 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Base62Driver.php: -------------------------------------------------------------------------------- 1 | base62 = extension_loaded('gmp') 28 | ? new GmpEncoder($options) : new PhpEncoder($options); 29 | } 30 | 31 | /** 32 | * Encode the data. 33 | * 34 | * @param string $data 35 | * @return string 36 | */ 37 | public function encode($data) 38 | { 39 | return $this->base62->encode($data); 40 | } 41 | 42 | /** 43 | * Decode the data. 44 | * 45 | * @param string $data 46 | * @return string 47 | */ 48 | public function decode($data) 49 | { 50 | return $this->base62->decode($data); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/HashidsDriver.php: -------------------------------------------------------------------------------- 1 | hashids = new Hashids( 25 | Arr::get($config, 'salt', ''), 26 | Arr::get($config, 'min_length', 0), 27 | Arr::get($config, 'alphabet', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890') 28 | ); 29 | } 30 | 31 | /** 32 | * Encode the data. 33 | * 34 | * @param mixed $data 35 | * @return string 36 | */ 37 | public function encode($data) 38 | { 39 | return $this->hashids->encode($data); 40 | } 41 | 42 | /** 43 | * Decode the data. 44 | * 45 | * @param mixed $data 46 | * @return mixed 47 | */ 48 | public function decode($data) 49 | { 50 | return $this->hashids->decode($data); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Release Notes 2 | 3 | ## 1.7.4 (2025-06-15) 4 | 5 | - Add support for Laravel 12 6 | 7 | ## 1.7.3 (2024-10-24) 8 | 9 | - Add support for Laravel 11 10 | 11 | ## 1.7.2 (2023-05-16) 12 | 13 | - Add support for Laravel 10 14 | - Add support for Hashids 5 15 | 16 | ## 1.7.1 (2022-05-02) 17 | 18 | - Add PHPDoc for `Hashid` facade 19 | - Override `setContainer()` method for `HashidManager` 20 | 21 | ## 1.7.0 (2022-02-18) 22 | 23 | - Add support for Laravel 9 24 | 25 | ## 1.6.0 (2020-11-30) 26 | 27 | - Support PHP 8.0 28 | 29 | ## 1.5.0 (2020-09-13) 30 | 31 | - Add support for Laravel 8 32 | 33 | ## 1.4.0 (2020-03-04) 34 | 35 | - Add support for Laravel 7 36 | 37 | ## 1.3.2 (2019-09-15) 38 | 39 | - Add support for Laravel 6, Hashids 4 40 | 41 | ## 1.3.0 (2019-04-02) 42 | 43 | - Removed PHP 5 support 44 | - Added support for Laravel 5.7 and 5.8 45 | - Added support for `tuupola/base62` 2.x 46 | - Added support for `hashids/hashids` 3.x 47 | - Added support for `jenssegers/optimus` 1.x 48 | 49 | ## 1.2.0 (2018-02-04) 50 | 51 | - Support Laravel 5.6 52 | 53 | ## 1.1.0 (2018-01-18) 54 | 55 | - Updated dependences versions: `hashids`, `optimus` 56 | 57 | ## 1.0.1 (2017-11-12) 58 | 59 | - Optimized `composer.json` 60 | - Added `elfsundae/laravel-hashid-uuid` package suggestion 61 | 62 | ## 1.0.0 (2017-11-06) 63 | 64 | - Initial release 65 | -------------------------------------------------------------------------------- /src/OptimusDriver.php: -------------------------------------------------------------------------------- 1 | optimus = new Optimus( 31 | $config['prime'], 32 | $config['inverse'], 33 | isset($config['random']) ? $config['random'] : 0 34 | ); 35 | } 36 | 37 | /** 38 | * Encode the data. 39 | * 40 | * @param int $data 41 | * @return int 42 | */ 43 | public function encode($data) 44 | { 45 | return $this->optimus->encode($data); 46 | } 47 | 48 | /** 49 | * Decode the data. 50 | * 51 | * @param int $data 52 | * @return int 53 | */ 54 | public function decode($data) 55 | { 56 | return $this->optimus->decode($data); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Console/OptimusGenerateCommand.php: -------------------------------------------------------------------------------- 1 | generateOptimusNumbers( 33 | $this->getTimes(), 34 | (int) $this->option('prime') 35 | ); 36 | 37 | $this->table(['prime', 'inverse', 'random'], $numbers); 38 | } 39 | 40 | /** 41 | * Get "times" option value. 42 | * 43 | * @return int 44 | */ 45 | protected function getTimes() 46 | { 47 | return max(1, min(100, (int) $this->option('times'))); 48 | } 49 | 50 | /** 51 | * Generate Optimus numbers. 52 | * 53 | * @param int $times 54 | * @param int $prime 55 | * @return array 56 | */ 57 | protected function generateOptimusNumbers($times = 1, $prime = null) 58 | { 59 | $prime = $prime ?: null; 60 | 61 | $result = []; 62 | for ($i = 0; $i < $times; $i++) { 63 | $result[] = Energon::generate($prime); 64 | } 65 | 66 | return $result; 67 | } 68 | 69 | /** 70 | * Get the console command options. 71 | * 72 | * @return array 73 | */ 74 | protected function getOptions() 75 | { 76 | return [ 77 | ['prime', 'p', InputOption::VALUE_OPTIONAL, 'Generate with the given prime'], 78 | ['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1], 79 | ]; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elfsundae/laravel-hashid", 3 | "description": "A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.", 4 | "license": "MIT", 5 | "type": "library", 6 | "keywords": [ 7 | "hashid", 8 | "obfuscate", 9 | "base62", 10 | "base64", 11 | "hashids", 12 | "optimus", 13 | "URL safe" 14 | ], 15 | "homepage": "https://github.com/ElfSundae/laravel-hashid", 16 | "authors": [ 17 | { 18 | "name": "Elf Sundae", 19 | "email": "elf.sundae@gmail.com", 20 | "homepage": "https://0x123.com" 21 | } 22 | ], 23 | "require": { 24 | "php": "^7.1|^8.0", 25 | "illuminate/support": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 26 | "illuminate/console": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 27 | "tuupola/base62": "^2.0", 28 | "elfsundae/urlsafe-base64": "^1.1", 29 | "hashids/hashids": "^2.0.4|^3.0|^4.0|^5.0", 30 | "jenssegers/optimus": "^1.0" 31 | }, 32 | "require-dev": { 33 | "mockery/mockery": "^1.0", 34 | "phpunit/phpunit": "^5.7|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0", 35 | "orchestra/testbench": "^3.0|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0" 36 | }, 37 | "suggest": { 38 | "elfsundae/laravel-hashid-uuid": "Shorten UUID encoding" 39 | }, 40 | "minimum-stability": "dev", 41 | "prefer-stable": true, 42 | "autoload": { 43 | "psr-4": { 44 | "ElfSundae\\Laravel\\Hashid\\": "src/" 45 | }, 46 | "files": [ 47 | "src/helpers.php" 48 | ] 49 | }, 50 | "autoload-dev": { 51 | "psr-4": { 52 | "ElfSundae\\Laravel\\Hashid\\Test\\": "tests/" 53 | } 54 | }, 55 | "extra": { 56 | "branch-alias": { 57 | "dev-master": "1.7-dev" 58 | }, 59 | "laravel": { 60 | "providers": [ 61 | "ElfSundae\\Laravel\\Hashid\\HashidServiceProvider" 62 | ], 63 | "aliases": { 64 | "Hashid": "ElfSundae\\Laravel\\Hashid\\Facades\\Hashid" 65 | } 66 | } 67 | }, 68 | "scripts": { 69 | "test": "vendor/bin/phpunit" 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Console/AlphabetGenerateCommand.php: -------------------------------------------------------------------------------- 1 | generateRandomAlphabets( 39 | $this->getTimes(), 40 | (string) $this->option('characters') 41 | ); 42 | 43 | $this->comment(implode(PHP_EOL, $alphabets)); 44 | } 45 | 46 | /** 47 | * Get "times" option value. 48 | * 49 | * @return int 50 | */ 51 | protected function getTimes() 52 | { 53 | return max(1, min(100, (int) $this->option('times'))); 54 | } 55 | 56 | /** 57 | * Generate random alphabets. 58 | * 59 | * @param int $times 60 | * @param string $characters 61 | * @return array 62 | */ 63 | protected function generateRandomAlphabets($times = 1, $characters = null) 64 | { 65 | $characters = $characters ?: $this->defaultCharacters; 66 | 67 | $result = []; 68 | for ($i = 0; $i < $times; $i++) { 69 | $result[] = str_shuffle(count_chars($characters, 3)); 70 | } 71 | 72 | return $result; 73 | } 74 | 75 | /** 76 | * Get the console command options. 77 | * 78 | * @return array 79 | */ 80 | protected function getOptions() 81 | { 82 | return [ 83 | ['characters', 'c', InputOption::VALUE_OPTIONAL, 'Use custom characters', $this->defaultCharacters], 84 | ['times', 't', InputOption::VALUE_OPTIONAL, 'Times to generate', 1], 85 | ]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /config/hashid.php: -------------------------------------------------------------------------------- 1 | env('HASHID_CONNECTION', 'hashids_integer'), 17 | 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Hashid Connections 21 | |-------------------------------------------------------------------------- 22 | | 23 | | Here are each of the hashid connections setup for your application. 24 | | Of course, examples of configuring each hashid driver is shown below 25 | | to make development simple. You are free to add more. 26 | | 27 | | Built-in drivers: "base62", "base62_integer", "base64", "base64_integer", 28 | | "hashids", "hashids_hex", "hashids_integer", "hashids_string", 29 | | "hex", "hex_integer", "optimus". 30 | | 31 | */ 32 | 33 | 'connections' => [ 34 | 35 | 'hashids' => [ 36 | 'driver' => 'hashids', 37 | 'salt' => env('HASHIDS_SALT', ''), 38 | 'min_length' => env('HASHIDS_MIN_LENGTH', 0), 39 | 'alphabet' => env('HASHIDS_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), 40 | ], 41 | 42 | 'hashids_integer' => [ 43 | 'driver' => 'hashids_integer', 44 | 'salt' => env('HASHIDS_INTEGER_SALT', ''), 45 | 'min_length' => env('HASHIDS_INTEGER_MIN_LENGTH', 0), 46 | 'alphabet' => env('HASHIDS_INTEGER_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), 47 | ], 48 | 49 | 'hashids_hex' => [ 50 | 'driver' => 'hashids_hex', 51 | 'salt' => env('HASHIDS_HEX_SALT', ''), 52 | 'min_length' => env('HASHIDS_HEX_MIN_LENGTH', 0), 53 | 'alphabet' => env('HASHIDS_HEX_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), 54 | ], 55 | 56 | 'hashids_string' => [ 57 | 'driver' => 'hashids_string', 58 | 'salt' => env('HASHIDS_STRING_SALT', ''), 59 | 'min_length' => env('HASHIDS_STRING_MIN_LENGTH', 0), 60 | 'alphabet' => env('HASHIDS_STRING_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'), 61 | ], 62 | 63 | 'optimus' => [ 64 | 'driver' => 'optimus', 65 | 'prime' => env('OPTIMUS_PRIME'), 66 | 'inverse' => env('OPTIMUS_INVERSE'), 67 | 'random' => env('OPTIMUS_RANDOM', 0), 68 | ], 69 | 70 | 'base62' => [ 71 | 'driver' => 'base62', 72 | 'characters' => env('BASE62_CHARACTERS', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'), 73 | ], 74 | 75 | 'base62_integer' => [ 76 | 'driver' => 'base62_integer', 77 | 'characters' => env('BASE62_INTEGER_CHARACTERS', '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'), 78 | ], 79 | 80 | ], 81 | 82 | ]; 83 | -------------------------------------------------------------------------------- /src/HashidServiceProvider.php: -------------------------------------------------------------------------------- 1 | setupAssets(); 19 | 20 | $this->registerServices(); 21 | $this->registerCommands(); 22 | } 23 | 24 | /** 25 | * Setup package assets. 26 | * 27 | * @return void 28 | */ 29 | protected function setupAssets() 30 | { 31 | if ($this->app instanceof LumenApplication) { 32 | $this->app->configure('hashid'); // @codeCoverageIgnore 33 | } 34 | 35 | $this->mergeConfigFrom($config = __DIR__.'/../config/hashid.php', 'hashid'); 36 | 37 | if ($this->app->runningInConsole()) { 38 | $this->publishes([$config => base_path('config/hashid.php')], 'hashid'); 39 | } 40 | } 41 | 42 | /** 43 | * Register service bindings. 44 | * 45 | * @return void 46 | */ 47 | protected function registerServices() 48 | { 49 | $this->app->singleton('hashid', function ($app) { 50 | return new HashidManager($app); 51 | }); 52 | $this->app->alias('hashid', HashidManager::class); 53 | 54 | foreach ($this->getSingletonDrivers() as $class) { 55 | $this->app->singleton( 56 | $key = $this->getBindingKeyForDriver($class), 57 | function () use ($class) { 58 | return new $class; 59 | } 60 | ); 61 | $this->app->alias($key, $class); 62 | } 63 | 64 | foreach ($this->getNonSingletonDrivers() as $class) { 65 | $this->app->bind($this->getBindingKeyForDriver($class), $class); 66 | } 67 | } 68 | 69 | /** 70 | * Get singleton drivers classes. 71 | * 72 | * @return array 73 | */ 74 | protected function getSingletonDrivers() 75 | { 76 | return [ 77 | Base64Driver::class, 78 | Base64IntegerDriver::class, 79 | HexDriver::class, 80 | HexIntegerDriver::class, 81 | ]; 82 | } 83 | 84 | /** 85 | * Get non-singleton drivers classes. 86 | * 87 | * @return array 88 | */ 89 | protected function getNonSingletonDrivers() 90 | { 91 | return [ 92 | Base62Driver::class, 93 | Base62IntegerDriver::class, 94 | HashidsDriver::class, 95 | HashidsHexDriver::class, 96 | HashidsIntegerDriver::class, 97 | HashidsStringDriver::class, 98 | OptimusDriver::class, 99 | ]; 100 | } 101 | 102 | /** 103 | * Get the binding key for the driver class. 104 | * 105 | * @param string $class 106 | * @return string 107 | */ 108 | protected function getBindingKeyForDriver($class) 109 | { 110 | return 'hashid.driver.'.Str::snake( 111 | preg_replace('#Driver$#', '', class_basename($class)) 112 | ); 113 | } 114 | 115 | /** 116 | * Register console commands. 117 | * 118 | * @return void 119 | */ 120 | protected function registerCommands() 121 | { 122 | if ($this->app->runningInConsole()) { 123 | $this->commands([ 124 | Console\AlphabetGenerateCommand::class, 125 | Console\OptimusGenerateCommand::class, 126 | ]); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/HashidManager.php: -------------------------------------------------------------------------------- 1 | app = $container; 32 | } 33 | 34 | /** 35 | * Get a hashid connection instance. 36 | * 37 | * @param string|null $name 38 | * @return mixed 39 | */ 40 | public function connection($name = null) 41 | { 42 | return $this->driver($name); 43 | } 44 | 45 | /** 46 | * Get all of the created connections. 47 | * 48 | * @return array 49 | */ 50 | public function getConnections() 51 | { 52 | return $this->getDrivers(); 53 | } 54 | 55 | /** 56 | * Get the default connection name. 57 | * 58 | * @return string 59 | */ 60 | public function getDefaultConnection() 61 | { 62 | return $this->app['config']['hashid.default']; 63 | } 64 | 65 | /** 66 | * Set the default connection name. 67 | * 68 | * @param string $name 69 | * @return $this 70 | */ 71 | public function setDefaultConnection($name) 72 | { 73 | $this->app['config']['hashid.default'] = $name; 74 | 75 | return $this; 76 | } 77 | 78 | /** 79 | * {@inheritdoc} 80 | */ 81 | public function getDefaultDriver() 82 | { 83 | return $this->getDefaultConnection(); 84 | } 85 | 86 | /** 87 | * {@inheritdoc} 88 | */ 89 | protected function createDriver($name) 90 | { 91 | $config = Arr::get($this->app['config']['hashid.connections'], $name, []); 92 | 93 | return $this->createForConnection($name, $config) ?: 94 | $this->createForDriver(Arr::pull($config, 'driver', $name), $config); 95 | } 96 | 97 | /** 98 | * Create a new driver instance for the given connection. 99 | * 100 | * @param string $name 101 | * @param array $config 102 | * @return mixed 103 | */ 104 | protected function createForConnection($name, array $config = []) 105 | { 106 | if (isset($this->customCreators[$name])) { 107 | return $this->callCustom($name, compact('config')); 108 | } 109 | } 110 | 111 | /** 112 | * Create a new driver instance for the given driver. 113 | * 114 | * We will check to see if a creator method exists for the given driver, 115 | * and will call the Closure if so, which allows us to have a more generic 116 | * resolver for the drivers themselves which applies to all connections. 117 | * 118 | * @param string $driver 119 | * @param array $config 120 | * @return mixed 121 | * 122 | * @throws \InvalidArgumentException 123 | */ 124 | protected function createForDriver($driver, array $config = []) 125 | { 126 | if (isset($this->customCreators[$driver])) { 127 | return $this->callCustom($driver, compact('config')); 128 | } 129 | 130 | if ($binding = $this->getBindingKeyForDriver($driver)) { 131 | return $this->resolveBinding($binding, compact('config')); 132 | } 133 | 134 | throw new InvalidArgumentException("Unsupported driver [$driver]"); 135 | } 136 | 137 | /** 138 | * Call a custom creator. 139 | * 140 | * @param string $key 141 | * @param array $parameters 142 | * @return mixed 143 | */ 144 | protected function callCustom($key, array $parameters = []) 145 | { 146 | return $this->app->call($this->customCreators[$key], $parameters); 147 | } 148 | 149 | /** 150 | * Get the binding key for the driver. 151 | * 152 | * @param string $driver 153 | * @return string|null 154 | */ 155 | protected function getBindingKeyForDriver($driver) 156 | { 157 | if (class_exists($driver)) { 158 | return $driver; 159 | } 160 | 161 | if ($this->app->bound($key = "hashid.driver.$driver")) { 162 | return $key; 163 | } 164 | } 165 | 166 | /** 167 | * Resolve the given binding from the container. 168 | * 169 | * NOTE: 170 | * `Container::make($abstract, $parameters)` which can pass additional 171 | * parameters to the constructor was removed in Laravel 5.4 172 | * (https://github.com/laravel/internals/issues/391), but then re-added 173 | * as `makeWith()` in v5.4.16 (https://github.com/laravel/framework/pull/18271). 174 | * And in L55 the `makeWith()` is just an alias to `make()`. 175 | * 176 | * @param string $key 177 | * @param array $parameters 178 | * @return mixed 179 | */ 180 | protected function resolveBinding($key, array $parameters = []) 181 | { 182 | if ($this->app->isShared($key)) { 183 | return $this->app->make($key); 184 | } 185 | 186 | $makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make'; 187 | 188 | return $this->app->$makeWith($key, $parameters); 189 | } 190 | 191 | /** 192 | * {@inheritdoc} 193 | */ 194 | public function setContainer(Container $container) 195 | { 196 | $this->app = $container; 197 | 198 | return parent::setContainer($container); 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Hashid 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/elfsundae/laravel-hashid.svg?style=flat-square)](https://packagist.org/packages/elfsundae/laravel-hashid) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![tests](https://github.com/ElfSundae/laravel-hashid/actions/workflows/tests.yml/badge.svg)](https://github.com/ElfSundae/laravel-hashid/actions/workflows/tests.yml) 6 | [![StyleCI](https://styleci.io/repos/106044131/shield)](https://styleci.io/repos/106044131) 7 | [![SymfonyInsight Grade](https://img.shields.io/symfony/i/grade/5ecfdb3d-b2c4-47ca-b50b-4f041fe42f4f?style=flat-square)](https://insight.symfony.com/projects/5ecfdb3d-b2c4-47ca-b50b-4f041fe42f4f) 8 | [![Quality Score](https://img.shields.io/scrutinizer/g/ElfSundae/laravel-hashid.svg?style=flat-square)](https://scrutinizer-ci.com/g/ElfSundae/laravel-hashid) 9 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/ElfSundae/laravel-hashid/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/ElfSundae/laravel-hashid/?branch=master) 10 | [![Total Downloads](https://img.shields.io/packagist/dt/elfsundae/laravel-hashid.svg?style=flat-square)](https://packagist.org/packages/elfsundae/laravel-hashid) 11 | 12 | Laravel Hashid provides a unified API across various drivers such as [Base62], [Base64], [Hashids] and [Optimus], with support for multiple connections or different encoding options. It offers a simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers. 13 | 14 | 15 | 16 | - [Installation](#installation) 17 | - [Configuration](#configuration) 18 | - [Usage](#usage) 19 | - [Built-in Drivers](#built-in-drivers) 20 | - [Base62](#base62) 21 | - [Base64](#base64) 22 | - [Hashids](#hashids) 23 | - [Hex](#hex) 24 | - [Optimus](#optimus) 25 | - [Custom Drivers](#custom-drivers) 26 | - [Testing](#testing) 27 | - [License](#license) 28 | 29 | 30 | 31 | ## Installation 32 | 33 | You can install this package using the [Composer](https://getcomposer.org) manager: 34 | 35 | ```sh 36 | $ composer require elfsundae/laravel-hashid 37 | ``` 38 | 39 | For Lumen or earlier Laravel than v5.5, you need to register the service provider manually: 40 | 41 | ```php 42 | ElfSundae\Laravel\Hashid\HashidServiceProvider::class 43 | ``` 44 | 45 | Then publish the configuration file: 46 | 47 | ```sh 48 | # For Laravel application: 49 | $ php artisan vendor:publish --tag=hashid 50 | 51 | # For Lumen application: 52 | $ cp vendor/elfsundae/laravel-hashid/config/hashid.php config/hashid.php 53 | ``` 54 | 55 | ## Configuration 56 | 57 | Our well documented configuration file is extremely similar to the configurations of numerous Laravel manager integrations such as Database, Queue, Cache and Filesystem. So you do not need to spend extra time to learn how to configure Hashid. 58 | 59 | Additionally, for simplicity you do not need to add singleton drivers like Base64 to your config file as they have no encoding options, unless you would like to specify a meaningful connection name. 60 | 61 | Let's see an example of the configuration: 62 | 63 | ```php 64 | 'default' => 'id', 65 | 66 | 'connections' => [ 67 | 68 | 'basic' => [ 69 | 'driver' => 'base64', 70 | ], 71 | 72 | 'hashids' => [ 73 | 'driver' => 'hashids', 74 | 'salt' => 'sweet girl', 75 | ], 76 | 77 | 'id' => [ 78 | 'driver' => 'hashids_integer', 79 | 'salt' => 'My Application', 80 | 'min_length' => 6, 81 | 'alphabet' => '1234567890abcdef', 82 | ], 83 | 84 | 'base62' => [ 85 | 'driver' => 'base62', 86 | 'characters' => 'f9FkqDbzmn0QRru7PBVeGl5pU28LgIvYwSydK41sCO3htaicjZoWAJNxH6EMTX', 87 | ], 88 | 89 | ], 90 | ``` 91 | 92 | ## Usage 93 | 94 | The `hashid()` helper or the `Hashid` facade may be used to interact with any of your configured connections or drivers: 95 | 96 | ```php 97 | use ElfSundae\Laravel\Hashid\Facades\Hashid; 98 | 99 | // Obtain the default connection instance 100 | hashid(); 101 | Hashid::connection(); 102 | 103 | // Obtain the "base62" connection instance 104 | hashid('base62'); 105 | Hashid::connection('base62'); 106 | 107 | // Obtain the Base64 driver instance 108 | hashid('base64'); 109 | Hashid::connection('base64'); 110 | Hashid::driver('base64'); 111 | ``` 112 | 113 | There are only two methods you need to know to use any connection or driver: 114 | 115 | - `encode($data)` for encoding data. 116 | - `decode($data)` for decoding data. 117 | 118 | ```php 119 | hashid()->encode(123456); 120 | 121 | hashid('base64')->decode('TGFyYXZlbA'); 122 | 123 | Hashid::encode(123456); 124 | 125 | Hashid::connection('hashids')->decode('X68fkp'); 126 | ``` 127 | 128 | And there are also two corresponding helper functions: 129 | 130 | - `hashid_encode($data, $name = null)` 131 | - `hashid_decode($data, $name = null)` 132 | 133 | ```php 134 | hashid_encode(123456); 135 | 136 | hashid_decode('TGFyYXZlbA', 'base64'); 137 | ``` 138 | 139 | ## Built-in Drivers 140 | 141 | #### Base62 142 | 143 | - Drivers: `base62` , `base62_integer` 144 | - Configuration: 145 | - `characters` : 62 unique characters 146 | - Backend: [`tuupola/base62`][base62] 147 | - Notes: 148 | - You may use the `hashid:alphabet` command to generate random characters. 149 | - [GMP] is strongly recommended as it is much faster than pure PHP. 150 | 151 | #### Base64 152 | 153 | - Drivers: `base64` , `base64_integer` 154 | - Backend: [`elfsundae/urlsafe-base64`][base64] 155 | 156 | #### Hashids 157 | 158 | - Drivers: `hashids` , `hashids_hex` , `hashids_integer` , `hashids_string` 159 | - Configuration: 160 | - `salt` 161 | - `min_length` 162 | - `alphabet` : At least 16 unique characters 163 | - Backend: [`hashids/hashids`][hashids] 164 | - Notes: 165 | - You may use the `hashid:alphabet` command to generate a random alphabet. 166 | - [GMP] is strongly recommended. 167 | 168 | #### Hex 169 | 170 | - Drivers: `hex` , `hex_integer` 171 | 172 | #### Optimus 173 | 174 | - Drivers: `optimus` 175 | - Configuration: 176 | - `prime` : Large prime number lower than `2147483647` 177 | - `inverse` : The inverse prime so that `(PRIME * INVERSE) & MAXID == 1` 178 | - `random` : A large random integer lower than `2147483647` 179 | - Backend: [`jenssegers/optimus`][optimus] 180 | - Notes: 181 | - You may use the `hashid:optimus` command to generate needed numbers. 182 | - Only for integer numbers. 183 | - The max number can be handled correctly is `2147483647`. 184 | 185 | ## Custom Drivers 186 | 187 | To create a custom Hashid driver, you only need to implement the [`ElfSundae\Laravel\Hashid\DriverInterface`](src/DriverInterface.php) interface that contains two methods: `encode` and `decode`. The constructor can optionally receive the driver configuration from a `$config` argument, and type-hinted dependencies injection is supported as well: 188 | 189 | ```php 190 | encrypter = $encrypter; 206 | 207 | $this->serialize = $config['serialize'] ?? false; 208 | } 209 | 210 | public function encode($data) 211 | { 212 | return $this->encrypter->encrypt($data, $this->serialize); 213 | } 214 | 215 | public function decode($data) 216 | { 217 | return $this->encrypter->decrypt($data, $this->serialize); 218 | } 219 | } 220 | ``` 221 | 222 | Now you can configure the connection with this driver: 223 | 224 | ```php 225 | 'connections' => [ 226 | 227 | 'custom' => [ 228 | 'driver' => App\Hashid\CustomDriver::class, 229 | 'serialize' => false, 230 | ], 231 | 232 | // ... 233 | ] 234 | ``` 235 | 236 | If you prefer a short name for your driver, just register a container binding with `hashid.driver.` prefix: 237 | 238 | ```php 239 | $this->app->bind('hashid.driver.custom', CustomDriver::class); 240 | ``` 241 | 242 | ## Testing 243 | 244 | ```sh 245 | $ composer test 246 | ``` 247 | 248 | ## License 249 | 250 | This package is open-sourced software licensed under the [MIT License](LICENSE.md). 251 | 252 | [base62]: https://github.com/tuupola/base62 253 | [base64]: https://github.com/ElfSundae/urlsafe-base64 254 | [hashids]: https://github.com/ivanakimov/hashids.php 255 | [optimus]: https://github.com/jenssegers/optimus 256 | [gmp]: https://secure.php.net/gmp 257 | --------------------------------------------------------------------------------