• Stars
    star
    396
  • Rank 108,801 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Laravel Hashid

Latest Version on Packagist Software License tests StyleCI SymfonyInsight Grade 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.

More Repositories

1

AVDemo

Demo projects for iOS Audio & Video development.
Objective-C
189
star
2

laravel-apps

Laravel multi-application support.
PHP
85
star
3

laravel-bearychat

Laravel integration for BearyChat.
PHP
73
star
4

bearychat

An elegant way of interacting with BearyChat webhooks.
PHP
66
star
5

iOS-Model-List

The ultimate list of  iOS device models - Identify model for iPhone, iPad, iPod touch, Apple Watch, Apple TV, and Mac computers with Apple Silicon.
HTML
36
star
6

laravel-gravatar

👤 The easiest way to generate Gravatar avatar URL.
PHP
36
star
7

IconFontKit

Icon fonts toolkit for iOS.
Objective-C
30
star
8

ESFramework

An efficient, lightweight foundational framework for iOS, macOS, tvOS, watchOS, and Mac Catalyst.
Objective-C
15
star
9

Awesome-iOS

A curated list of most common & awesome iOS frameworks, libraries, tools, components and much more.
15
star
10

laravel-hashid-uuid

Shorten UUID encoding for Laravel Hashid.
PHP
12
star
11

SQLitePersistentObjects

SQLite Persistent Objects for iOS.
Objective-C
10
star
12

urlsafe-base64

URL safe base64 encoding for PHP.
PHP
9
star
13

console

CLI library based on Laravel Console for creating PHP console application.
PHP
8
star
14

build-laravel.com

Build mirror of Laravel.com
Shell
7
star
15

AppComponents

A series of components for iOS development.
Objective-C
5
star
16

sync-git-mirror

Sync Git Mirror - Keep your git mirrors and forks up to date.
Shell
5
star
17

laravel-skeleton

Laravel package skeleton.
PHP
5
star
18

tools-archived

My tools and scripts.
3
star
19

ESMediaPlayerDemo

Demo for [FFmpeg-iOS-build](https://github.com/ElfSundae/FFmpeg-iOS-build)
Objective-C
3
star
20

CocoaPods-China-Mirror

🇨🇳 CocoaPods CDN 中国镜像源,全量同步 pods 索引和 podspec 文件
3
star
21

FFmpeg-iOS-build

Build FFmpeg for iOS, supports armv7, armv7s and i386 (iOS Simulator) architectures.
Perl
3
star
22

crtmpserver

Git mirror of crtmpserver's SVN trunk. http://rtmpd.com/
C++
2
star
23

FaceUnity

iOS face-beautification toolkit built upon FaceUnity Nama SDK and FULiveDemo. 相芯美颜 SDK 集成工具包。
Objective-C
2
star
24

CocoaPods-Specs

My CocoaPods Specs
Ruby
2
star
25

iOS-Runtime-Headers

iOS 9+ Objective-C headers as derived from runtime introspection.
Objective-C
2
star
26

xgpush

【信鸽推送】Tencent Xinge Push PHP SDK
PHP
1
star
27

ESCodeSection

Demonstration to make Xcode plugin.
Objective-C
1
star
28

rtb

The rtb is a command line tool which generate pure C-Style array from any resource file. http://0x123.com
C
1
star
29

TalkingDataAppAnalytics

TalkingData App Analytics SDK
Objective-C
1
star
30

NIM_iOS_Demo

网易云信 iOS Demo App
Objective-C
1
star
31

KSYMediaEditorKit_iOS

金山云短视频编辑 SDK 备份
Objective-C
1
star
32

laravel-api

PHP
1
star