PHPackages                             hashids/hashids - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. hashids/hashids

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hashids/hashids
===============

Generate short, unique, non-sequential ids (like YouTube and Bitly) from numbers

5.0.2(3y ago)5.4k48.6M—0.9%41420MITPHPPHP ^8.1CI passing

Since Jul 22Pushed 1mo ago108 watchersCompare

[ Source](https://github.com/vinkla/hashids)[ Packagist](https://packagist.org/packages/hashids/hashids)[ Docs](https://hashids.org/php)[ RSS](/packages/hashids-hashids/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (21)Used By (20)

[![hashids](https://raw.githubusercontent.com/hashids/hashids.github.io/master/public/img/hashids.gif "Hashids")](https://hashids.org/)

[![Build Status](https://camo.githubusercontent.com/b5419f4e6d5f5d81d170ef569b1b87ac8500f47e42d70f6ea2ea1584bef2ac00/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f76696e6b6c612f686173686964732f706870756e69742e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473)](https://github.com/vinkla/hashids/actions)[![Monthly Downloads](https://camo.githubusercontent.com/d9e9cbdebc7637b9e0a9a1755c87e599ef154d7a92113660f8bf21c09bf352c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f686173686964732f68617368696473)](https://packagist.org/packages/hashids/hashids/stats)[![Latest Version](https://camo.githubusercontent.com/323de8d1cf43d18cfdec41610aa220999637be8379214d75c910b8db16f76d9e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f686173686964732f68617368696473)](https://packagist.org/packages/hashids/hashids)

**Hashids** is a small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database numeric ids to users:

[ ![Sqids](https://private-user-images.githubusercontent.com/499192/408860913-75864e3a-483c-4b6f-9b0b-66f5e3d6e736.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NzkxNjk5NTUsIm5iZiI6MTc3OTE2OTY1NSwicGF0aCI6Ii80OTkxOTIvNDA4ODYwOTEzLTc1ODY0ZTNhLTQ4M2MtNGI2Zi05YjBiLTY2ZjVlM2Q2ZTczNi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNTE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDUxOVQwNTQ3MzVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mMzIzNGM4ZDFmNDcyNmY4MDVhOTMwNThkMWI1N2IxZTcwMjFmZTk0ZmFiZTBmNzAxOTFlYmM1MDNhZjJiNmI4JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZyZXNwb25zZS1jb250ZW50LXR5cGU9aW1hZ2UlMkZwbmcifQ.fWxMxXKe2Ohhp8pMYAdt7Fh0YtrCmJipeygLFWH2yeI)](https://sqids.org/)Note

The creator of Hashids has released a new, upgraded version rebranded as **Sqids**. However, Hashids will continue to be maintained and available for future use. For more information, please visit the [Sqids repository](https://github.com/sqids/sqids-php) and learn how it compares to Hashids on the [Sqids website](https://sqids.org/faq#hashids).

Getting started
---------------

[](#getting-started)

Require this package, with [Composer](https://getcomposer.org), in the root directory of your project.

```
composer require hashids/hashids
```

Then you can import the class into your application:

```
use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1);
```

> **Note** Hashids require either [`bcmath`](https://secure.php.net/manual/en/book.bc.php) or [`gmp`](https://secure.php.net/manual/en/book.gmp.php) extension in order to work.

Quick Example
-------------

[](#quick-example)

```
use Hashids\Hashids;

$hashids = new Hashids();

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

More Options
------------

[](#more-options)

#### A few more ways to pass input ids to the `encode()` function:

[](#a-few-more-ways-to-pass-input-ids-to-the-encode-function)

```
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
```

#### Making your output ids unique

[](#making-your-output-ids-unique)

Pass a project name to make your output ids unique:

```
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 make your output ids longer

[](#use-padding-to-make-your-output-ids-longer)

Note that output ids are only padded to fit **at least** a certain length. It doesn't mean that they will be *exactly* that 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
```

#### Using a custom alphabet

[](#using-a-custom-alphabet)

```
use Hashids\Hashids;

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

#### Encode hex instead of numbers

[](#encode-hex-instead-of-numbers)

Useful if you want to encode [Mongo](https://www.mongodb.com)'s ObjectIds. Note that *there is no limit* on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).

```
use Hashids\Hashids;

$hashids = new Hashids();

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

Pitfalls
--------

[](#pitfalls)

1. When decoding, output is always an array of numbers (even if you encoded only one number):

    ```
    use Hashids\Hashids;

    $hashids = new Hashids();

    $id = $hashids->encode(1);

    $hashids->decode($id); // [1]
    ```
2. Encoding negative numbers is not supported.
3. If you pass bogus input to `encode()`, an empty string will be returned:

    ```
    use Hashids\Hashids;

    $hashids = new Hashids();

    $id = $hashids->encode('123a');

    $id === ''; // true
    ```
4. Do not use this library as a security measure. **Do not** encode sensitive data with it. Hashids is **not** an encryption library.

Randomness
==========

[](#randomness)

The primary purpose of Hashids is to obfuscate numeric ids. It's **not** meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:

There is no pattern shown when encoding multiple identical numbers (3 shown in the following example):

```
use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(5, 5, 5); // A6t1tQ
```

The same is true when encoding a series of numbers vs. encoding them separately:

```
use Hashids\Hashids;

$hashids = new Hashids();

$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
```

Curse words! #$%@
-----------------

[](#curse-words-)

This code was written with the intent of placing the output ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:

```
c, f, h, i, s, t, u

```

###  Health Score

73

—

ExcellentBetter than 100% of packages

Maintenance61

Regular maintenance activity

Popularity81

Widely adopted with strong download metrics

Community58

Growing community involvement

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 73% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~165 days

Recently: every ~355 days

Total

20

Last Release

1180d ago

Major Versions

0.3.1 → 1.0.02014-09-09

1.0.x-dev → 2.0.02016-11-15

2.0.4 → 3.0.02018-03-12

3.0.0 → 4.0.02019-04-03

4.1.0 → 5.0.02023-02-14

PHP version history (7 changes)0.3.1PHP &gt;=5.3.0

1.0.6PHP ^5.3.3 || ^7.0

2.0.0PHP ^5.6.4 || ^7.0

3.0.0PHP ^7.1.3

4.0.0PHP ^7.2

4.1.0PHP ^7.2 || ^8.0

5.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/63e77154439395019fb74adc3d1402bbba09ab726c74e2448968a1174c6a4415?d=identicon)[vinkla](/maintainers/vinkla)

![](https://www.gravatar.com/avatar/ff560349f9e537ced353c02e70bdca5713dd9d08db288bd081e84a32d2f64574?d=identicon)[ivanakimov](/maintainers/ivanakimov)

---

Top Contributors

[![vinkla](https://avatars.githubusercontent.com/u/499192?v=4)](https://github.com/vinkla "vinkla (227 commits)")[![ivanakimov](https://avatars.githubusercontent.com/u/56521216?v=4)](https://github.com/ivanakimov "ivanakimov (27 commits)")[![4kimov](https://avatars.githubusercontent.com/u/56128128?v=4)](https://github.com/4kimov "4kimov (21 commits)")[![arzeth](https://avatars.githubusercontent.com/u/546779?v=4)](https://github.com/arzeth "arzeth (4 commits)")[![Trismegiste](https://avatars.githubusercontent.com/u/1260026?v=4)](https://github.com/Trismegiste "Trismegiste (4 commits)")[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (3 commits)")[![jwpage](https://avatars.githubusercontent.com/u/52687?v=4)](https://github.com/jwpage "jwpage (2 commits)")[![Nyholm](https://avatars.githubusercontent.com/u/1275206?v=4)](https://github.com/Nyholm "Nyholm (2 commits)")[![jamband](https://avatars.githubusercontent.com/u/1117087?v=4)](https://github.com/jamband "jamband (2 commits)")[![ignaciogc](https://avatars.githubusercontent.com/u/44265905?v=4)](https://github.com/ignaciogc "ignaciogc (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![PhilETaylor](https://avatars.githubusercontent.com/u/400092?v=4)](https://github.com/PhilETaylor "PhilETaylor (1 commits)")[![ptondereau](https://avatars.githubusercontent.com/u/4287777?v=4)](https://github.com/ptondereau "ptondereau (1 commits)")[![sokool](https://avatars.githubusercontent.com/u/4784286?v=4)](https://github.com/sokool "sokool (1 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")[![tothimre](https://avatars.githubusercontent.com/u/74795412?v=4)](https://github.com/tothimre "tothimre (1 commits)")[![w33ble](https://avatars.githubusercontent.com/u/404731?v=4)](https://github.com/w33ble "w33ble (1 commits)")[![bonfante](https://avatars.githubusercontent.com/u/8030979?v=4)](https://github.com/bonfante "bonfante (1 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (1 commits)")

---

Tags

composer-packagesdatabase-idsencodinghashhashidsidsphpphp-libraryhashyoutubeencodedecodebitlyidshashidsobfuscatehashid

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hashids-hashids/health.svg)

```
[![Health](https://phpackages.com/badges/hashids-hashids/health.svg)](https://phpackages.com/packages/hashids-hashids)
```

###  Alternatives

[delight-im/ids

Short, obfuscated and efficient IDs for PHP

289.5k1](/packages/delight-im-ids)[sqids/sqids

Generate short YouTube-looking IDs from numbers

5781.4M33](/packages/sqids-sqids)[jenssegers/optimus

Id obfuscation based on Knuth's integer hash method

1.3k4.8M27](/packages/jenssegers-optimus)[torann/hashids

Laravel package for Hashids

54335.1k](/packages/torann-hashids)[elfsundae/laravel-hashid

A simple, elegant way to obfuscate your data by generating reversible, non-sequential, URL-safe identifiers.

415246.3k2](/packages/elfsundae-laravel-hashid)[christian-riesen/base32

Base32 encoder/decoder according to RFC 4648

13331.8M61](/packages/christian-riesen-base32)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
