2017 © Pedro Peláez
 

library hashids

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

image

hashids/hashids

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

  • Saturday, April 21, 2018
  • by ivanakimov
  • Repository
  • 115 Watchers
  • 2634 Stars
  • 2,821,328 Installations
  • PHP
  • 143 Dependents
  • 5 Suggesters
  • 242 Forks
  • 4 Open issues
  • 18 Versions
  • 10 % Grown

The README.md

hashids, (*1)

Build Status Monthly Downloads Latest Version, (*2)

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, (*3)

Getting started

Require this package, with Composer, in the root directory of your project., (*4)

composer require hashids/hashids

Then you can import the class into your application:, (*5)

use Hashids\Hashids;

$hashids = new Hashids();

$hashids->encode(1);

Note Hashids require either bcmath or gmp extension in order to work., (*6)

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:, (*7)

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., (*8)

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)., (*9)

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):, (*10)

    use Hashids\Hashids;
    
    $hashids = new Hashids();
    
    $id = $hashids->encode(1);
    
    $hashids->decode($id); // [1]
    
  2. Encoding negative numbers is not supported., (*11)

  3. If you pass bogus input to encode(), an empty string will be returned:, (*12)

    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., (*13)

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:, (*14)

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

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:, (*16)

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:, (*17)

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

The Versions

21/04 2018

dev-master

9999999-dev http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^7.1.3

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

12/03 2018

3.0.0

3.0.0.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^7.1.3

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

12/03 2018

dev-php7

dev-php7 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^7.1.3

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

28/10 2017

2.0.4

2.0.4.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.6.4 || ^7.0

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

25/06 2017

dev-develop

dev-develop http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.6.4 || ^7.0

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

01/01 2017

2.0.3

2.0.3.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.6.4 || ^7.0
  • ext-bcmath *

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

31/12 2016

2.0.2

2.0.2.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.6.4 || ^7.0
  • ext-bcmath *

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

29/12 2016

2.0.1

2.0.1.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.6.4 || ^7.0
  • ext-bcmath *

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

15/11 2016

2.0.0

2.0.0.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.6.4 || ^7.0
  • ext-bcmath *

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

26/04 2016

1.0.x-dev

1.0.9999999.9999999-dev http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.3.3 || ^7.0

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

21/01 2016

1.0.6

1.0.6.0 http://hashids.org/php

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

  Sources   Download

MIT

The Requires

  • php ^5.3.3 || ^7.0

 

The Development Requires

bitly hash encode decode youtube ids hashids obfuscate hashid

21/01 2015

1.0.5

1.0.5.0 http://hashids.org/php

Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate hashid

14/11 2014

1.0.4

1.0.4.0 http://hashids.org/php

Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate hashid

14/11 2014

1.0.3

1.0.3.0 http://hashids.org/php

Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate hashid

27/10 2014

1.0.2

1.0.2.0 http://hashids.org/php

Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate hashid

11/09 2014

1.0.1

1.0.1.0 http://hashids.org/php

Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate hashid

09/09 2014

1.0.0

1.0.0.0 http://hashids.org/php

Generate hashids like YouTube or Bitly from numbers to obfuscate your database primary ids, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate hashid

22/07 2014

0.3.1

0.3.1.0 http://www.hashids.org/php/

Generate hashes like YouTube or Bitly from numbers to obfuscate your database IDs, or navigate to the right shard.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

encrypt decrypt bitly hash youtube ids hashids obfuscate