• Stars
    star
    1,254
  • Rank 35,932 (Top 0.8 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Optimus id transformation

Packagist Downloads Build Coverage

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

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;

  • 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!

Encoding and decoding

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

Framework Integrations

Laravel

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

<?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:

App\Providers\OptimusServiceProvider::class,

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

<?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

Third-party integrations

Security contact information

To report a security vulnerability, follow these steps.

License

The MIT License.

More Repositories

1

agent

👮 A PHP desktop/mobile user agent parser with support for Laravel, based on Mobiledetect
PHP
4,417
star
2

imagehash

🌄 Perceptual image hashing for PHP
PHP
1,940
star
3

date

🗓 A library to help you work with dates in multiple languages, based on Carbon.
PHP
1,813
star
4

php-proxy

A PHP proxy script with https and post support
PHP
922
star
5

blade

🔪 A standalone version of Laravel's Blade templating engine for use outside of Laravel.
PHP
752
star
6

model

This model provides an eloquent-like base class that can be used to build custom models in Laravel and other frameworks
PHP
348
star
7

RTL8188-hostapd

hostapd for Realtek RTL8188
C
334
star
8

laravel-rollbar

Rollbar error monitoring integration for Laravel projects
PHP
333
star
9

laravel-ab

A server-side A/B testing tool for Laravel.
PHP
285
star
10

captain

⚓️ Easily start and stop docker compose projects
Go
231
star
11

codeigniter-hmvc-modules

Create HMVC modules with this lightweight extension for Codeiginter 2
PHP
187
star
12

codeigniter-advanced-images

Resize and crop images on the fly
PHP
181
star
13

dashboard

A server dashboard for Ubuntu
PHP
136
star
14

codeigniter-template-library

A template library for Codeigniter
PHP
107
star
15

laravel-raven

Sentry (Raven) error monitoring integration for Laravel projects
PHP
91
star
16

laravel-mongodb-session

A MongoDB session driver for Laravel
PHP
75
star
17

codeigniter-authentication-library

A secure authentication library for CodeIgniter
PHP
67
star
18

laravel-oauth

A Laravel OAuth 1 & 2 library
PHP
65
star
19

laravel-mongodb-sentry

An extension for Laravel-MongoDB that lets you work with Sentry
PHP
53
star
20

codeigniter-message-library

Store and display error or information messages to display on the current page or next page
PHP
51
star
21

lean

Use the PHP League's Container package with auto-wiring support as the core container in Slim 3
PHP
31
star
22

laravel-mongodb-core

A MongoDB based query builder for Laravel
PHP
25
star
23

php-chef

Opscode Chef API PHP library
PHP
20
star
24

chef-patch

Helps you replace text and lines in files from Chef recipes.
Ruby
20
star
25

palenight.zsh-theme

My custom zsh theme
13
star
26

laravel-mongodb-lite

A lightweight MongoDB database library and model for Laravel 4
PHP
11
star
27

vagrant

My personal Vagrant development environment
HTML
7
star
28

docker

My personal Docker setup
5
star
29

docker-php-cs-fixer

A tool to automatically fix PHP coding standards issues
4
star
30

quotebot

Quotebot for slack
TypeScript
4
star
31

ddd-oke-cars

PHP
4
star
32

docker-php

Custom PHP docker image with some basic extensions
Dockerfile
4
star
33

cryptonope

Replace crypto words with random things
JavaScript
3
star
34

homebrew-custom

Custom homebrew formulas
Ruby
2
star
35

spacegray-monokai

A Monokai color scheme for Sublime's Spacegray theme
1
star