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(base64_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.60.0140.00718.56
8.3.50.0100.01222.11
8.3.40.0040.01118.79
8.3.30.0140.00018.97
8.3.20.0080.00020.16
8.3.10.0040.00421.74
8.3.00.0000.00819.14
8.2.180.0120.00318.43
8.2.170.0090.01222.96
8.2.160.0070.00722.12
8.2.150.0060.00324.18
8.2.140.0000.00924.66
8.2.130.0040.00426.16
8.2.120.0070.00019.79
8.2.110.0030.00622.14
8.2.100.0070.00417.96
8.2.90.0030.00619.29
8.2.80.0000.00817.97
8.2.70.0040.00417.75
8.2.60.0030.00718.16
8.2.50.0060.00318.07
8.2.40.0030.00519.88
8.2.30.0040.00418.10
8.2.20.0040.00417.69
8.2.10.0090.00018.01
8.2.00.0000.00917.74
8.1.280.0090.00625.92
8.1.270.0040.00424.66
8.1.260.0050.00326.35
8.1.250.0030.00528.09
8.1.240.0060.00323.72
8.1.230.0040.00719.09
8.1.220.0040.00417.79
8.1.210.0000.00818.77
8.1.200.0060.00617.48
8.1.190.0050.00317.35
8.1.180.0060.00318.10
8.1.170.0030.00518.45
8.1.160.0000.00722.02
8.1.150.0000.00718.77
8.1.140.0000.00819.17
8.1.130.0070.00017.76
8.1.120.0000.00717.36
8.1.110.0070.00017.37
8.1.100.0000.00817.42
8.1.90.0000.00717.44
8.1.80.0050.00317.32
8.1.70.0000.00717.47
8.1.60.0060.00317.55
8.1.50.0060.00317.59
8.1.40.0040.00417.48
8.1.30.0040.00417.63
8.1.20.0000.00817.57
8.1.10.0050.00317.63
8.1.00.0040.00417.49
8.0.300.0040.00418.77
8.0.290.0000.00816.88
8.0.280.0040.00418.39
8.0.270.0050.00317.30
8.0.260.0030.00617.25
8.0.250.0000.00717.10
8.0.240.0070.00017.02
8.0.230.0030.00316.95
8.0.220.0040.00416.97
8.0.210.0040.00417.02
8.0.200.0000.00817.00
8.0.190.0060.00316.95
8.0.180.0080.00016.89
8.0.170.0000.00716.89
8.0.160.0000.00716.99
8.0.150.0070.00316.86
8.0.140.0000.00816.94
8.0.130.0060.00013.34
8.0.120.0040.00416.85
8.0.110.0050.00216.80
8.0.100.0000.00716.89
8.0.90.0000.00816.97
8.0.80.0000.01516.92
8.0.70.0000.00716.89
8.0.60.0040.00416.84
8.0.50.0040.00416.99
8.0.30.0110.01116.93
8.0.20.0110.00817.40
8.0.10.0000.00717.16
8.0.00.0100.00716.89
7.4.330.0000.00515.03
7.4.320.0000.00616.72
7.4.300.0080.00016.55
7.4.290.0000.00816.57
7.4.280.0060.00316.46
7.4.270.0000.00716.58
7.4.260.0050.00216.57
7.4.250.0040.00416.58
7.4.240.0020.00616.62
7.4.230.0050.00216.64
7.4.220.0140.00416.72
7.4.210.0070.00716.67
7.4.200.0050.00316.38
7.4.160.0120.00316.76
7.4.150.0040.01617.40
7.4.140.0120.00917.86
7.4.130.0110.00616.55
7.4.120.0090.00916.75
7.4.110.0120.00916.71
7.4.100.0150.00316.66
7.4.90.0090.00916.49
7.4.80.0090.01519.39
7.4.70.0120.00816.70
7.4.60.0130.00316.56
7.4.50.0000.00516.61
7.4.40.0140.00316.48
7.4.30.0060.01216.63
7.4.10.0130.00714.91
7.4.00.0070.00915.03
7.3.330.0020.00613.24
7.3.320.0030.00213.29
7.3.310.0000.00716.23
7.3.300.0000.00716.33
7.3.290.0020.01216.38
7.3.280.0090.01116.40
7.3.270.0120.00617.40
7.3.260.0100.01016.38
7.3.250.0120.00816.42
7.3.240.0110.00816.34
7.3.230.0030.01416.54
7.3.210.0120.00616.54
7.3.200.0030.01419.39
7.3.190.0140.00216.33
7.3.180.0130.00316.46
7.3.170.0090.00916.40
7.3.160.0060.01616.58
7.3.130.0060.01314.89
7.3.120.0140.00414.70
7.3.110.0000.01914.86
7.3.100.0040.01114.91
7.3.90.0060.01014.93
7.3.80.0060.00315.05
7.3.70.0070.00314.72
7.3.60.0070.00715.08
7.3.50.0060.00914.94
7.3.40.0000.00914.68
7.3.30.0000.01114.83
7.3.20.0060.01016.62
7.3.10.0000.01516.43
7.3.00.0040.00816.64
7.2.330.0110.00716.82
7.2.320.0160.00316.48
7.2.310.0090.00916.53
7.2.300.0120.00416.70
7.2.290.0060.01216.50
7.2.260.0120.00615.40
7.2.250.0090.00915.02
7.2.240.0000.01614.85
7.2.230.0030.00715.15
7.2.220.0110.00415.30
7.2.210.0030.00915.11
7.2.200.0040.00814.93
7.2.190.0040.01115.01
7.2.180.0000.00915.23
7.2.170.0070.00715.20
7.2.160.0100.00715.29
7.2.150.0070.00717.16
7.2.140.0100.01016.82
7.2.130.0130.00316.93
7.2.120.0070.00716.97
7.2.110.0040.01216.75
7.2.100.0080.00417.16
7.2.90.0070.00717.06
7.2.80.0100.00017.08
7.2.70.0060.00616.98
7.2.60.0050.01117.00
7.2.50.0110.00416.76
7.2.40.0030.01217.00
7.2.30.0070.01116.96
7.2.20.0040.00416.93
7.2.10.0060.00617.07
7.2.00.0060.00518.17
7.1.330.0040.01215.86
7.1.320.0070.00715.82
7.1.310.0040.00816.04
7.1.300.0070.01015.83
7.1.290.0060.01015.89
7.1.280.0070.00715.89
7.1.270.0090.00915.70
7.1.260.0000.01815.61
7.1.250.0000.01015.66
7.1.240.0110.00016.02
7.1.230.0130.00015.76
7.1.220.0030.01015.57
7.1.210.0000.01415.76
7.1.200.0010.01115.74
7.1.190.0060.00615.96
7.1.180.0120.00015.79
7.1.170.0040.01115.98
7.1.160.0070.00315.81
7.1.150.0030.00716.00
7.1.140.0070.00315.93
7.1.130.0070.00315.76
7.1.120.0030.00915.95
7.1.110.0030.01015.86
7.1.100.0050.00816.92
7.1.90.0060.00615.75
7.1.80.0000.01315.82
7.1.70.0050.00516.45
7.1.60.0070.01117.70
7.1.50.0120.00716.29
7.1.40.0080.00615.58
7.1.30.0070.00715.66
7.1.20.0060.00615.96
7.1.10.0000.01015.93
7.1.00.0050.04019.04
7.0.330.0040.00815.36
7.0.320.0040.00815.43
7.0.310.0030.00915.45
7.0.300.0100.00315.39
7.0.290.0030.00615.42
7.0.280.0100.00315.40
7.0.270.0040.01115.61
7.0.260.0060.00615.45
7.0.250.0060.00615.32
7.0.240.0100.00315.17
7.0.230.0060.00615.42
7.0.220.0000.00915.47
7.0.210.0030.01015.52
7.0.200.0090.00416.17
7.0.190.0070.00715.22
7.0.180.0000.01315.55
7.0.170.0070.00315.44
7.0.160.0030.00715.41
7.0.150.0100.00715.53
7.0.140.0050.02018.59
7.0.130.0070.00415.55
7.0.120.0030.01015.10
7.0.110.0050.02117.71
7.0.100.0040.02217.78
7.0.90.0090.02817.63
7.0.80.0000.03317.83
7.0.70.0070.02217.62
7.0.60.0100.01717.70
7.0.50.0050.02317.79
7.0.40.0050.02216.71
7.0.30.0050.02216.50
7.0.20.0060.02616.46
7.0.10.0070.02016.51
7.0.00.0030.02216.70
5.6.400.0070.00714.14
5.6.390.0040.00814.27
5.6.380.0130.00014.62
5.6.370.0040.00814.52
5.6.360.0030.01014.47
5.6.350.0100.00714.65
5.6.340.0050.00814.77
5.6.330.0040.00814.73
5.6.320.0070.00714.25
5.6.310.0030.00714.49
5.6.300.0080.00814.72
5.6.290.0100.00014.57
5.6.280.0060.04717.83
5.6.270.0080.00614.35
5.6.260.0050.02517.45
5.6.250.0190.01217.48
5.6.240.0120.02017.64
5.6.230.0020.02217.68
5.6.220.0040.02717.57
5.6.210.0050.02617.58
5.6.200.0100.02517.68
5.6.190.0050.02217.57
5.6.180.0080.02217.56
5.6.170.0050.02917.46
5.6.160.0050.02617.52
5.6.150.0090.01817.59
5.6.140.0050.02217.50
5.6.130.0050.02817.46
5.6.120.0050.02217.64
5.6.110.0040.02217.58
5.6.100.0050.02517.63
5.6.90.0090.02117.56
5.6.80.0060.02417.06
5.6.70.0070.01817.24
5.6.60.0020.02217.12
5.6.50.0050.02017.21
5.6.40.0080.01717.12
5.6.30.0030.02417.15
5.6.20.0010.02117.07
5.6.10.0030.02317.28
5.6.00.0100.01717.15
5.5.380.0080.02315.91
5.5.370.0030.02515.87
5.5.360.0040.02116.03
5.5.350.0060.01815.80
5.5.340.0050.02316.31
5.5.330.0050.02015.97
5.5.320.0080.02316.42
5.5.310.0040.02816.28
5.5.300.0050.02016.17
5.5.290.0030.02416.12
5.5.280.0080.01816.21
5.5.270.0040.02216.37
5.5.260.0080.02016.21
5.5.250.0060.02315.99
5.5.240.0050.02115.88
5.5.230.0070.02415.76
5.5.220.0060.02315.93
5.5.210.0030.02815.99
5.5.200.0090.01915.82
5.5.190.0060.02015.88
5.5.180.0020.02215.68
5.5.170.0030.01314.27
5.5.160.0020.02615.83
5.5.150.0110.01416.04
5.5.140.0050.01815.92
5.5.130.0030.02315.73
5.5.120.0080.01715.78
5.5.110.0020.02515.74
5.5.100.0050.02415.97
5.5.90.0030.02515.87
5.5.80.0030.02215.93
5.5.70.0030.02015.85
5.5.60.0030.02515.87
5.5.50.0050.02315.78
5.5.40.0060.02215.92
5.5.30.0050.02015.88
5.5.20.0040.02215.68
5.5.10.0020.02215.80
5.5.00.0030.02015.71
5.4.450.0090.02615.24
5.4.440.0050.02415.27
5.4.430.0060.01815.30
5.4.420.0070.02515.32
5.4.410.0050.01715.11
5.4.400.0050.02115.11
5.4.390.0000.02115.14
5.4.380.0050.02214.99
5.4.370.0040.02315.05
5.4.360.0030.02015.13
5.4.350.0030.01814.98
5.4.340.0020.02315.05
5.4.330.0040.00411.00
5.4.320.0070.01714.97
5.4.310.0050.02115.05
5.4.300.0050.01814.90
5.4.290.0000.02414.91
5.4.280.0000.02415.02
5.4.270.0020.02315.02
5.4.260.0030.02215.17
5.4.250.0000.02415.01
5.4.240.0020.02314.91
5.4.230.0070.01715.01
5.4.220.0050.02514.92
5.4.210.0040.02115.06
5.4.200.0000.02314.99
5.4.190.0070.02314.99
5.4.180.0020.02414.90
5.4.170.0080.01814.91
5.4.160.0060.01814.89
5.4.150.0060.01615.07
5.4.140.0090.01913.60
5.4.130.0050.01813.77
5.4.120.0020.02613.74
5.4.110.0040.02113.74
5.4.100.0050.01813.63
5.4.90.0020.02013.78
5.4.80.0050.01913.64
5.4.70.0050.01813.62
5.4.60.0020.02413.78
5.4.50.0020.02613.79
5.4.40.0070.01813.79
5.4.30.0040.02413.63
5.4.20.0030.02313.66
5.4.10.0030.02113.57
5.4.00.0040.02113.32
5.3.290.0000.04014.56
5.3.280.0000.04014.77
5.3.270.0070.03314.51
5.3.260.0130.03314.64
5.3.250.0000.03714.68
5.3.240.0070.04014.66
5.3.230.0000.03714.63
5.3.220.0100.03714.63
5.3.210.0030.03014.56
5.3.200.0070.02714.84
5.3.190.0030.03314.61
5.3.180.0070.04014.58
5.3.170.0070.03314.68
5.3.160.0100.03714.65
5.3.150.0000.04014.60
5.3.140.0070.02714.61
5.3.130.0030.03314.58
5.3.120.0000.04314.61
5.3.110.0100.03314.59
5.3.100.0030.04014.07
5.3.90.0100.03014.07
5.3.80.0030.03314.02
5.3.70.0070.02714.14
5.3.60.0070.02714.00
5.3.50.0030.03313.95
5.3.40.0070.02713.98
5.3.30.0100.02313.91
5.3.20.0030.03313.74
5.3.10.0100.03313.72
5.3.00.0030.03013.91
5.2.170.0070.02711.20
5.2.160.0030.03011.17
5.2.150.0030.03011.18
5.2.140.0100.02711.20
5.2.130.0000.02711.37
5.2.120.0000.03011.14
5.2.110.0000.03311.16
5.2.100.0030.03011.18
5.2.90.0030.02311.14
5.2.80.0030.03011.10
5.2.70.0030.02711.14
5.2.60.0000.03711.20
5.2.50.0000.02711.02
5.2.40.0070.02711.05
5.2.30.0000.02710.98
5.2.20.0000.03011.06
5.2.10.0000.02310.93
5.2.00.0000.02710.74
5.1.60.0030.02710.29
5.1.50.0000.02710.03
5.1.40.0000.02710.20
5.1.30.0070.02310.34
5.1.20.0030.02310.46
5.1.10.0070.01710.12
5.1.00.0030.02010.18
5.0.50.0030.0208.60
5.0.40.0030.0178.43
5.0.30.0070.0678.34
5.0.20.0070.0338.25
5.0.10.0000.0478.38
5.0.00.0000.0678.04
4.4.90.0000.0137.11
4.4.80.0000.0177.11
4.4.70.0000.0137.11
4.4.60.0000.0177.11
4.4.50.0030.0137.11
4.4.40.0000.0207.11
4.4.30.0070.0107.11
4.4.20.0000.0137.11
4.4.10.0000.0137.11
4.4.00.0030.0507.11
4.3.110.0000.0337.11
4.3.100.0070.0307.11
4.3.90.0000.0137.11
4.3.80.0000.0577.11
4.3.70.0000.0137.11
4.3.60.0000.0307.11
4.3.50.0000.0337.11
4.3.40.0100.0477.11
4.3.30.0000.0307.11
4.3.20.0000.0277.11
4.3.10.0000.0377.11
4.3.00.0000.0277.11

preferences:
49.97 ms | 401 KiB | 5 Q