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.
Install using composer:
composer require jenssegers/optimus
For 32 bit systems it is required to install the GMP extension. For debian/ubuntu you can install the extension with:
apt-get install php5-gmp
To get started you will need 3 things;
- Large prime number lower than
2147483647
- The inverse prime so that
(PRIME * INVERSE) & MAXID == 1
- A large random integer lower than
2147483647
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 spark
Prime: 2123809381
Inverse: 1885413229
Random: 146808189
If you prefer to choose your own prime number (from this list for example), you can pass it to the command to calculate the remaining numbers:
> php vendor/bin/optimus spark 1580030173
Prime: 1580030173
Inverse: 59260789
Random: 1163945558
Using those numbers, you can start creating instances of Optimus($prime, $inverted, $random)
:
use Jenssegers\Optimus\Optimus;
new Optimus(1580030173, 59260789, 1163945558);
NOTE: Make sure that you are using the same constructor values throughout your entire application!
To encode id's, use the encode
method:
$encoded = $optimus->encode(20); // 1535832388
To decode the resulting 1535832388
back to its original value, use the decode
method:
$original = $optimus->decode(1535832388); // 20