├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── bin └── genset ├── composer.json ├── phpunit.xml ├── src ├── ZackKitzmiller │ ├── Facades │ │ └── Tiny.php │ ├── Laravel5 │ │ ├── TinyGenerateCommand.php │ │ └── TinyServiceProvider.php │ ├── Tiny.php │ ├── TinyGenerateCommand.php │ └── TinyServiceProvider.php └── config │ └── config.php └── tests └── TinyTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | build 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - hhvm 5 | - 5.6 6 | - 5.5 7 | - 5.4 8 | - 5.3 9 | 10 | before_script: 11 | - composer self-update 12 | - composer install 13 | 14 | script: 15 | - phpunit --coverage-text 16 | 17 | matrix: 18 | allow_failures: 19 | - php: hhvm 20 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | =============== 3 | 4 | Copyright (c) 2014 Zack Kitmziller 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/zackkitzmiller/tiny-php.png?branch=master)](https://travis-ci.org/zackkitzmiller/tiny-php) 2 | [![Coverage Status](https://coveralls.io/repos/zackkitzmiller/tiny-php/badge.png?branch=master)](https://coveralls.io/r/zackkitzmiller/tiny-php?branch=master) 3 | 4 | # Tiny 5 | 6 | A reversible base62 ID obfuscater 7 | 8 | ## Authors 9 | 10 | Originally by Jacob DeHart, with Ruby and Python ports by Kyle Bragger 11 | 12 | Now maintained by [Zack Kitzmiller](https://github.com/zackkitzmiller). 13 | 14 | ## Installation 15 | 16 | Install via Composer 17 | 18 | ```json 19 | { 20 | "require": { 21 | "zackkitzmiller/tiny": "1.2.0" 22 | }, 23 | } 24 | ``` 25 | 26 | ## Usage 27 | 28 | ```php 29 | $tiny = new \ZackKitzmiller\Tiny('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B'); 30 | 31 | echo $tiny->to(5); 32 | // E 33 | 34 | echo $tiny->from('E'); 35 | // 5 36 | 37 | echo $tiny->to(126); 38 | // XX 39 | 40 | echo $tiny->from('XX'); 41 | // 126 42 | 43 | echo $tiny->to(999); 44 | // vk 45 | 46 | echo $tiny->from('vk'); 47 | // 999 48 | 49 | ``` 50 | 51 | ## Configuration 52 | 53 | You must instanciate a new instance of Tiny with a random alpha-numeric set where each character must only be used exactly once. Do **NOT** change this once you start using Tiny, as you won't be able to reverse. 54 | 55 | You can generate a random set from the commandline with `$ ./bin/genset` 56 | 57 | ## Using laravel? 58 | 59 | If you're using laravel and want to use a more laravel-like and cleaner syntax you only have to follow these steps. 60 | 61 | First open your ``app/config/app.php`` file and scroll down to your providers and add 62 | ```php 63 | 'providers' => array( 64 | ... 65 | 'ZackKitzmiller\TinyServiceProvider', 66 | ) 67 | ``` 68 | and then this to aliases 69 | ```php 70 | 'aliases' => array( 71 | ... 72 | 'Tiny' => 'ZackKitzmiller\Facades\Tiny', 73 | ) 74 | ``` 75 | 76 | Lastly you run ``php artisan config:publish zackkitzmiller/tiny`` to publish the configuration file and then run ``php artisan tiny:generate`` to create a valid key. 77 | 78 | ### Usage in Laravel 79 | ```php 80 | echo Tiny::to(999); 81 | // echos vk 82 | 83 | echo Tiny::from('E'); 84 | // echos 5 85 | ``` 86 | -------------------------------------------------------------------------------- /bin/genset: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | 3 | =5.3.0" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "~3.7.0", 22 | "league/phpunit-coverage-listener": "~1.1" 23 | }, 24 | "autoload": { 25 | "psr-0": {"ZackKitzmiller": "src/"} 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | tests 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | src/ZackKitzmiller/Tiny.php 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | ZackKitzmiller 29 | 30 | 31 | 4GggSuZVGvVaxBi8E9IZTqG31EXFvjMe7 32 | 33 | 34 | https://coveralls.io/api/v1/jobs 35 | 36 | 37 | build/ 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/ZackKitzmiller/Facades/Tiny.php: -------------------------------------------------------------------------------- 1 | info("Tiny key [$key] has been set."); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/ZackKitzmiller/Laravel5/TinyServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('tiny.generate', function () { 11 | return new TinyGenerateCommand(); 12 | }); 13 | 14 | $this->commands('tiny.generate'); 15 | } 16 | 17 | public function register() 18 | { 19 | $this->app->singleton('tiny', function () { 20 | $key = getenv('LEAGUE_TINY_KEY'); 21 | 22 | return new Tiny($key); 23 | }); 24 | } 25 | 26 | public function provides() 27 | { 28 | return array('tiny'); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/ZackKitzmiller/Tiny.php: -------------------------------------------------------------------------------- 1 | set = $set; 9 | } 10 | 11 | public function to($id) 12 | { 13 | $set = $this->set; 14 | 15 | $hexn = ''; 16 | $id = floor(abs(intval($id))); 17 | $radix = strlen($set); 18 | while (true) { 19 | $r = $id % $radix; 20 | $hexn = $set{$r} . $hexn; 21 | $id = ($id - $r) / $radix; 22 | if ($id == 0) { 23 | break; 24 | } 25 | } 26 | return $hexn; 27 | } 28 | 29 | public function from($str) 30 | { 31 | $set = $this->set; 32 | 33 | $radix = strlen($set); 34 | $strlen = strlen($str); 35 | $n = 0; 36 | for ($i = 0; $i < $strlen; $i++) { 37 | $n += strpos($set, $str{$i}) * pow($radix, ($strlen - $i - 1)); 38 | } 39 | return $n; 40 | } 41 | 42 | public static function generate_set() 43 | { 44 | $arr = array(); 45 | 46 | for ($i = 65; $i <= 122; $i++) { 47 | if ($i < 91 || $i > 96) $arr[] = chr($i); 48 | } 49 | 50 | $arr = array_merge($arr, range(0, 9)); 51 | shuffle($arr); 52 | 53 | return join('', $arr); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/ZackKitzmiller/TinyGenerateCommand.php: -------------------------------------------------------------------------------- 1 | files = $files; 17 | } 18 | 19 | public function fire() 20 | { 21 | list($path, $contents) = $this->getKeyFile(); 22 | 23 | $key = Tiny::generate_set(); 24 | 25 | $contents = str_replace($this->laravel['config']['zackkitzmiller/tiny::key'], $key, $contents); 26 | 27 | $this->files->put($path, $contents); 28 | 29 | $this->laravel['config']['zackkitzmiller/tiny::key'] = $key; 30 | 31 | $this->info("Tiny key [$key] has been set."); 32 | } 33 | 34 | protected function getKeyFile() 35 | { 36 | $env = $this->option('env') ? $this->option('env').'/' : ''; 37 | 38 | $contents = $this->files->get($path = $this->laravel['path']."/config/packages/zackkitzmiller/tiny/{$env}config.php"); 39 | 40 | return array($path, $contents); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/ZackKitzmiller/TinyServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('zackkitzmiller/tiny', 'zackkitzmiller/tiny', __DIR__.'/../'); 10 | 11 | $this->app['tiny.generate'] = $this->app->share(function($app) 12 | { 13 | return new TinyGenerateCommand($app['files']); 14 | }); 15 | 16 | $this->commands('tiny.generate'); 17 | } 18 | 19 | public function register() 20 | { 21 | $this->app['tiny'] = $this->app->share(function($app) 22 | { 23 | $key = $app['config']['zackkitzmiller/tiny::key']; 24 | 25 | return new Tiny($key); 26 | }); 27 | } 28 | 29 | public function provides() 30 | { 31 | return array('tiny'); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | '', 15 | 16 | ); -------------------------------------------------------------------------------- /tests/TinyTest.php: -------------------------------------------------------------------------------- 1 | tiny = new Tiny('5SX0TEjkR1mLOw8Gvq2VyJxIFhgCAYidrclDWaM3so9bfzZpuUenKtP74QNH6B'); 11 | } 12 | 13 | public function testToTiny() { 14 | $converted = $this->tiny->to(5); 15 | $this->assertEquals('E', $converted); 16 | } 17 | 18 | public function testFromTiny() { 19 | $reversed = $this->tiny->from('E'); 20 | $this->assertEquals(5, $reversed); 21 | } 22 | 23 | public function testReversingRandomInt() { 24 | for ($i = 0; $i <= 100; $i++) { 25 | $this->assertEquals($this->tiny->from($this->tiny->to($i)), $i); 26 | } 27 | } 28 | 29 | public function testGenerateRandomSetsWork() { 30 | for ($i = 0; $i <= 1000; $i++) { 31 | $tiny = new Tiny(Tiny::generate_set()); 32 | $this->assertEquals($tiny->from($tiny->to($i)), $i); 33 | } 34 | } 35 | 36 | public function testGenerateSetUnique() { 37 | $set = Tiny::generate_set(); 38 | $set_parts = str_split($set); 39 | $used = array(); 40 | foreach ($set_parts as $char) { 41 | $this->assertArrayNotHasKey($char, $used); 42 | $used[$char] = $char; 43 | } 44 | } 45 | } 46 | --------------------------------------------------------------------------------