Пример #1
0
function generate_switchid($switch_name, $timestamp)
{
    $hashids = new Hashids\Hashids('embiggen');
    $hash = $hashids->encrypt(strlen($switch_name), $timestamp, 6);
    $numbers = $hashids->decrypt($hash);
    //!debug
    //var_dump($hash, $numbers);
    return $hash;
}
<?php

/* including hashids code */
require_once __DIR__ . '/../lib/Hashids/Hashids.php';
/* creating class object */
$hashids = new Hashids\Hashids('this is my salt');
/* encrypting several numbers into one hash */
$hash = $hashids->encrypt(1337, 5, 77, 12345678);
/* decrypting that hash */
$numbers = $hashids->decrypt($hash);
/* $numbers is always an array */
var_dump($hash, $numbers);
exit;
<?php

/* including hashids code */
require_once __DIR__ . '/../lib/Hashids/Hashids.php';
/* creating class object with hash length of 8 */
$hashids = new Hashids\Hashids('this is my salt', 8);
/* encrypting several numbers into one hash (length of hash is going to be at least 8) */
$hash = $hashids->encrypt(1337, 5);
/* decrypting the same hash */
$numbers = $hashids->decrypt($hash);
/* $numbers is always an array */
var_dump($hash, $numbers);
exit;
 /**
  * Generates a new unique hash
  *
  * @return string - the new hash
  **/
 private function _generate_new_hash()
 {
     // Set up a new instance of hashids
     $hashids = new Hashids\Hashids(HASH_SALT, 1, $this->alphabet);
     // Set the default timezone
     date_default_timezone_set(TIMEZONE);
     // Get the daily count
     $dailycount = $this->_get_daily_count();
     // Let's check if the user passed in an ID to hash
     $passed_id = $this->_get_param('id');
     if ($passed_id) {
         $id = intval($passed_id . $dailycount);
     } else {
         // No id passed in?
         // Get the current timestamp as a number that represents YYMMDD
         // We'll append the daily count to it and use it as an ID
         $datestr = date('ymd');
         $id = intval($datestr . $dailycount);
     }
     // Generate the hash
     $hash = $hashids->encrypt($id);
     // Check to see if the hash is already in use
     if ($this->_does_hash_exist($hash)) {
         $this->_increment_daily_count();
         $this->_generate_new_hash();
     }
     return $hash;
 }
<?php

/* including hashids code */
require_once __DIR__ . '/../lib/Hashids/Hashids.php';
/* creating class object with custom alphabet */
$hashids = new Hashids\Hashids('this is my salt', 0, 'abcdefgh123456789');
/* encrypting several numbers into one hash */
$hash = $hashids->encrypt(1, 2, 3, 4);
/* decrypting the same hash */
$numbers = $hashids->decrypt($hash);
/* $numbers is always an array */
var_dump($hash, $numbers);
exit;
<?php

/* including hashids code */
require_once __DIR__ . '/../lib/Hashids/Hashids.php';
/* creating class object */
$hashids = new Hashids\Hashids('this is my salt');
/* encrypting several numbers into one hash */
$hash = $hashids->encrypt(45, 434, 1313, 99);
/* $hash is always a string */
var_dump($hash);
exit;