<?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 /* 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;
function _hashed_id_post_link($permalink, $post) { $hashids = new hashids(AUTH_KEY, HASHED_IDS_MIN_LENGTH); $permalink = str_replace('%hashed_id%', $hashids->encrypt((int) $post->ID), $permalink); return $permalink; }
<?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;