3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Crypt { private static $range = 62; private static $goldenPrimes36 = array( /* * Key..: next prime greater than 36 ^ n / 1.618033988749894848 * Value: modular multiplicative inverse */ '1' => '1', '23' => '11', '809' => '809', '28837' => '29485', '1038073' => '179017', '37370153' => '47534873', '1345325473' => '264202849', '48431716939' => '19727015779', '1743541808839' => '1532265214711', '62767505117101' => '67935388019749', '3656158440062987' => '3323780400057251', '131621703842267239' => '14056686818106199' ); private static $goldenPrimes62 = array( /* * Key..: next prime greater than 62 ^ n / 1.618033988749894848 * Value: modular multiplicative inverse */ '1' => '1', '41' => '59', '2377' => '1677', '147299' => '187507', '9132313' => '5952585', '566201239' => '643566407', '35104476161' => '22071637057', '2176477521929' => '294289236153', '134941606358731' => '88879354792675', '8366379594239857' => '7275288500431249', '518715534842869223' => '280042546585394647' ); private static $chars62 = array( // Ascii : 0-9 0 => 48, 1 => 49, 2 => 50, 3 => 51, 4 => 52, 5 => 53, 6 => 54, 7 => 55, 8 => 56, 9 => 57, // Ascii : A-Z 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, // Ascii : a-z 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 ); /** * Calculates the hash 36 or 62 based range. * @param integer $int * @return string */ private static function baseX($int) { $key = ''; while (bccomp($int, 0) > 0) { $mod = bcmod($int, self::$range); $key .= chr(self::$chars62[$mod]); $int = bcdiv($int, self::$range, 0); } return strrev($key); } /** * Inverse calculation for hashes 36 or 62 based range. * @param string $key * @return integer */ private static function unbaseX($key) { $int = 0; foreach (str_split(strrev($key)) as $i => $char) { $dec = array_search(ord($char), self::$chars62); $int = bcadd(bcmul($dec, bcpow(self::$range, $i)), $int); } return $int; } /** * Generates the hash 62 based chars(a-z, A-Z, 0-9) to a given int number. * Important: * – the biggest cryptable integer with hash length = 5 is 916 132 831. * – the biggest cryptable integer with hash length = 6 is 56 800 235 583. It seems to be enough, no? * * @param integer $num * @param integer $len * @return string */ public static function hash($num, $len = 5) { $ceil = bcpow(self::$range, $len); $primes = array_keys(self::$range === 36 ? self::$goldenPrimes36 : self::$goldenPrimes62); $prime = $primes[$len]; $dec = bcmod(bcmul($num, $prime, 0),$ceil); $hash = self::baseX($dec); return str_pad($hash, $len, '0', STR_PAD_LEFT); } /** * Generates the hash 36 based chars(A-Z, 0-9) to a given int number. * * Important: * * – the biggest cryptable integer with hash length = 5 is 60 466 175. * * – the biggest cryptable integer with hash length = 6 is 2 176 782 335. */ public static function hash36($num, $len = 5) { self::$range = 36; return self::hash($num, $len); } /** * Converts a previous generated hash to its original integer. * @param string $hash * @return integer */ public static function unhash($hash) { $len = strlen($hash); $ceil = bcpow(self::$range, $len); $mmiprimes = array_values(self::$range === 36 ? self::$goldenPrimes36 : self::$goldenPrimes62); $mmi = $mmiprimes[$len]; $num = self::unbaseX($hash); $dec = bcmod(bcmul($num, $mmi), $ceil); return $dec; } } $hashParams = array( 'plopi' => 'gato', 'token' => 'lol' ); var_dump($hashParams); $strEncoded = Crypt::hash(json_encode($hashParams)); echo "\n encoded\t:" . $strEncoded. "\n"; echo "\n decode encoded\t:" . Crypt::unhash($strEncoded)."\n"; var_dump(json_decode(Crypt::unhash($strEncoded), true));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0170.00318.55
8.3.60.0180.00018.56
8.3.50.0130.00822.11
8.3.40.0120.00318.79
8.3.30.0070.00719.17
8.3.20.0030.00620.16
8.3.10.0030.00621.55
8.3.00.0060.00319.15
8.2.180.0120.00318.54
8.2.170.0100.00722.96
8.2.160.0070.00722.16
8.2.150.0000.00824.18
8.2.140.0050.00324.66
8.2.130.0030.00626.16
8.2.120.0030.00519.21
8.2.110.0000.00922.11
8.2.100.0060.00618.22
8.2.90.0030.00519.08
8.2.80.0050.00317.97
8.2.70.0030.00617.63
8.2.60.0070.00317.93
8.2.50.0040.00418.07
8.2.40.0030.00519.83
8.2.30.0020.00518.17
8.2.20.0040.00417.77
8.2.10.0030.00618.23
8.2.00.0070.00017.79
8.1.280.0110.00425.92
8.1.270.0060.00319.13
8.1.260.0040.00426.35
8.1.250.0100.00028.09
8.1.240.0000.00923.91
8.1.230.0090.00319.23
8.1.220.0050.00317.74
8.1.210.0030.00618.77
8.1.200.0070.00317.36
8.1.190.0080.00017.25
8.1.180.0050.00318.10
8.1.170.0040.00418.65
8.1.160.0000.00821.97
8.1.150.0030.00618.81
8.1.140.0030.00619.11
8.1.130.0000.00717.89
8.1.120.0000.00717.51
8.1.110.0000.00717.36
8.1.100.0040.00417.46
8.1.90.0040.00417.30
8.1.80.0000.00817.41
8.1.70.0000.00717.44
8.1.60.0040.00417.59
8.1.50.0040.00417.44
8.1.40.0060.00317.59
8.1.30.0090.00017.57
8.1.20.0050.00317.68
8.1.10.0050.00217.58
8.1.00.0030.00517.59
8.0.300.0030.00518.77
8.0.290.0040.00416.88
8.0.280.0050.00218.44
8.0.270.0040.00417.32
8.0.260.0060.00017.25
8.0.250.0070.00016.98
8.0.240.0030.00317.04
8.0.230.0000.00817.08
8.0.220.0030.00317.03
8.0.210.0040.00416.96
8.0.200.0040.00417.02
8.0.190.0030.00616.97
8.0.180.0040.00417.01
8.0.170.0000.00817.00
8.0.160.0000.00716.96
8.0.150.0000.00816.86
8.0.140.0000.00716.90
8.0.130.0000.00613.38
8.0.120.0060.00316.91
8.0.110.0030.00616.89
8.0.100.0000.00816.86
8.0.90.0040.00416.94
8.0.80.0100.00616.97
8.0.70.0040.00416.93
8.0.60.0040.00416.87
8.0.50.0000.00816.84
8.0.30.0120.00917.12
8.0.20.0040.01517.40
8.0.10.0000.00817.13
8.0.00.0090.01116.82
7.4.330.0000.00515.03
7.4.320.0030.00316.64
7.4.300.0000.00716.58
7.4.290.0040.00416.57
7.4.280.0050.00216.65
7.4.270.0040.00416.71
7.4.260.0080.00016.66
7.4.250.0040.00416.48
7.4.240.0020.00516.61
7.4.230.0030.00516.73
7.4.220.0120.00616.52
7.4.210.0030.01216.61
7.4.200.0020.00516.77
7.4.160.0160.00016.53
7.4.150.0200.00017.40
7.4.140.0090.00917.86
7.4.130.0120.00716.69
7.4.120.0100.01116.54
7.4.110.0090.00916.46
7.4.100.0120.00916.66
7.4.90.0070.01716.50
7.4.80.0150.00919.39
7.4.70.0050.01316.57
7.4.60.0030.01316.58
7.4.50.0040.00016.46
7.4.40.0100.00716.48
7.4.30.0130.00916.73
7.4.10.0130.00614.91
7.4.00.0060.01014.91
7.3.330.0070.00013.25
7.3.320.0030.00313.32
7.3.310.0050.00316.52
7.3.300.0030.00316.27
7.3.290.0060.00816.39
7.3.280.0120.00716.39
7.3.270.0050.01417.40
7.3.260.0110.00716.49
7.3.250.0120.00416.53
7.3.240.0170.00516.72
7.3.230.0080.01316.45
7.3.210.0090.00916.70
7.3.200.0130.00319.39
7.3.190.0100.00716.67
7.3.180.0040.01316.69
7.3.170.0130.00616.50
7.3.160.0100.01316.42
7.3.130.0140.00415.02
7.3.120.0050.01214.84
7.3.110.0030.01414.96
7.3.100.0020.01114.92
7.3.90.0050.00714.81
7.3.80.0070.00514.93
7.3.70.0040.01114.88
7.3.60.0050.00914.95
7.3.50.0040.01214.94
7.3.40.0040.00914.77
7.3.30.0050.00814.87
7.3.20.0030.01016.68
7.3.10.0090.00416.61
7.3.00.0030.01116.28
7.2.330.0140.00516.52
7.2.320.0120.00616.79
7.2.310.0030.01516.93
7.2.300.0110.00516.86
7.2.290.0070.01016.88
7.2.260.0150.00315.08
7.2.250.0000.01615.02
7.2.240.0050.01014.84
7.2.230.0060.00815.11
7.2.220.0030.01215.30
7.2.210.0040.01115.09
7.2.200.0100.00515.07
7.2.190.0020.01015.14
7.2.180.0060.00915.07
7.2.170.0020.01214.90
7.2.160.0040.00815.07
7.2.150.0020.01016.85
7.2.140.0050.00916.92
7.2.130.0060.00916.80
7.2.120.0060.01116.87
7.2.110.0060.00916.82
7.2.100.0040.00916.73
7.2.90.0050.01016.95
7.2.80.0070.00816.94
7.2.70.0040.00916.84
7.2.60.0070.00916.77
7.2.50.0050.00916.85
7.2.40.0030.01117.05
7.2.30.0050.00916.96
7.2.20.0060.00816.98
7.2.10.0070.00916.93
7.2.00.0060.00717.63
7.1.330.0020.01115.80
7.1.320.0090.00515.72
7.1.310.0090.00515.77
7.1.300.0020.01315.79
7.1.290.0060.00715.71
7.1.280.0020.00915.76
7.1.270.0070.00715.64
7.1.260.0080.00515.88
7.1.250.0040.00715.78
7.1.240.0070.00715.62
7.1.230.0060.00815.79
7.1.220.0080.00815.76
7.1.210.0000.01815.97
7.1.200.0030.01015.97
7.1.190.0070.00315.71
7.1.180.0110.00716.01
7.1.170.0030.01315.75
7.1.160.0000.01215.94
7.1.150.0060.00915.63
7.1.140.0000.01515.89
7.1.130.0000.01015.71
7.1.120.0000.01215.90
7.1.110.0060.00815.57
7.1.100.0090.00317.00
7.1.90.0050.00515.65
7.1.80.0030.00615.97
7.1.70.0050.00616.36
7.1.60.0080.01217.65
7.1.50.0080.01216.42
7.1.40.0070.00715.81
7.1.30.0070.01015.78
7.1.20.0100.00715.76
7.1.10.0070.00715.59
7.1.00.0050.04319.02
7.0.330.0040.01115.44
7.0.320.0000.00915.14
7.0.310.0100.00315.50
7.0.300.0000.00915.18
7.0.290.0000.00815.35
7.0.280.0030.00615.44
7.0.270.0030.00615.43
7.0.260.0040.01115.43
7.0.250.0120.00315.36
7.0.240.0060.00615.59
7.0.230.0000.00915.45
7.0.220.0030.01015.29
7.0.210.0090.00615.38
7.0.200.0020.00816.22
7.0.190.0040.00815.23
7.0.180.0040.01115.50
7.0.170.0030.01315.54
7.0.160.0100.00715.43
7.0.150.0030.00615.26
7.0.140.0020.02718.94
7.0.130.0120.00315.08
7.0.120.0070.00715.53
7.0.110.0030.01015.32
7.0.100.0030.00715.51
7.0.90.0230.04717.66
7.0.80.0270.04517.63
7.0.70.0320.04817.91
7.0.60.0140.02617.60
7.0.50.0300.04117.84
7.0.40.0080.04316.84
7.0.30.0080.04316.69
7.0.20.0020.03216.87
7.0.10.0050.02716.89
7.0.00.0100.03516.80
5.6.400.0030.01214.62
5.6.390.0080.00014.24
5.6.380.0090.00614.34
5.6.370.0120.00313.98
5.6.360.0060.00614.16
5.6.350.0000.00814.39
5.6.340.0060.00914.50
5.6.330.0090.00614.09
5.6.320.0070.00714.66
5.6.310.0030.01114.12
5.6.300.0030.00814.36
5.6.290.0090.00614.29
5.6.280.0070.03517.79
5.6.270.0080.00614.43
5.6.260.0080.00314.17
5.6.250.0090.00614.48
5.6.240.0070.04017.66
5.6.230.0030.04517.64
5.6.220.0080.03117.52
5.6.210.0110.03817.53
5.6.200.0060.02717.79
5.6.190.0020.05217.85
5.6.180.0090.04217.63
5.6.170.0080.04217.66
5.6.160.0080.04317.85
5.6.150.0020.05117.64
5.6.140.0100.02717.75
5.6.130.0050.04517.76
5.6.120.0080.04717.86
5.6.110.0030.04417.66
5.6.100.0120.03817.65
5.6.90.0070.04517.83
5.6.80.0060.04117.52
5.6.70.0050.04717.46
5.6.60.0080.04017.51
5.6.50.0050.03517.57
5.6.40.0050.03817.46
5.6.30.0100.04317.30
5.6.20.0160.03217.55
5.6.10.0050.04717.46
5.6.00.0140.03017.28
5.5.380.0050.04617.24
5.5.370.0050.02817.30
5.5.360.0080.02817.37
5.5.350.0060.03717.40
5.5.340.0070.04017.80
5.5.330.0100.03917.54
5.5.320.0080.03717.71
5.5.310.0020.03117.67
5.5.300.0030.04617.66
5.5.290.0070.04717.59
5.5.280.0090.04417.53
5.5.270.0030.05117.60
5.5.260.0040.03417.54
5.5.250.0080.02717.42
5.5.240.0080.02517.08
5.5.230.0070.03017.29
5.5.220.0100.03517.21
5.5.210.0120.03717.17
5.5.200.0030.03017.19
5.5.190.0080.04017.30
5.5.180.0050.04317.25
5.5.170.0000.01513.95
5.5.160.0050.04317.30
5.5.150.0080.03317.23
5.5.140.0030.03617.24
5.5.130.0020.04717.37
5.5.120.0100.04617.27
5.5.110.0040.04517.39
5.5.100.0030.04717.10
5.5.90.0050.02717.15
5.5.80.0020.04817.28
5.5.70.0050.03017.31
5.5.60.0050.02517.24
5.5.50.0020.04517.22
5.5.40.0050.04417.31
5.5.30.0070.04617.29
5.5.20.0100.02617.10
5.5.10.0090.04717.01
5.5.00.0050.04817.13
5.4.450.0030.04115.25
5.4.440.0100.04015.12
5.4.430.0050.04115.12
5.4.420.0050.02715.17
5.4.410.0050.04315.12
5.4.400.0050.02514.82
5.4.390.0030.04214.91
5.4.380.0080.03515.12
5.4.370.0050.02315.05
5.4.360.0060.03114.72
5.4.350.0050.03714.89
5.4.340.0050.03414.84
5.4.330.0060.00310.88
5.4.320.0020.02714.94
5.4.310.0060.02315.02
5.4.300.0090.02315.18
5.4.290.0080.03814.97
5.4.280.0010.04015.08
5.4.270.0100.03514.81
5.4.260.0080.03814.97
5.4.250.0100.03714.97
5.4.240.0050.04014.94
5.4.230.0070.02015.01
5.4.220.0020.02914.88
5.4.210.0070.04014.95
5.4.200.0130.03315.01
5.4.190.0060.03015.05
5.4.180.0100.03615.12
5.4.170.0090.03114.91
5.4.160.0070.04214.74
5.4.150.0070.03714.93
5.4.140.0030.04313.57
5.4.130.0100.03513.58
5.4.120.0050.03513.69
5.4.110.0080.02513.76
5.4.100.0080.02713.69
5.4.90.0060.03713.68
5.4.80.0060.03413.76
5.4.70.0090.03613.38
5.4.60.0100.03213.48
5.4.50.0060.04313.62
5.4.40.0060.04013.79
5.4.30.0080.03013.78
5.4.20.0030.03013.50
5.4.10.0060.03013.56
5.4.00.0020.03813.38
5.3.290.0000.05014.70
5.3.280.0100.07314.75
5.3.270.0030.08014.67
5.3.260.0000.07714.61
5.3.250.0070.05714.71
5.3.240.0100.06314.71
5.3.230.0030.05314.61
5.3.220.0030.04314.54
5.3.210.0030.05014.75
5.3.200.0000.07314.74
5.3.190.0000.05014.62
5.3.180.0070.06714.62
5.3.170.0000.06714.75
5.3.160.0030.06314.60
5.3.150.0030.04714.60
5.3.140.0100.07014.52
5.3.130.0100.05714.52
5.3.120.0070.05014.54
5.3.110.0030.08314.58
5.3.100.0030.05314.16
5.3.90.0000.08014.20
5.3.80.0070.06014.19
5.3.70.0100.05713.98
5.3.60.0170.05013.94
5.3.50.0030.05313.91
5.3.40.0030.05313.93
5.3.30.0100.05314.09
5.3.20.0130.07013.63
5.3.10.0070.04313.75
5.3.00.0070.06713.58
5.2.170.0000.03311.29
5.2.160.0130.05011.25
5.2.150.0070.06011.30
5.2.140.0070.06011.08
5.2.130.0100.03011.07
5.2.120.0030.05711.20
5.2.110.0030.06311.20
5.2.100.0100.03011.21
5.2.90.0030.03711.21
5.2.80.0000.04311.27
5.2.70.0030.03711.04
5.2.60.0100.05311.23
5.2.50.0170.05011.12
5.2.40.0030.06311.09
5.2.30.0070.05311.09
5.2.20.0030.04310.98
5.2.10.0000.06311.02
5.2.00.0030.05010.75
5.1.60.0030.04010.03
5.1.50.0000.04010.13
5.1.40.0100.04010.14
5.1.30.0030.05310.42
5.1.20.0170.05310.51
5.1.10.0100.06710.11
5.1.00.0030.0509.97
5.0.50.0070.0408.59
5.0.40.0000.0278.55
5.0.30.0070.0338.37
5.0.20.0130.0338.29
5.0.10.0100.0308.31
5.0.00.0070.0608.28
4.4.90.0000.0406.91
4.4.80.0030.0336.91
4.4.70.0070.0176.91
4.4.60.0000.0376.91
4.4.50.0000.0306.91
4.4.40.0070.0506.91
4.4.30.0030.0306.91
4.4.20.0030.0306.91
4.4.10.0000.0376.91
4.4.00.0070.0406.91
4.3.110.0030.0176.91
4.3.100.0030.0276.91
4.3.90.0030.0336.91
4.3.80.0130.0376.91
4.3.70.0030.0276.91
4.3.60.0000.0206.91
4.3.50.0030.0336.91
4.3.40.0030.0506.91
4.3.30.0030.0336.91
4.3.20.0000.0306.91
4.3.10.0030.0236.91
4.3.00.0000.0236.91

preferences:
88.05 ms | 401 KiB | 5 Q