PHP Classes
elePHPant
Icontem

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   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-09-24 (5 months ago) RSS 2.0 feedStarStarStarStar 77%Total: 165 This week: 2All time: 8,506 This week: 234Up
Version License PHP version Categories
tinyid 1.0MIT/X Consortium ...5.6Algorithms, PHP 5, Cryptography, Text...
Description Author

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
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
  Performance   Level  
Name: Kacper Rowinski <contact>
Classes: 9 packages by
Country: Poland Poland
Age: 35
All time rank: 109828 in Poland Poland
Week rank: 39 Up1 in Poland Poland Up
Innovation award
Innovation award
Nominee: 4x

Details

Shorten and obfuscate IDs

Build Status

SYNOPSIS

    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

DESCRIPTION

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.

METHODS

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(123)

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('rer')

Decode string back into a positive integer.

TRICKS

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)); // '??????'

OTHER IMPLEMENTATIONS

It's based on great work of bbkr and his project https://github.com/bbkr/TinyID

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 README.md 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  
 100%
Total:165
This week:2
All time:8,506
This week:234Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:77%StarStarStarStar
Rank:80