function _hashed_id_parse_request($qv)
{
    $hashed_id = $qv->query_vars['hashed_id'];
    if (strlen($hashed_id) > 0) {
        $hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH);
        $id = $hashids->decrypt($hashed_id);
        if (isset($id[0]) && is_numeric($id[0])) {
            $qv->query_vars['p'] = $id[0];
        } else {
            $qv->query_vars['pagename'] = $hashed_id;
        }
    }
    return $qv;
}
Example #2
0
<?php

/* test for speed */
$number_of_ints_to_encrypt_at_once = 3;
$start_at = 0;
$end_at = 100;
/* this script will encrypt AND decrypt (when it decrypts it checks that hash is legit) */
require_once '../lib/hashids.php-5-3.php';
$hashids = new hashids();
function microtime_float()
{
    list($usec, $sec) = explode(' ', microtime());
    return (double) $usec + (double) $sec;
}
$total = 0;
$time_start = microtime_float();
for ($i = $start_at; $i <= $end_at; $i++, $total++) {
    $numbers = array_fill(0, $number_of_ints_to_encrypt_at_once, $i);
    $hash = call_user_func_array(array($hashids, 'encrypt'), $numbers);
    $numbers = $hashids->decrypt($hash);
    echo $hash . ' - ' . implode(', ', $numbers) . "\n";
}
$time_stop = microtime_float();
$total = number_format($total);
echo "\nTotal hashes created: {$total}.\nTotal time: " . ($time_stop - $time_start) . "\n";
exit;
<?php

/* including hashids code */
require_once '../lib/hashids.php-5-3.php';
/* creating class object */
$hashids = new hashids('this is my salt');
/* encrypting one number */
$hash = $hashids->encrypt(1337);
/* $hash is always a string */
var_dump($hash);
exit;
<?php

/* including hashids code */
require_once '../lib/hashids.php-5-3.php';
/* creating class object with hash length of 8 */
$hashids = new 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;
<?php

/* test for collisions with 3 integers */
$start_at = 0;
$end_at = 15;
/* this script will create hashes and check against each other to make sure there are no collisions */
require_once '../lib/hashids.php-5-3.php';
$hashids = new hashids('this is my salt');
$hash_array = array();
$total = 0;
for ($i = $start_at; $i <= $end_at; $i++) {
    for ($j = $start_at; $j <= $end_at; $j++) {
        for ($k = $start_at; $k <= $end_at; $k++) {
            $hash = $hashids->encrypt($i, $j, $k);
            echo "{$hash} - {$i}, {$j}, {$k}\n";
            if (!isset($hash_array[$hash])) {
                $hash_array[$hash] = "{$i}, {$j}, {$k}";
            } else {
                echo "Collision for {$hash}: {$i}, {$j}, {$k} and " . $hash_array[$hash];
                exit;
            }
            $total++;
        }
    }
}
$total = number_format($total);
echo "\nNo collisions, ran through {$total} hashes.\n";
exit;
<?php

/* including hashids code */
require_once '../lib/hashids.php-5-3.php';
/* creating class object */
$hashids = new 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;
<?php

/* including hashids code */
require_once '../lib/hashids.php-5-3.php';
/* creating class object with custom alphabet */
$hashids = new 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 '../lib/hashids.php-5-3.php';
/* creating class object */
$hashids = new 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;