PHPackages                             zhujinkui/hashlib - 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. zhujinkui/hashlib

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

zhujinkui/hashlib
=================

Hashlib is small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user: http://hashids.org/php

V1.0(7y ago)31271Apache-2.0PHPPHP &gt;=5.6.0

Since Sep 25Pushed 7y agoCompare

[ Source](https://github.com/zhujinkui/hashlib)[ Packagist](https://packagist.org/packages/zhujinkui/hashlib)[ RSS](/packages/zhujinkui-hashlib/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

hashlib
=======

[](#hashlib)

Hashlib是一个小型PHP库，可以从数字中生成类似YouTube的ID，当您不想将数据库ID暴露给用户时使用它。

**hashlib** is small PHP library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user:

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

[](#getting-started)

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

```
$ composer require zhujinkui/hashlib
```

Then you can import the class into your application:

```
use think\Hashlib;

$hashlib = new Hashlib();

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

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

[](#quick-example)

```
use think\Hashlib;

$hashlib = new Hashlib();

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

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

[](#more-options)

**A few more ways to pass to `encode()`:**

```
use think\Hashlib;

$hashlib = new Hashlib();

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

**Make your ids unique:**

Pass a project name to make your ids unique:

```
use think\Hashlib;

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

$hashlib = new Hashlib('My Other Project');
$hashlib->encode(1, 2, 3); // gPUasb
```

**Use padding to make your ids longer:**

Note that ids are only padded to fit **at least** a certain length. It doesn't mean that your ids will be *exactly* that length.

```
use think\Hashlib;

$hashlib = new Hashlib(); // no padding
$hashlib->encode(1); // jR

$hashlib = new Hashlib('', 10); // pad to length 10
$hashlib->encode(1); // VolejRejNm
```

**Pass a custom alphabet:**

```
use think\Hashlib;

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

**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 think\Hashlib;

$hashlib = new Hashlib();

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

Pitfalls
--------

[](#pitfalls)

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

    ```
    use think\Hashlib;

    $hashlib = new Hashlib();

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

    $hashlib->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 think\Hashlib;

    $hashlib = new Hashlib();

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

    $id === ''; // true
    ```
4. Do not use this library as a security tool and do not encode sensitive data. This is **not** an encryption library.

Randomness
==========

[](#randomness)

The primary purpose of Hashids is to obfuscate 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:

No repeating patterns showing there are 3 identical numbers in the id:

```
use think\Hashlib;

$hashlib = new Hashlib();

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

Same with incremented numbers:

```
use think\Hashlib;

$hashlib = new Hashlib();

$hashlib->encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // wpfLh9iwsqt0uyCEFjHM

$hashlib->encode(1); // jR
$hashlib->encode(2); // k5
$hashlib->encode(3); // l5
$hashlib->encode(4); // mO
$hashlib->encode(5); // nR
```

Curses! #$%@
------------

[](#curses-)

This code was written with the intent of placing created 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

```

License
-------

[](#license)

MIT License. See the [LICENSE](LICENSE) file. You can use Hashids in open source projects and commercial products. Don't break the Internet. Kthxbye.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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

Unknown

Total

1

Last Release

2835d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/36879659?v=4)[朱金奎](/maintainers/zhujinkui)[@zhujinkui](https://github.com/zhujinkui)

---

Top Contributors

[![262877348](https://avatars.githubusercontent.com/u/3145551?v=4)](https://github.com/262877348 "262877348 (1 commits)")[![zhujinkui](https://avatars.githubusercontent.com/u/36879659?v=4)](https://github.com/zhujinkui "zhujinkui (1 commits)")

### Embed Badge

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

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

###  Alternatives

[jakoch/phantomjs-installer

A Composer package which installs the PhantomJS binary (Linux, Windows, Mac) into `/bin` of your project.

1515.4M72](/packages/jakoch-phantomjs-installer)[solspace/craft-calendar

The most powerful event management and calendaring plugin!

1831.6k1](/packages/solspace-craft-calendar)

PHPackages © 2026

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