Obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.
Switch branches/tags
Clone or download
Latest commit 41c1e72 Feb 3, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Add HASHID_CONNECTION env key Nov 6, 2017
src Mark Lumen configure() with @codeCoverageIgnore Dec 1, 2017
tests Add artisan() for L5.0-5.1 Jan 18, 2018
.gitattributes initial commit Oct 6, 2017
.gitignore initial commit Oct 6, 2017
.php_cs.dist exclude resources Jan 17, 2018
.scrutinizer.yml Update CI config files Jan 17, 2018
.styleci.yml Remove unnecessary linting config Nov 1, 2017
.travis.yml Update CI config files Jan 17, 2018
CHANGELOG.md v1.2.0 Feb 3, 2018
LICENSE.md 2018 Jan 4, 2018
README.md formatting Nov 7, 2017
composer.json v1.2.0 Feb 3, 2018
phpunit.xml.dist initial commit Oct 6, 2017


Laravel Hashid

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

Laravel Hashid provides a unified API across various drivers such as Base62, Base64, Hashids and 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.


You can install this package using the Composer manager:

$ composer require elfsundae/laravel-hashid

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


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


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',



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

// Obtain the "base62" connection instance

// Obtain the Base64 driver instance

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.




And there are also two corresponding helper functions:

  • hashid_encode($data, $name = null)
  • hashid_decode($data, $name = null)

hashid_decode('TGFyYXZlbA', 'base64');

Built-in Drivers


  • Drivers: base62 , base62_integer
  • Configuration:
    • characters : 62 unique characters
  • Backend: tuupola/base62
  • Notes:
    • You may use the hashid:alphabet command to generate random characters.
    • GMP is strongly recommended as it is much faster than pure PHP.



  • Drivers: hashids , hashids_hex , hashids_integer , hashids_string
  • Configuration:
    • salt
    • min_length
    • alphabet : At least 16 unique characters
  • Backend: hashids/hashids
  • Notes:
    • You may use the hashid:alphabet command to generate a random alphabet.
    • GMP is strongly recommended.


  • Drivers: hex , hex_integer


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

To create a custom Hashid driver, you only need to implement the ElfSundae\Laravel\Hashid\DriverInterface 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:


namespace App\Hashid;

use ElfSundae\Laravel\Hashid\DriverInterface;
use Illuminate\Contracts\Encryption\Encrypter;

class CustomDriver implements DriverInterface
    protected $encrypter;

    protected $serialize;

    public function __construct(Encrypter $encrypter, array $config = [])
        $this->encrypter = $encrypter;

        $this->serialize = $config['serialize'] ?? false;

    public function encode($data)
        return $this->encrypter->encrypt($data, $this->serialize);

    public function decode($data)
        return $this->encrypter->decrypt($data, $this->serialize);

Now you can configure the connection with this driver:

'connections' => [

    'custom' => [
        'driver' => App\Hashid\CustomDriver::class,
        'serialize' => false,

    // ...

If you prefer a short name for your driver, just register a container binding with hashid.driver. prefix:

$this->app->bind('hashid.driver.custom', CustomDriver::class);

Donation 赞赏

If you find my work useful, please consider buying me a cup of coffee ☕️ , all donations are much appreciated ❤️

如果这个轮子对你有用,可否递我一支香烟 🚬赞赏是一种肯定! 谢谢 😘