Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

License

NotificationsYou must be signed in to change notification settings

ElfSundae/laravel-hashid

Repository files navigation

Latest Version on PackagistSoftware LicensetestsStyleCISymfonyInsight GradeQuality ScoreCode CoverageTotal Downloads

Laravel Hashid provides a unified API across various drivers such asBase62,Base64,Hashids andOptimus, 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

You can install this package using theComposer 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

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

Thehashid() helper or theHashid facade may be used to interact with any of your configured connections or drivers:

useElfSundae\Laravel\Hashid\Facades\Hashid;// Obtain the default connection instancehashid();Hashid::connection();// Obtain the "base62" connection instancehashid('base62');Hashid::connection('base62');// Obtain the Base64 driver instancehashid('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

Base62

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

Base64

Hashids

  • 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 thehashid:alphabet command to generate a random alphabet.
    • GMP is strongly recommended.

Hex

  • Drivers:hex ,hex_integer

Optimus

  • Drivers:optimus
  • Configuration:
    • prime : Large prime number lower than2147483647
    • inverse : The inverse prime so that(PRIME * INVERSE) & MAXID == 1
    • random : A large random integer lower than2147483647
  • Backend:jenssegers/optimus
  • Notes:
    • You may use thehashid:optimus command to generate needed numbers.
    • Only for integer numbers.
    • The max number can be handled correctly is2147483647.

Custom Drivers

To create a custom Hashid driver, you only need to implement theElfSundae\Laravel\Hashid\DriverInterface interface that contains two methods:encode anddecode. The constructor can optionally receive the driver configuration from a$config argument, and type-hinted dependencies injection is supported as well:

<?phpnamespaceApp\Hashid;useElfSundae\Laravel\Hashid\DriverInterface;useIlluminate\Contracts\Encryption\Encrypter;class CustomDriverimplements DriverInterface{protected$encrypter;protected$serialize;publicfunction__construct(Encrypter$encrypter,array$config = [])    {$this->encrypter =$encrypter;$this->serialize =$config['serialize'] ??false;    }publicfunctionencode($data)    {return$this->encrypter->encrypt($data,$this->serialize);    }publicfunctiondecode($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 withhashid.driver. prefix:

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

Testing

$ composertest

License

This package is open-sourced software licensed under theMIT License.

About

Obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp