Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.

License

NotificationsYou must be signed in to change notification settings

jenssegers/optimus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PackagistDownloadsBuildCoverage

With this library, you can transform your internal id's to obfuscated integers based on Knuth's integer hash. It is similar to Hashids, but will generate integers instead of random strings. It is also super fast.

Installation

Install using composer:

composer require jenssegers/optimus

If you will be running your code on a 32 bit system or will be working with large prime numbers it is suggested that you install theGMP extension.For debian/ubuntu you can install the extension with one of these commands:

apt-get install php7.4-gmpapt-get install php8.0-gmpapt-get install php8.1-gmp

Usage

To get started you will need 3 things;

  • Large prime number lower than2147483647
  • The inverse prime so that(PRIME * INVERSE) & MAXID == 1
  • A large random integer lower than2147483647

Luckily for you, I have included a console command that can do all of this for you. To get started, just run the following command:

> php vendor/bin/optimus sparkPrime: 2123809381Inverse: 1885413229Random: 146808189

If you prefer to choose your own prime number (fromthis list for example), you can pass it to the command to calculate the remaining numbers:

> php vendor/bin/optimus spark 1580030173Prime: 1580030173Inverse: 59260789Random: 1163945558

Using those numbers, you can start creating instances ofOptimus($prime, $inverted, $random):

useJenssegers\Optimus\Optimus;newOptimus(1580030173,59260789,1163945558);

NOTE: Make sure that you are using the same constructor values throughout your entire application!

Encoding and decoding

To encode id's, use theencode method:

$encoded =$optimus->encode(20);// 1535832388

To decode the resulting1535832388 back to its original value, use thedecode method:

$original =$optimus->decode(1535832388);// 20

Framework Integrations

Laravel

This is an example service provider which registers a shared Optimus instance for your entire application:

<?phpnamespaceApp\Providers;useJenssegers\Optimus\Optimus;useIlluminate\Support\ServiceProvider;class OptimusServiceProviderextends ServiceProvider{publicfunctionregister()    {$this->app->singleton(Optimus::class,function ($app) {returnnewOptimus(1580030173,59260789,1163945558);        });    }}

Once you have created the service provider, add it to the providers array in yourconfig/app.php configuration file:

App\Providers\OptimusServiceProvider::class,

Laravel's automatic injection will pass this instance where needed. Example controller:

<?phpnamespaceApp\Http\Controllers;useJenssegers\Optimus\Optimus;useApp\Http\Controllers\Controller;class UserControllerextends Controller{publicfunctionshow($id,Optimus$optimus)    {$id =$optimus->decode($id);    }}

More information:https://laravel.com/docs/5.3/container#resolving

Third-party integrations

Security contact information

To report a security vulnerability, followthese steps.

License

TheMIT License.


[8]ページ先頭

©2009-2025 Movatter.jp