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: 223 This week: 1All time: 8,235 This week: 571Up
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


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: 40
All time rank: 91726 in Poland Poland
Week rank: 33 Up1 in Poland Poland Up
Innovation award
Innovation award
Nominee: 8x

Example

<?php

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')); // 1FN7Ab
var_dump($tinyId->decode('1FN7Ab')); // 48888851145

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

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


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:223
This week:1
All time:8,235
This week:571Up
User Ratings User Comments (1)
 All time
Utility:75%StarStarStarStar
Consistency:68%StarStarStarStar
Documentation:68%StarStarStarStar
Examples:68%StarStarStarStar
Tests:-
Videos:-
Overall:58%StarStarStar
Rank:1539
 
good
4 years ago (Igor Demin)
0%Star