All Projects → veelasky → laravel-hashid

veelasky / laravel-hashid

Licence: MIT license
HashId Implementation on Laravel Eloquent ORM

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-hashid

hashids.pm
Hashids, ported for Perl
Stars: ✭ 15 (-34.78%)
Mutual labels:  hashids, hash
noble-hashes
Audited & minimal JS implementation of SHA2, SHA3, RIPEMD, BLAKE2/3, HMAC, HKDF, PBKDF2 & Scrypt
Stars: ✭ 213 (+826.09%)
Mutual labels:  hashing, hash
node-blake2
All four BLAKE2 variants (blake2b, blake2bp, blake2s, blake2sp) with stream support for Node.js
Stars: ✭ 52 (+126.09%)
Mutual labels:  hashing, hash
Util
A collection of useful utility functions
Stars: ✭ 201 (+773.91%)
Mutual labels:  hashing, hash
metrohash-rs
Rust MetroHash
Stars: ✭ 45 (+95.65%)
Mutual labels:  hashing, hash
Clhash
C library implementing the ridiculously fast CLHash hashing function
Stars: ✭ 220 (+856.52%)
Mutual labels:  hashing, hash
eloquent-hashids
Automatically generate and persist Hashids for newly created Eloquent models.
Stars: ✭ 17 (-26.09%)
Mutual labels:  hashids, eloquent
Minperf
A Minimal Perfect Hash Function Library
Stars: ✭ 107 (+365.22%)
Mutual labels:  hashing, hash
haiti
🔑 Hash type identifier (CLI & lib)
Stars: ✭ 287 (+1147.83%)
Mutual labels:  hashing, hash
objection-hashid
Objection plugin to automatically obfuscate model ids using hashids!
Stars: ✭ 13 (-43.48%)
Mutual labels:  hashids, hashid
Dagon
Advanced Hash Manipulation
Stars: ✭ 155 (+573.91%)
Mutual labels:  hashing, hash
harsh
Hashids implementation in Rust
Stars: ✭ 48 (+108.7%)
Mutual labels:  hashids, hash
Password4j
Password4j is a user-friendly cryptographic library that supports Argon2, Bcrypt, Scrypt, PBKDF2 and various cryptographic hash functions.
Stars: ✭ 124 (+439.13%)
Mutual labels:  hashing, hash
agent
hashtopolis.org
Stars: ✭ 19 (-17.39%)
Mutual labels:  hashing, hash
Data Structures
Data-Structures using C++.
Stars: ✭ 121 (+426.09%)
Mutual labels:  hashing, hash
prvhash
PRVHASH - Pseudo-Random-Value Hash. Hash functions, PRNG with unlimited period, randomness extractor. (Codename Gradilac/Градилак)
Stars: ✭ 194 (+743.48%)
Mutual labels:  hashing, hash
Eternal
A C++14 compile-time/constexpr map and hash map with minimal binary footprint
Stars: ✭ 93 (+304.35%)
Mutual labels:  hashing, hash
Xxhash cpp
Port of the xxhash library to C++17.
Stars: ✭ 106 (+360.87%)
Mutual labels:  hashing, hash
hash-checker
Fast and simple application that allows you to generate and compare hashes from files and text
Stars: ✭ 72 (+213.04%)
Mutual labels:  hashing, hash
id-mask
IDMask is a Java library for masking internal ids (e.g. from your DB) when they need to be published to hide their actual value and to prevent forging. It has support optional randomisation has a wide support for various Java types including long, UUID and BigInteger. This library bases its security on strong cryptographic primitives.
Stars: ✭ 39 (+69.57%)
Mutual labels:  hashids, hashid

Laravel HashId

Test Codacy Badge codecov Latest Stable Version StyleCI Total Downloads Dependents License

Automatic HashId generator for your eloquent model.

Version Compatibilities

Laravel HashId PHP Version Laravel 5.* Laravel 6.* Laravel 7.* Laravel 8.* Laravel 9.*
1.x >=7.0
2.x >=7.2 - <= 8.0
3.x >=7.4 || >= 8.0

Install

composer require veelasky/laravel-hashid

With laravel package auto discovery, this will automatically add this package to your laravel application.

TLDR

Simply add HashableId trait on any of your eloquent model you are intending to use with HashId.

Example:

use Illuminate\Database\Eloquent\Model;
use Veelasky\LaravelHashId\Eloquent\HashableId;

class User extends Model {
    use HashableId;
    ...
}

Usage

With Eloquent Model

$user = User::find(1);     // instance of user.
$user->hash;               // generate HashId.

// Database operation

// get user by hashed id.
$user = User::byHash($hash);

// get user by hashed id, and throw ModelNotFoundException if not present.
$user = User::byHashOrFail($hash);

// get hashed id from the primary key.
User::idToHash($id);

// get ID from hashed string.
User::hashToId($hash);

 // query scope with `byHash` method.
User::query()->byHash($hash);

By default, all hash calculation will be calculated at runtime, but sometime you want to persist the hashed id to the database.

NOTE: when using persisting model, all database query will be check againts the table itself, except: $model->hash will always be calculated at runtime.

class User extends Model {
    use HashableId;

    // add this property to your model if you want to persist to the database.
    protected $shouldHashPersist = true;

    // by default, the persisted value will be stored in `hashid` column
    // override column name to your desired name.
    protected $hashColumnName = 'hashid';
    ...
}

Route binding

When HashableId trait is used, base getRouteKey() and resolveRouteBinding() are overwritten to use the HashId as route key.

use App\Models\User;

class UserController extends Controller
{
    /**
     * Route /users/{user}
     * Ex: GET /users/k1jTdv6l
     */
    public function show(User $user)
    {
        ...
    }
}

In-Depth Coverage

This package use repository pattern to store all instantiated implementation of HashId\HashId class, this to achieve different hash result on every eloquent models defined with HashableId trait.

// using facade.
HashId::hashToId($hash, User::class)      // same as User::hashToId($hash);
HashId::idToHash($id, User::class)        // same as User::idToHash($hash);

// HashId facade class is an implementation of \Veelasky\Laravel\HashId\Repository

However you can opt-out to not using any eloquent model or implementing your own logic to the repository.

HashId::make($key, $salt);              // will return \HashId\HashId class.

// once you instantiated the object, you can retrieve it on your next operation
HashId::get($key);

If you're using single table inheritance model, where you want to has the same calculated hash across all inherited models, use $hashKey property, this will result the calculation remain the same across all inherited model.

class User extends Model {
    protected $hashKey = 'somethingUnique';
}

class Customer extends User {

}

$customer = Customer::find(1);
$user = User::find(1);

$user->hash; // will be equal to $customer->hash

You can also specify the length and characters of the hashed Id with HASHID_LENGTH and HASHID_ALPHABET environment variable respectively, or you can publish the configuration file using this command:

php artisan vendor:publish --tag=laravel-hashid-config

Extra: Validation Rules

You can also use this as validation rules, simply add this rule to your validator.

use App\Models\User;
use Veelasky\LaravelHashId\Rules\ExistsByHash;

...
Validator::make([
    'id' => $hashedId
], [
    'id' => ['required', new ExistsByHash(User::class)],
]);
...

License

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