PHP Classes

tinyID: Shorten and obfuscate identifier strings

Recommend this page to a friend!
  Info   View files Example   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 58%Total: 221 All time: 8,229 This week: 96Up
Version License PHP version Categories
tinyid 1.0MIT/X Consortium ...5.6Algorithms, PHP 5, Cryptography, Text...


Kacper Rowi?ski


This package can shorten and obfuscate identifier strings.

It takes a given key string made of unique Unicode characters that will work as dictionary.

The package can encode another string using the key string as dictionary so the resulting encoded string is made only of characters of the key string, resulting in a shorter string.

It can also take a previously encoded string and reverts to the original string using the same key.

Innovation Award
PHP Programming Innovation award nominee
September 2018
Number 2

Prize: One subscription to the PDF edition of the PHP Architect magazine
One way to share identifiers publicly without disclosing the actual information that they refer internally in an application is create alternative identifiers that act as keys for instance for searching the related information in a database that is managed by the application.

This package can create short identifiers for sharing publicly using private keys that the package can generate.

The package can also recover the original information that was encoded in the form of the short identifier that was created previously.

Manuel Lemos
Picture of Kacper Rowinski
  Performance   Level  
Name: Kacper Rowinski <contact>
Classes: 14 packages by
Country: Poland Poland
Age: 39
All time rank: 92326 in Poland Poland
Week rank: 132 Up1 in Poland Poland Up
Innovation award
Innovation award
Nominee: 8x


Shorten and obfuscate IDs

Build Status


    use TinyID\TinyID;
    include __DIR__ . '/../vendor/autoload.php';
    // dictionary must consist of at least two UNIQUE unicode characters.
    $tinyId = new TinyID('2BjLhRduC6Tb8Q5cEk9oxnFaWUDpOlGAgwYzNre7tI4yqPvXm0KSV1fJs3ZiHM');
    var_dump($tinyId->encode('48888851145')); // will print 1FN7Ab
    var_dump($tinyId->decode('1FN7Ab')); // will print 48888851145


Using real IDs in various places - such as GET links or API payload - is generally a bad idea:

  • It may reveal some sensitive informations about your business, such as growth rate or amount of customers.
  • If someone finds unprotected resource link, where you forgot to check if passed resource ID really belongs to currently logged-in user, he will be able to steal all of your data really fast just by incrementing ID in links.
  • Big numbers may cause overflows in places where length is limited, such as SMS messages.

With the help of this module you can shorten and obfuscate your IDs at the same time.


new TidyID('qwerty')

Key must consist of at least two *unique* unicode characters.

The longer the dictionary - the shorter encoded ID.

Encoded ID will be made exclusively out of characters from the key. This very useful property allows to adapt your encoding to the environment. For example in SMS messages you may restrict key to US ASCII to avoid available length reduction caused by conversion to GSM 03.38 charset. Or if you want to use such ID as file/directory name in case insensitive filesystem you may want to use only lowercase letters in the key.


Encode positive integer into a string.

Note that leading 0s are not preserved, encode(123) is the same as encode(00123).

Used algorithm is a base to the length of the key conversion that maps to distinct permutation of characters. Do not consider it a strong encryption, but if you have secret and long and well shuffled key it is almost impossible to reverse-engineer real ID.


Decode string back into a positive integer.


If you provide sequential characters in key you can convert your numbers to some weird numeric systems, for example base18:

    var_dump((new TinyID('0123456789ABCDEFGH'))->encode(48888851145)); // '47F709HFF'

Or you can go wild just for the fun of it.

    var_dump((new TinyID('????????????????????????????????????????????????????????????????'))->encode(48888851145)); // '??????'


It's based on great work of bbkr and his project

All examples are in example dir.

  Files folder image Files  
File Role Description
Files folder imageexample (1 file)
Files folder imagesrc (1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file Doc. Documentation

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file example.php Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageTinyID (1 file)

  Files folder image Files  /  src  /  TinyID  
File Role Description
  Plain text file TinyID.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageUnit (1 file)

  Files folder image Files  /  tests  /  Unit  
File Role Description
  Plain text file TinyIDTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:8,229
This week:96Up
User Ratings User Comments (1)
 All time
3 years ago (Igor Demin)