PHPackages                             jenssegers/optimus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jenssegers/optimus

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jenssegers/optimus
==================

Id obfuscation based on Knuth's integer hash method

v1.1.2(2y ago)1.3k4.8M↓13.3%75[10 issues](https://github.com/jenssegers/optimus/issues)[4 PRs](https://github.com/jenssegers/optimus/pulls)20MITPHPPHP ^7.4||^8.0

Since May 4Pushed 2y ago22 watchersCompare

[ Source](https://github.com/jenssegers/optimus)[ Packagist](https://packagist.org/packages/jenssegers/optimus)[ Docs](https://github.com/jenssegers/optimus)[ GitHub Sponsors](https://github.com/jenssegers)[ Fund](https://tidelift.com/funding/github/packagist/jenssegers/optimus)[ RSS](/packages/jenssegers-optimus/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (16)Used By (20)

Optimus id transformation
=========================

[](#optimus-id-transformation)

[![Packagist](https://camo.githubusercontent.com/62e27ac4ee0dea447a5319ef24c4b6bb1604c7ae796af839ba0c60a478fdaf99/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f762f6a656e737365676572732f6f7074696d7573)](https://packagist.org/packages/jenssegers/optimus)[![Downloads](https://camo.githubusercontent.com/a568a165597622f4a842b3e812eb8fcd774155b4c51b442985f457471627f7f9/68747470733a2f2f62616467656e2e6e65742f7061636b61676973742f64742f6a656e737365676572732f6f7074696d7573)](https://packagist.org/packages/jenssegers/optimus/stats)[![Build](https://github.com/jenssegers/optimus/workflows/tests/badge.svg)](https://github.com/jenssegers/optimus/actions)[![Coverage](https://camo.githubusercontent.com/67f0005d5d82e69fe8f069b3618f267244d7a95c2693f45376f4f2e72d1e0c6c/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6a656e737365676572732f6f7074696d75732e737667)](https://coveralls.io/r/jenssegers/optimus?branch=master)

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.

[![](https://camo.githubusercontent.com/5859f808e69e041b0bbbde3614209f401d118b0637d695647363ed379d4d2f42/68747470733a2f2f6a656e737365676572732e636f6d2f7374617469632f6d656469612f6f7074696d75732e706e67)](https://camo.githubusercontent.com/5859f808e69e041b0bbbde3614209f401d118b0637d695647363ed379d4d2f42/68747470733a2f2f6a656e737365676572732e636f6d2f7374617469632f6d656469612f6f7074696d75732e706e67)

Installation
------------

[](#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](http://php.net/manual/en/book.gmp.php). 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
-----

[](#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](http://primes.utm.edu/lists/small/millions/) 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
---------------------

[](#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
----------------------

[](#framework-integrations)

### Laravel

[](#laravel)

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

```
