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:
With the help of this module you can shorten and obfuscate your IDs at the same time.
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 0
s 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 https://github.com/bbkr/TinyID
All examples are in example dir.
Classes of Kacper Rowinski | > | tinyID | > | Download .zip .tar.gz | > | Support forum | > | Blog | > | Latest changes |
|
|
Groups | Applications | Files |
Groups |
Algorithms | Numerical and statistical algorithms | View top rated classes |
PHP 5 | Classes using PHP 5 specific features | View top rated classes |
Cryptography | Encrypting, decrypting and hashing data | View top rated classes |
Text processing | Manipulating and validating text data | View top rated classes |
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Files |
File | Role | Description | ||
---|---|---|---|---|
example (1 file) | ||||
src (1 directory) | ||||
tests (1 directory) | ||||
.travis.yml | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Download all files: tinyid.tar.gz tinyid.zip NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
|