Giter Site home page Giter Site logo

ydatech / hashids.php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vinkla/hashids

0.0 2.0 0.0 140 KB

A small PHP class to generate YouTube-like ids from one or many numbers. Use hashids when you do not want to expose your database ids to the user.

Home Page: http://hashids.org/php

License: MIT License

PHP 100.00%

hashids.php's Introduction

hashids

Full Documentation

A small PHP class to generate YouTube-like ids from numbers. Read documentation at http://hashids.org/php

Build Status License

Installation

You can install Hashids thru Composer (packagist has hashids/hashids package). In your composer.json file use:

{
    "require": {
        "hashids/hashids": "^1.0"
    }
}

And run: php composer.phar install. After that you can require the autoloader and use Hashids:

require_once 'vendor/autoload.php';

use Hashids\Hashids;

$hashids = new Hashids('this is my salt');

Updating from v0.3 to 1.0?

Read the CHANGELOG!

Example Usage

The simplest way to use Hashids:

$hashids = new Hashids\Hashids();

$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);

var_dump($id, $numbers);
string(5) "laHquq"
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

And an example with all the custom parameters provided (unique salt value, minimum id length, custom alphabet):

$hashids = new Hashids('this is my salt', 8, 'abcdefghij1234567890');

$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);

var_dump($id, $numbers);
string(5) "514cdi42"
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Curses! #$%@

This code was written with the intent of placing created ids in visible places - like the URL. Which makes it unfortunate if generated hashes accidentally formed a bad word.

Therefore, the algorithm tries to avoid generating most common English curse words. This is done by never placing the following letters next to each other:

c, C, s, S, f, F, h, H, u, U, i, I, t, T

Big Numbers

Each number passed to the constructor cannot be negative or greater than 1 billion by default (1,000,000,000). Hashids encode() function will return an empty string if at least one of the numbers is out of bounds. Be sure to check for that -- no exception is thrown.

PHP starts approximating numbers when it does arithmetic on large integers (by converting them to floats). Which is usually not a big issue, but a problem when precise integers are needed.

However, if you have either GNU Multiple Precision --with-gmp, or BCMath Arbitrary Precision Mathematics --enable-bcmath libraries installed, Hashids will increase its upper limit to PHP_INT_MAX which is int(2147483647) on 32-bit systems and int(9223372036854775807) on 64-bit.

It will then use regular arithmetic on numbers less than 1 billion (because it's faster), and one of these libraries if greater than. GMP takes precedence over BCMath.

You can get the upper limit by doing: $hashids->get_max_int_value(); (which will stay at 1 billion if neither of the libraries is installed).

Speed

Even though speed is an important factor of every hashing algorithm, primary goal here was encoding several numbers at once while avoiding collisions.

On a 2.26 GHz Intel Core 2 Duo with 8GB of RAM, it takes about:

  1. 0.000093 seconds to encode one number.
  2. 0.000240 seconds to decode one id (while ensuring that it's valid).
  3. 0.493436 seconds to generate 10,000 ids in a for loop.

On a 2.7 GHz Intel Core i7 with 16GB of RAM, it takes roughly:

  1. 0.000067 seconds to encode one number.
  2. 0.000113 seconds to decode one id (and ensuring that it's valid).
  3. 0.297426 seconds to generate 10,000 ids in a for loop.

Sidenote: The numbers tested with were relatively small -- if you increase them, the speed will obviously decrease.

Notes

  • If you want to squeeze out even more performance, set a shorter alphabet. Hashes will be less random and longer, but calculating them will be faster.

Contact

I am on the internets @IvanAkimov

License

Hashids is licensed under The MIT License (MIT).

hashids.php's People

Contributors

ivanakimov avatar 4kimov avatar vinkla avatar arzeth avatar trismegiste avatar grahamcampbell avatar tothimre avatar w33ble avatar leihog avatar pablofmorales avatar philetaylor avatar mcmillanthomas avatar nyholm avatar leunggamciu avatar sokool avatar

Watchers

James Cloos avatar Yuda Sukmana avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.