Created
February 18, 2019 09:49
-
-
Save jhony112/236494c66f208c60fbd5c2fc13d3152e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function alphaID($in, $to_num = false, $pad_up = false, $pass_key = null) | |
{ | |
$out = ''; | |
$index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$base = strlen($index); | |
if ($pass_key !== null) { | |
// Although this function's purpose is to just make the | |
// ID short - and not so much secure, | |
// with this patch by Simon Franz (http://blog.snaky.org/) | |
// you can optionally supply a password to make it harder | |
// to calculate the corresponding numeric ID | |
for ($n = 0; $n < strlen($index); $n++) { | |
$i[] = substr($index, $n, 1); | |
} | |
$pass_hash = hash('sha256',$pass_key); | |
$pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash); | |
for ($n = 0; $n < strlen($index); $n++) { | |
$p[] = substr($pass_hash, $n, 1); | |
} | |
array_multisort($p, SORT_DESC, $i); | |
$index = implode($i); | |
} | |
if ($to_num) { | |
// Digital number <<-- alphabet letter code | |
$len = strlen($in) - 1; | |
for ($t = $len; $t >= 0; $t--) { | |
$bcp = bcpow($base, $len - $t); | |
$out = $out + strpos($index, substr($in, $t, 1)) * $bcp; | |
} | |
if (is_numeric($pad_up)) { | |
$pad_up--; | |
if ($pad_up > 0) { | |
$out -= pow($base, $pad_up); | |
} | |
} | |
} else { | |
// Digital number -->> alphabet letter code | |
if (is_numeric($pad_up)) { | |
$pad_up--; | |
if ($pad_up > 0) { | |
$in += pow($base, $pad_up); | |
} | |
} | |
for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) { | |
$bcp = bcpow($base, $t); | |
$a = floor($in / $bcp) % $base; | |
$out = $out . substr($index, $a, 1); | |
$in = $in - ($a * $bcp); | |
} | |
} | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment