Hashids

vinkla/hashids image

Hashids stats

Downloads
6.8M
Stars
5,065
Open Issues
0
Forks
418

View on GitHub →

A Hashids bridge for Laravel

Laravel Hashids Package Overview

The Hashids package is a PHP library designed to convert numeric identifiers into a short, unique, non-sequential hash, akin to YouTube's video IDs. This is particularly useful for developers looking to hide database numeric IDs from end users.

Key Features

  • Obfuscation of IDs: Converts numbers into a more user-friendly and non-sequential hash string.
  • Flexible Input Handling: Supports encoding from single, multiple numbers or arrays, and even hexadecimal values.
  • Customizable Hash Generation: Allows customization of hash output through salt, length, and custom alphabets.
  • Decoding Capability: Encoded strings can be easily decoded back to the original numbers.

Installation

To install Hashids, run the following Composer command in your project's root directory:

composer require hashids/hashids

Ensure you have one of the PHP extensions bcmath or gmp installed, as they are necessary for the package to function correctly.

Usage Examples

  1. Basic Encoding and Decoding:

    use Hashids\Hashids;
     
    $hashids = new Hashids();
    $id = $hashids->encode(1, 2, 3);
    $numbers = $hashids->decode($id);
  2. Customization:

    • Unique Salts: Different salts generate different hashes for the same input numbers.
    • Padding: Specify a minimum hash length.
    • Custom Alphabet: Define a custom alphabet for hash generation.
    • Hexadecimal Encoding: Encode and decode hexadecimal numbers.
    $hashids = new Hashids('My Project', 10, 'abcdefghijklmnopqrstuvwxyz');
    $id = $hashids->encode(1); // Custom length and alphabet

Important Notes

  • Output Format: Decoding always returns an array.
  • Non-Negative Inputs: Negative numbers cannot be encoded.
  • Error Handling: Invalid input to the encode() method returns an empty string.
  • Security: Hashids should not be used as a security measure or encryption tool.

Practical Use

While it does not offer security encryption, Hashids is excellent for obfuscating identifiers in public-facing elements like URLs, thereby preventing predictable sequential IDs and enhancing the aesthetic and security posture of web applications.

For more detailed information, visit the official Hashids documentation.

vinkla photo

I'm just Ken

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Vinkla Hashids Related Articles

Generate Short, URL-Safe IDs From Numbers With Sqids image

Generate Short, URL-Safe IDs From Numbers With Sqids

Read article
On-the-Fly Hashids with the Eloquent Hashids Package image

On-the-Fly Hashids with the Eloquent Hashids Package

Read article
Laravel Package: Optimus id transformation image

Laravel Package: Optimus id transformation

Read article
Laravel 5 Hashids image

Laravel 5 Hashids

Read article
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian