2017 © Pedro Peláez
 

library optimus

Id obfuscation based on Knuth's integer hash method

image

jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  • Sunday, July 22, 2018
  • by jenssegers
  • Repository
  • 28 Watchers
  • 809 Stars
  • 275,336 Installations
  • PHP
  • 21 Dependents
  • 1 Suggesters
  • 44 Forks
  • 10 Open issues
  • 11 Versions
  • 12 % Grown

The README.md

Optimus id transformation

Packagist Downloads Build Coverage, (*1)

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., (*2)

, (*3)

Installation

Install using composer:, (*4)

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 the GMP extension. For debian/ubuntu you can install the extension with one of these commands:, (*5)

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

Usage

To get started you will need 3 things;, (*6)

  • 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:, (*7)

> 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:, (*8)

> 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):, (*9)

use Jenssegers\Optimus\Optimus;

new Optimus(1580030173, 59260789, 1163945558);

NOTE: Make sure that you are using the same constructor values throughout your entire application!, (*10)

Encoding and decoding

To encode id's, use the encode method:, (*11)

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

To decode the resulting 1535832388 back to its original value, use the decode method:, (*12)

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

Framework Integrations

Laravel

This is an example service provider which registers a shared Optimus instance for your entire application:, (*13)

<?php

namespace App\Providers;

use Jenssegers\Optimus\Optimus;
use Illuminate\Support\ServiceProvider;

class OptimusServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Optimus::class, function ($app) {
            return new Optimus(1580030173, 59260789, 1163945558);
        });
    }
}

Once you have created the service provider, add it to the providers array in your config/app.php configuration file:, (*14)

App\Providers\OptimusServiceProvider::class,

Laravel's automatic injection will pass this instance where needed. Example controller:, (*15)

<?php

namespace App\Http\Controllers;

use Jenssegers\Optimus\Optimus;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function show($id, Optimus $optimus)
    {
        $id = $optimus->decode($id);
    }
}

More information: https://laravel.com/docs/5.3/container#resolving, (*16)

Third-party integrations, (*17)

Security contact information

To report a security vulnerability, follow these steps., (*18)

License

The MIT License., (*19)

The Versions

22/07 2018

dev-master

9999999-dev https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

05/03 2018

dev-experimental

dev-experimental https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

21/04 2017

v0.2.3

0.2.3.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

07/11 2016

v0.2.2

0.2.2.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

20/02 2016

v0.2.1

0.2.1.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

28/10 2015

v0.2.0

0.2.0.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

13/09 2015

v0.1.4

0.1.4.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

12/07 2015

v0.1.3

0.1.3.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

24/05 2015

v0.1.2

0.1.2.0 https://github.com/jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

04/05 2015

v0.1.1

0.1.1.0 https://github.com/jenssegers/php-optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation

04/05 2015

v0.1.0

0.1.0.0 https://github.com/jenssegers/php-optimus

Id obfuscation based on Knuth's integer hash method

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

ids hashids optimus obfuscation id obfuscation