Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I wrote a tiny lib to generate obfuscated hashes from integers.</p> <p><a href="http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash" rel="noreferrer">http://web.archive.org/web/20130727034425/http://blog.kevburnsjr.com/php-unique-hash</a></p> <pre><code>$ids = range(1,10); foreach($ids as $id) { echo PseudoCrypt::unhash($id) . "\n"; }</code></pre> <pre>m8z2p 8hy5e uqx83 gzwas 38vdh phug6 bqtiv xzslk k8ro9 6hqqy</pre> <p>7/14/2015: Adding the actual code below, since it has become difficult to find:</p> <pre><code>&lt;?php /** * PseudoCrypt by KevBurns (http://blog.kevburnsjr.com/php-unique-hash) * Reference/source: http://stackoverflow.com/a/1464155/933782 * * I want a short alphanumeric hash that’s unique and who’s sequence is difficult to deduce. * I could run it out to md5 and trim the first n chars but that’s not going to be very unique. * Storing a truncated checksum in a unique field means that the frequency of collisions will increase * geometrically as the number of unique keys for a base 62 encoded integer approaches 62^n. * I’d rather do it right than code myself a timebomb. So I came up with this. * * Sample Code: * * echo "&lt;pre&gt;"; * foreach(range(1, 10) as $n) { * echo $n." - "; * $hash = PseudoCrypt::hash($n, 6); * echo $hash." - "; * echo PseudoCrypt::unhash($hash)."&lt;br/&gt;"; * } * * Sample Results: * 1 - cJinsP - 1 * 2 - EdRbko - 2 * 3 - qxAPdD - 3 * 4 - TGtDVc - 4 * 5 - 5ac1O1 - 5 * 6 - huKpGQ - 6 * 7 - KE3d8p - 7 * 8 - wXmR1E - 8 * 9 - YrVEtd - 9 * 10 - BBE2m2 - 10 */ class PseudoCrypt { /* Key: Next prime greater than 62 ^ n / 1.618033988749894848 */ /* Value: modular multiplicative inverse */ private static $golden_primes = array( '1' =&gt; '1', '41' =&gt; '59', '2377' =&gt; '1677', '147299' =&gt; '187507', '9132313' =&gt; '5952585', '566201239' =&gt; '643566407', '35104476161' =&gt; '22071637057', '2176477521929' =&gt; '294289236153', '134941606358731' =&gt; '88879354792675', '8366379594239857' =&gt; '7275288500431249', '518715534842869223' =&gt; '280042546585394647' ); /* Ascii : 0 9, A Z, a z */ /* $chars = array_merge(range(48,57), range(65,90), range(97,122)) */ private static $chars62 = array( 0=&gt;48,1=&gt;49,2=&gt;50,3=&gt;51,4=&gt;52,5=&gt;53,6=&gt;54,7=&gt;55,8=&gt;56,9=&gt;57,10=&gt;65, 11=&gt;66,12=&gt;67,13=&gt;68,14=&gt;69,15=&gt;70,16=&gt;71,17=&gt;72,18=&gt;73,19=&gt;74,20=&gt;75, 21=&gt;76,22=&gt;77,23=&gt;78,24=&gt;79,25=&gt;80,26=&gt;81,27=&gt;82,28=&gt;83,29=&gt;84,30=&gt;85, 31=&gt;86,32=&gt;87,33=&gt;88,34=&gt;89,35=&gt;90,36=&gt;97,37=&gt;98,38=&gt;99,39=&gt;100,40=&gt;101, 41=&gt;102,42=&gt;103,43=&gt;104,44=&gt;105,45=&gt;106,46=&gt;107,47=&gt;108,48=&gt;109,49=&gt;110, 50=&gt;111,51=&gt;112,52=&gt;113,53=&gt;114,54=&gt;115,55=&gt;116,56=&gt;117,57=&gt;118,58=&gt;119, 59=&gt;120,60=&gt;121,61=&gt;122 ); public static function base62($int) { $key = ""; while(bccomp($int, 0) &gt; 0) { $mod = bcmod($int, 62); $key .= chr(self::$chars62[$mod]); $int = bcdiv($int, 62); } return strrev($key); } public static function hash($num, $len = 5) { $ceil = bcpow(62, $len); $primes = array_keys(self::$golden_primes); $prime = $primes[$len]; $dec = bcmod(bcmul($num, $prime), $ceil); $hash = self::base62($dec); return str_pad($hash, $len, "0", STR_PAD_LEFT); } public static function unbase62($key) { $int = 0; foreach(str_split(strrev($key)) as $i =&gt; $char) { $dec = array_search(ord($char), self::$chars62); $int = bcadd(bcmul($dec, bcpow(62, $i)), $int); } return $int; } public static function unhash($hash) { $len = strlen($hash); $ceil = bcpow(62, $len); $mmiprimes = array_values(self::$golden_primes); $mmi = $mmiprimes[$len]; $num = self::unbase62($hash); $dec = bcmod(bcmul($num, $mmi), $ceil); return $dec; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload