All Projects → ElfSundae → Laravel Hashid

ElfSundae / Laravel Hashid

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

Projects that are alternatives of or similar to Laravel Hashid

Laravel Gdpr
GDPR compliance with ease.
Stars: ✭ 189 (-46.61%)
Mutual labels:  laravel, encryption
Laravel Hashids
Integrate Hashids with Laravel. Automatic model binding and id resolving included!
Stars: ✭ 8 (-97.74%)
Mutual labels:  hashids, laravel
Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (-32.77%)
Mutual labels:  laravel, encryption
Laravel Source Encrypter
Laravel and Lumen Source Code Encrypter
Stars: ✭ 175 (-50.56%)
Mutual labels:  laravel, encryption
Laravel Hashslug
Package providing a trait to use Hashids on a model
Stars: ✭ 136 (-61.58%)
Mutual labels:  hashids, laravel
Securitydriven.inferno
✅ .NET crypto done right. Professionally audited.
Stars: ✭ 501 (+41.53%)
Mutual labels:  base64, encryption
Ksprefs
🚀⚡ Kotlin SharedPreferences wrapper & cryptographic preferences android library.
Stars: ✭ 176 (-50.28%)
Mutual labels:  base64, encryption
Optimus
🤖 Id obfuscation based on Knuth's multiplicative hashing method for PHP.
Stars: ✭ 1,084 (+206.21%)
Mutual labels:  hashids, laravel
Laravel Hashids
A Hashids bridge for Laravel
Stars: ✭ 1,714 (+384.18%)
Mutual labels:  hashids, laravel
Laravel Optimus
Transform your internal id's to obfuscated integers based on Knuth's integer hash. Laravel wrapper for the Optimus Library by Jens Segers with multiple connections support.
Stars: ✭ 119 (-66.38%)
Mutual labels:  hashids, laravel
Eloquent Hashids
On-the-fly hashids for Laravel Eloquent models. (🍰 Easy & ⚡ Fast)
Stars: ✭ 196 (-44.63%)
Mutual labels:  hashids, laravel
Laravel Fakeid
Automatic model ID obfuscation in routes for Laravel
Stars: ✭ 161 (-54.52%)
Mutual labels:  hashids, laravel
jose-simple
Jose-Simple allows the encryption and decryption of data using the JOSE (JSON Object Signing and Encryption) standard.
Stars: ✭ 50 (-85.88%)
Mutual labels:  base64, encryption
Blessing Skin Server
Web application brings your custom skins back in offline Minecraft servers.
Stars: ✭ 344 (-2.82%)
Mutual labels:  laravel
Kissme
Kissme: Kotlin Secure Storage Multiplatform
Stars: ✭ 351 (-0.85%)
Mutual labels:  encryption
Laravel Code Style
Automatic code formatting for Laravel projects
Stars: ✭ 344 (-2.82%)
Mutual labels:  laravel
Laravel Dynamodb
Eloquent syntax for DynamoDB
Stars: ✭ 342 (-3.39%)
Mutual labels:  laravel
Uniquewith Validator
Custom Laravel Validator for combined unique indexes
Stars: ✭ 352 (-0.56%)
Mutual labels:  laravel
Gitamin
An open-source, self-hosted git repository management system. QQ群:656868
Stars: ✭ 350 (-1.13%)
Mutual labels:  laravel
Presenter
Decorate your objects using presenters. Primarily to keep presentation logic out of your models.
Stars: ✭ 344 (-2.82%)
Mutual labels:  laravel

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.

Installation

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:

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

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

Base62

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

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 the hashid: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 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:

<?php

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);

Testing

$ composer test

License

This package is open-sourced software licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].