PHPackages                             elfsundae/laravel-hashid - 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. [Security](/categories/security)
4. /
5. elfsundae/laravel-hashid

ActiveLibrary[Security](/categories/security)

elfsundae/laravel-hashid
========================

A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

1.7.6(3mo ago)416260.7k↓50.5%36[3 issues](https://github.com/ElfSundae/laravel-hashid/issues)2MITPHPPHP ^7.1|^8.0CI passing

Since Nov 6Pushed 3mo ago7 watchersCompare

[ Source](https://github.com/ElfSundae/laravel-hashid)[ Packagist](https://packagist.org/packages/elfsundae/laravel-hashid)[ Docs](https://github.com/ElfSundae/laravel-hashid)[ RSS](/packages/elfsundae-laravel-hashid/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (27)Versions (20)Used By (2)

Laravel Hashid
==============

[](#laravel-hashid)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e1301eb906895844cfc6a9d184a57ba9bf719a49f8a45f048b310d75be51648a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c6673756e6461652f6c61726176656c2d6861736869642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elfsundae/laravel-hashid)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![tests](https://github.com/ElfSundae/laravel-hashid/actions/workflows/tests.yml/badge.svg)](https://github.com/ElfSundae/laravel-hashid/actions/workflows/tests.yml)[![StyleCI](https://camo.githubusercontent.com/df908094c33e4de916e6bb6ef82fff58c6c7ecb55e88f1ef00e1a5345c025948/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130363034343133312f736869656c64)](https://styleci.io/repos/106044131)[![SymfonyInsight Grade](https://camo.githubusercontent.com/57a0ea2f8bbadf25c1d110795100233a3227fe3114c7d8212c985329fe95cda6/68747470733a2f2f696d672e736869656c64732e696f2f73796d666f6e792f692f67726164652f35656366646233642d623263342d343763612d623530622d3466303431666534326634663f7374796c653d666c61742d737175617265)](https://insight.symfony.com/projects/5ecfdb3d-b2c4-47ca-b50b-4f041fe42f4f)[![Quality Score](https://camo.githubusercontent.com/636db1f954166709ec799dad6950d035f6654c63ca8da4802fe5376685df49c1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f456c6653756e6461652f6c61726176656c2d6861736869642e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ElfSundae/laravel-hashid)[![Code Coverage](https://camo.githubusercontent.com/8e91ad4b7a0540310a1929699ae99e515f16e6086defae1d990910a159861b72/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f456c6653756e6461652f6c61726176656c2d6861736869642f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/ElfSundae/laravel-hashid/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/a7b809f6b85136ab5a33414ffc09c84e0761a7beb7c53ecc0dbb17a0ba77631e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c6673756e6461652f6c61726176656c2d6861736869642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elfsundae/laravel-hashid)

Laravel Hashid provides a unified API across various drivers such as [Base62](https://github.com/tuupola/base62), [Base64](https://github.com/ElfSundae/urlsafe-base64), [Hashids](https://github.com/ivanakimov/hashids.php) and [Optimus](https://github.com/jenssegers/optimus), with support for multiple connections or different encoding options. It offers a simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Built-in Drivers](#built-in-drivers)
    - [Base62](#base62)
    - [Base64](#base64)
    - [Hashids](#hashids)
    - [Hex](#hex)
    - [Optimus](#optimus)
- [Custom Drivers](#custom-drivers)
- [Testing](#testing)
- [License](#license)

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

[](#installation)

You can install this package using the [Composer](https://getcomposer.org) manager:

```
$ composer require elfsundae/laravel-hashid
```

For Lumen or earlier Laravel than v5.5, you need to register the service provider manually:

```
ElfSundae\Laravel\Hashid\HashidServiceProvider::class
```

Then publish the configuration file:

```
# For Laravel application:
$ php artisan vendor:publish --tag=hashid

# For Lumen application:
$ cp vendor/elfsundae/laravel-hashid/config/hashid.php config/hashid.php
```

Configuration
-------------

[](#configuration)

Our well documented configuration file is extremely similar to the configurations of numerous Laravel manager integrations such as Database, Queue, Cache and Filesystem. So you do not need to spend extra time to learn how to configure Hashid.

Additionally, for simplicity you do not need to add singleton drivers like Base64 to your config file as they have no encoding options, unless you would like to specify a meaningful connection name.

Let's see an example of the configuration:

```
'default' => 'id',

'connections' => [

    'basic' => [
        'driver' => 'base64',
    ],

    'hashids' => [
        'driver' => 'hashids',
        'salt' => 'sweet girl',
    ],

    'id' => [
        'driver' => 'hashids_integer',
        'salt' => 'My Application',
        'min_length' => 6,
        'alphabet' => '1234567890abcdef',
    ],

    'base62' => [
        'driver' => 'base62',
        'characters' => 'f9FkqDbzmn0QRru7PBVeGl5pU28LgIvYwSydK41sCO3htaicjZoWAJNxH6EMTX',
    ],

],
```

Usage
-----

[](#usage)

The `hashid()` helper or the `Hashid` facade may be used to interact with any of your configured connections or drivers:

```
use ElfSundae\Laravel\Hashid\Facades\Hashid;

// Obtain the default connection instance
hashid();
Hashid::connection();

// Obtain the "base62" connection instance
hashid('base62');
Hashid::connection('base62');

// Obtain the Base64 driver instance
hashid('base64');
Hashid::connection('base64');
Hashid::driver('base64');
```

There are only two methods you need to know to use any connection or driver:

- `encode($data)` for encoding data.
- `decode($data)` for decoding data.

```
hashid()->encode(123456);

hashid('base64')->decode('TGFyYXZlbA');

Hashid::encode(123456);

Hashid::connection('hashids')->decode('X68fkp');
```

And there are also two corresponding helper functions:

- `hashid_encode($data, $name = null)`
- `hashid_decode($data, $name = null)`

```
hashid_encode(123456);

hashid_decode('TGFyYXZlbA', 'base64');
```

Built-in Drivers
----------------

[](#built-in-drivers)

#### Base62

[](#base62)

- Drivers: `base62` , `base62_integer`
- Configuration:
    - `characters` : 62 unique characters
- Backend: [`tuupola/base62`](https://github.com/tuupola/base62)
- Notes:
    - You may use the `hashid:alphabet` command to generate random characters.
    - [GMP](https://secure.php.net/gmp) is strongly recommended as it is much faster than pure PHP.

#### Base64

[](#base64)

- Drivers: `base64` , `base64_integer`
- Backend: [`elfsundae/urlsafe-base64`](https://github.com/ElfSundae/urlsafe-base64)

#### Hashids

[](#hashids)

- Drivers: `hashids` , `hashids_hex` , `hashids_integer` , `hashids_string`
- Configuration:
    - `salt`
    - `min_length`
    - `alphabet` : At least 16 unique characters
- Backend: [`hashids/hashids`](https://github.com/ivanakimov/hashids.php)
- Notes:
    - You may use the `hashid:alphabet` command to generate a random alphabet.
    - [GMP](https://secure.php.net/gmp) is strongly recommended.

#### Hex

[](#hex)

- Drivers: `hex` , `hex_integer`

#### Optimus

[](#optimus)

- Drivers: `optimus`
- Configuration:
    - `prime` : Large prime number lower than `2147483647`
    - `inverse` : The inverse prime so that `(PRIME * INVERSE) & MAXID == 1`
    - `random` : A large random integer lower than `2147483647`
- Backend: [`jenssegers/optimus`](https://github.com/jenssegers/optimus)
- Notes:
    - You may use the `hashid:optimus` command to generate needed numbers.
    - Only for integer numbers.
    - The max number can be handled correctly is `2147483647`.

Custom Drivers
--------------

[](#custom-drivers)

To create a custom Hashid driver, you only need to implement the [`ElfSundae\Laravel\Hashid\DriverInterface`](src/DriverInterface.php) interface that contains two methods: `encode` and `decode`. The constructor can optionally receive the driver configuration from a `$config` argument, and type-hinted dependencies injection is supported as well:

```
