Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2015 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a86de347781146402857 to your computer and use it in GitHub Desktop.
Save anonymous/a86de347781146402857 to your computer and use it in GitHub Desktop.
static function generateId()
{
$id = str_replace('-', '', Q_Utils::uuid());
$secret = $secret = Q_Config::get('Q', 'external', 'secret', null);
if (isset($secret)) {
$id = $id . md5($id . $secret);
}
$id = base64_encode(pack('H*', $id));
return str_replace(array('z', '+', '/', '='), array('zz', 'za', 'zb', 'zc'), $id);
}
static function decodeId($id)
{
$result = '';
$len = strlen($id);
$escaped = false;
$i = 0;
$replacements = array(
'z' => 'z',
'a' => '+',
'b' => '/',
'c' => '='
);
while ($i < $len-1) {
$r = $id[$i];
$c1 = $id[$i];
++$i;
if ($c1 == 'z') {
$c2 = $id[$i];
if (isset($replacements[$c2])) {
$r = $replacements[$c2];
++$i;
}
}
$result .= $r;
}
if ($i < $len) {
$result .= $id[$i];
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment