• Stars
    star
    5,053
  • Rank 8,241 (Top 0.2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

hashids

Build Status Monthly Downloads Latest Version

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: https://hashids.org/php

Getting started

Require this package, with Composer, 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 or gmp extension in order 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

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

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

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

use Hashids\Hashids;

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

Encode hex instead of numbers

Useful if you want to encode Mongo'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

  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

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! #$%@

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

More Repositories

1

wordplate

WordPlate is a boilerplate for WordPress, built with Composer and designed with sensible defaults.
PHP
1,974
star
2

laravel-hashids

A Hashids bridge for Laravel
PHP
1,909
star
3

instagram

An easy-to-use and simple Instagram package
PHP
493
star
4

extended-acf

Register advanced custom fields with object oriented PHP
PHP
376
star
5

climb

A Composer version manager tool
PHP
359
star
6

laravel-translator

An Eloquent translator for Laravel
PHP
274
star
7

laravel-shield

A HTTP basic auth middleware for Laravel
PHP
228
star
8

laravel-gitlab

A GitLab bridge for Laravel
PHP
143
star
9

laravel-facebook

A Facebook bridge for Laravel
PHP
114
star
10

laravel-alert

A Bootstrap alert helper for Laravel
PHP
110
star
11

laravel-backup

A easy-to-use backup manager for Laravel
PHP
93
star
12

laravel-algolia

An Algolia bridge for Laravel
PHP
71
star
13

headache

An easy-to-swallow painkiller plugin for WordPress
PHP
71
star
14

recaptcha

An easy-to-use reCAPTCHA package
PHP
55
star
15

alfred-packagist

Alfred workflow to search for PHP packages with Packagist
JavaScript
21
star
16

base62

A reversible base62 ID obfuscator
PHP
21
star
17

local-login

Enable automatic login within a local WordPress environment
PHP
4
star
18

laravel-sqids

PHP
3
star
19

extended-api

Enhancing the WordPress REST API with advanced request and response objects. Work in progress.
PHP
3
star
20

bason

A lightweight JSON viewer extension for Chromium based browsers
JavaScript
2
star
21

salts

A salts generator for environment files in WordPlate
HTML
2
star
22

tabsli.de

Simple text slides for your browser-based presentation.
Svelte
1
star