3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Crypt { /* Key: Next prime greater than 62 ^ n / 1.618033988749894848 */ /* Value: modular multiplicative inverse */ private static $golden_primes = array( '1' => '1', '41' => '59', '2377' => '1677', '147299' => '187507', '9132313' => '5952585', '566201239' => '643566407', '35104476161' => '22071637057', '2176477521929' => '294289236153', '134941606358731' => '88879354792675', '8366379594239857' => '7275288500431249', '518715534842869223' => '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=>48,1=>49,2=>50,3=>51,4=>52,5=>53,6=>54,7=>55,8=>56,9=>57,10=>65, 11=>66,12=>67,13=>68,14=>69,15=>70,16=>71,17=>72,18=>73,19=>74,20=>75, 21=>76,22=>77,23=>78,24=>79,25=>80,26=>81,27=>82,28=>83,29=>84,30=>85, 31=>86,32=>87,33=>88,34=>89,35=>90,36=>97,37=>98,38=>99,39=>100,40=>101, 41=>102,42=>103,43=>104,44=>105,45=>106,46=>107,47=>108,48=>109,49=>110, 50=>111,51=>112,52=>113,53=>114,54=>115,55=>116,56=>117,57=>118,58=>119, 59=>120,60=>121,61=>122 ); public static function base62($int) { $key = ""; while(bccomp($int, 0) > 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 => $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; } } $hashParams = array( 'plopi' => 'gato', 'token' => 'lol' ); var_dump($hashParams); $hashParams = 'poney'; $strEncoded = Crypt::hash(json_encode($hashParams)); echo "\n encoded\t:" . json_encode($hashParams). "\n"; echo "\n encoded\t:" . $strEncoded. "\n"; echo "\n decode encoded\t:" . Crypt::unhash($strEncoded)."\n"; var_dump(json_decode(Crypt::unhash($strEncoded), true));
Output for 8.0.2 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array(2) { ["plopi"]=> string(4) "gato" ["token"]=> string(3) "lol" } Fatal error: Uncaught ValueError: bcmul(): Argument #1 ($num1) is not well-formed in /in/HnP9r:49 Stack trace: #0 /in/HnP9r(49): bcmul('"poney"', '566201239') #1 /in/HnP9r(84): Crypt::hash('"poney"') #2 {main} thrown in /in/HnP9r on line 49
Process exited with code 255.
Output for 7.3.32 - 7.3.33, 7.4.33, 8.0.13
array(2) { ["plopi"]=> string(4) "gato" ["token"]=> string(3) "lol" } Fatal error: Uncaught Error: Call to undefined function bcpow() in /in/HnP9r:46 Stack trace: #0 /in/HnP9r(84): Crypt::hash('"poney"') #1 {main} thrown in /in/HnP9r on line 46
Process exited with code 255.
Output for 7.4.0 - 7.4.32, 8.0.0 - 8.0.1
array(2) { ["plopi"]=> string(4) "gato" ["token"]=> string(3) "lol" } Warning: bcmul(): bcmath function argument is not well-formed in /in/HnP9r on line 49 encoded :"poney" encoded :00000 decode encoded :0 int(0)
Output for 5.2.1 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31
array(2) { ["plopi"]=> string(4) "gato" ["token"]=> string(3) "lol" } encoded :"poney" encoded :00000 decode encoded :0 int(0)
Output for 5.2.0
array(2) { ["plopi"]=> string(4) "gato" ["token"]=> string(3) "lol" } encoded :"poney" encoded :00000 decode encoded :0 NULL
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
array(2) { ["plopi"]=> string(4) "gato" ["token"]=> string(3) "lol" } Fatal error: Call to undefined function json_encode() in /in/HnP9r on line 84
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CLASS in /in/HnP9r on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_CLASS in /in/HnP9r on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/HnP9r on line 5
Process exited with code 255.

preferences:
239.1 ms | 401 KiB | 352 Q