How to Obfuscate Numeric IDs with Hashids in PHP

This guide explains what the Hashids PHP library is, how to install and use it with Composer, demonstrates encoding and decoding examples, covers advanced options like custom salts, padding, alphabets, hexadecimal encoding, and highlights common pitfalls and randomness considerations.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Obfuscate Numeric IDs with Hashids in PHP

What is Hashids?

Hashids is a small PHP library that generates YouTube‑like IDs from numbers, useful when you don’t want to expose raw database IDs.

Getting Started

Install the package via Composer: composer require hashids/hashids Import the class and create an instance:

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1);
Note: Hashids requires the bcmath or gmp extension to work.

Quick Example

use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encode(1, 2, 3); // o2fXhV
$numbers = $hashids->decode($id); // [1, 2, 3]

More Options

Different ways to pass input IDs to encode()

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1, 2, 3); // o2fXhV
$hashids->encode([1, 2, 3]); // o2fXhV
$hashids->encode('1', '2', '3'); // o2fXhV
$hashids->encode(['1', '2', '3']); // o2fXhV

Make output IDs unique per project

Provide a salt (project name) when constructing the Hashids object:

use Hashids\Hashids;

$hashids = new Hashids('My Project');
$hashids->encode(1, 2, 3); // Z4UrtW

$hashids = new Hashids('My Other Project');
$hashids->encode(1, 2, 3); // gPUasb

Use padding to increase minimum length

Hashids pads the output to at least the specified length.

use Hashids\Hashids;

$hashids = new Hashids(); // no padding
$hashids->encode(1); // jR

$hashids = new Hashids('', 10); // pad to length 10
$hashids->encode(1); // VolejRejNm

Custom alphabet

use Hashids\Hashids;

$hashids = new Hashids('', 0, 'abcdefghijklmnopqrstuvwxyz'); // all lowercase
$hashids->encode(1, 2, 3); // mdfphx

Encode hexadecimal values

Useful for encoding MongoDB ObjectIds.

use Hashids\Hashids;

$hashids = new Hashids();

$id = $hashids->encodeHex('507f1f77bcf86cd799439011'); // y42LW46J9luq3Xq9XMly
$hex = $hashids->decodeHex($id); // 507f1f77bcf86cd799439011

Pitfalls

Decoding always returns an array, even for a single number.

Negative numbers are not supported.

Invalid input to encode() returns an empty string.

Do not use Hashids as a security measure; it is not encryption.

Randomness

Hashids is designed to obscure numeric IDs, not to provide cryptographic security or compression. The algorithm attempts to produce seemingly random, unpredictable IDs, as shown in the examples.

use Hashids\Hashids;

$hashids = new Hashids();
$hashids->encode(5, 5, 5); // A6t1tQ

$hashids->encode(1,2,3,4,5,6,7,8,9,10); // wpfLh9iwsqt0uyCEFjHM
$hashids->encode(1); // jR
$hashids->encode(2); // k5
$hashids->encode(3); // l5
$hashids->encode(4); // mO
$hashids->encode(5); // nR
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

hashidsid obfuscation
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.