3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @file * Contains \Drupal\Component\Utility\Crypt. */ namespace Drupal\Component\Utility { /** * Utility class for cryptographically-secure string handling routines. */ class Crypt { /** * Returns a string of highly randomized bytes (over the full 8-bit range). * * This function is better than simply calling mt_rand() or any other built-in * PHP function because it can return a long string of bytes (compared to < 4 * bytes normally from mt_rand()) and uses the best available pseudo-random * source. * * @param int $count * The number of characters (bytes) to return in the string. * * @return string * A randomly generated string. */ public static function randomBytes($count) { static $random_state, $bytes; // Initialize on the first call. The contents of $_SERVER includes a mix of // user-specific and system information that varies a little with each page. // Further initialize with the somewhat random PHP process ID. if (!isset($random_state)) { $random_state = print_r($_SERVER, TRUE) . getmypid(); $bytes = ''; } if (strlen($bytes) < $count) { // /dev/urandom is available on many *nix systems and is considered the // best commonly available pseudo-random source. if ($fh = @fopen('/dev/urandom', 'rb')) { // PHP only performs buffered reads, so in reality it will always read // at least 4096 bytes. Thus, it costs nothing extra to read and store // that much so as to speed any additional invocations. $bytes .= fread($fh, max(4096, $count)); fclose($fh); } // openssl_random_pseudo_bytes() will find entropy in a system-dependent // way. elseif (function_exists('openssl_random_pseudo_bytes')) { $bytes .= openssl_random_pseudo_bytes($count - strlen($bytes)); } // If /dev/urandom is not available or returns no bytes, this loop will // generate a good set of pseudo-random bytes on any system. // Note that it may be important that our $random_state is passed // through hash() prior to being rolled into $output, that the two hash() // invocations are different, and that the extra input into the first one - // the microtime() - is prepended rather than appended. This is to avoid // directly leaking $random_state via the $output stream, which could // allow for trivial prediction of further "random" numbers. while (strlen($bytes) < $count) { $random_state = hash('sha256', microtime() . mt_rand() . $random_state); $bytes .= hash('sha256', mt_rand() . $random_state, TRUE); } } $output = substr($bytes, 0, $count); $bytes = substr($bytes, $count); return $output; } /** * Calculates a base-64 encoded, URL-safe sha-256 hmac. * * @param mixed $data * Scalar value to be validated with the hmac. * @param mixed $key * A secret key, this can be any scalar value. * * @return string * A base-64 encoded sha-256 hmac, with + replaced with -, / with _ and * any = padding characters removed. */ public static function hmacBase64($data, $key) { // $data and $key being strings here is necessary to avoid empty string // results of the hash function if they are not scalar values. As this // function is used in security-critical contexts like token validation it // is important that it never returns an empty string. if (!is_scalar($data) || !is_scalar($key)) { throw new \InvalidArgumentException('Both parameters passed to \Drupal\Component\Utility\Crypt::hmacBase64 must be scalar values.'); } $hmac = base64_encode(hash_hmac('sha256', $data, $key, TRUE)); // Modify the hmac so it's safe to use in URLs. return strtr($hmac, array('+' => '-', '/' => '_', '=' => '')); } /** * Calculates a base-64 encoded, URL-safe sha-256 hash. * * @param string $data * String to be hashed. * * @return string * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and * any = padding characters removed. */ public static function hashBase64($data) { $hash = base64_encode(hash('sha256', $data, TRUE)); // Modify the hash so it's safe to use in URLs. return strtr($hash, array('+' => '-', '/' => '_', '=' => '')); } /** * Generates a random, base-64 encoded, URL-safe, sha-256 hashed string. * * @param int $count * The number of characters (bytes) of the string to be hashed. * * @return string * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and * any = padding characters removed. * * @see \Drupal\Component\Utility\Crypt::randomBytes() * @see \Drupal\Component\Utility\Crypt::hashBase64() */ public static function randomStringHashed($count) { return static::hashBase64(static::randomBytes($count)); } } } namespace { var_dump(\Drupal\Component\Utility\Crypt::randomBytes(8)); }

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.0.110.0040.00416.98
8.0.100.0030.00616.99
8.0.90.0070.00017.17
8.0.80.0100.01017.04
8.0.70.0030.00616.97
8.0.60.0030.00517.14
8.0.50.0040.00417.09
8.0.30.0070.01217.32
8.0.20.0120.00817.40
8.0.10.0000.00817.07
8.0.00.0100.01216.78
7.4.240.0000.00716.62
7.4.230.0030.00316.44
7.4.220.0060.02016.70
7.4.210.0060.01016.66
7.4.200.0040.00416.51
7.4.190.0000.00716.82
7.4.160.0160.00016.63
7.4.150.0070.01117.40
7.4.140.0090.01317.86
7.4.130.0070.01116.63
7.4.120.0070.01016.63
7.4.110.0110.01116.46
7.4.100.0100.00716.77
7.4.90.0090.01116.50
7.4.80.0100.01019.39
7.4.70.0130.00916.61
7.4.60.0110.00616.57
7.4.50.0030.00616.64
7.4.40.0030.01222.77
7.4.30.0110.00516.79
7.4.00.0100.00615.16
7.3.300.0000.00716.59
7.3.290.0090.00616.47
7.3.280.0090.00916.52
7.3.270.0090.00917.40
7.3.260.0070.01116.75
7.3.250.0150.00516.49
7.3.240.0070.01016.63
7.3.230.0000.01716.64
7.3.210.0150.00316.73
7.3.200.0190.00319.39
7.3.190.0060.01316.51
7.3.180.0120.00316.52
7.3.170.0080.00916.40
7.3.160.0080.00916.58
7.3.120.0090.00614.73
7.3.10.0030.01016.73
7.3.00.0030.00616.50
7.2.330.0070.01016.51
7.2.320.0140.00616.93
7.2.310.0060.01216.84
7.2.300.0090.01216.79
7.2.290.0100.00716.64
7.2.130.0070.00716.77
7.2.120.0070.00716.88
7.2.110.0070.01017.04
7.2.100.0060.00916.83
7.2.90.0100.00717.04
7.2.80.0070.00916.75
7.2.70.0040.01216.66
7.2.60.0080.00716.86
7.2.50.0140.00017.18
7.2.40.0040.00717.13
7.2.30.0030.01017.04
7.2.20.0070.00716.90
7.2.10.0110.00617.04
7.2.00.0060.00618.09
7.1.250.0090.00315.73
7.1.200.0080.00515.84
7.1.100.0000.01118.16
7.1.70.0000.00817.27
7.1.60.0030.01719.32
7.1.50.0090.01217.01
7.1.00.0030.06322.48
7.0.200.0230.00816.22
7.0.140.0130.06722.07
7.0.100.0600.07319.98
7.0.90.0570.08019.98
7.0.80.0570.07019.96
7.0.70.0530.08320.02
7.0.60.0530.07019.98
7.0.50.0500.07020.23
7.0.40.0070.05320.07
7.0.30.0130.08320.09
7.0.20.0170.07720.14
7.0.10.0070.08720.10
7.0.00.0100.04719.99
5.6.280.0030.07321.13
5.6.250.0270.04720.63
5.6.240.0100.07320.72
5.6.230.0070.06320.69
5.6.220.0030.08320.56
5.6.210.0200.06720.78
5.6.200.0070.08021.08
5.6.190.0130.07721.14
5.6.180.0000.06021.13
5.6.170.0100.06721.05
5.6.160.0030.08321.13
5.6.150.0170.06720.99
5.6.140.0100.07721.05
5.6.130.0130.07720.99
5.6.120.0030.06321.09
5.6.110.0100.08021.05
5.6.100.0070.07721.14
5.6.90.0100.08721.12
5.6.80.0070.08320.56
5.6.70.0130.06720.44
5.6.60.0070.08320.46
5.6.50.0100.07720.52
5.6.40.0100.08020.43
5.6.30.0200.06720.46
5.6.20.0130.07020.34
5.6.10.0170.07720.43
5.6.00.0070.07720.32
5.5.380.0170.07320.37
5.5.370.0130.07720.39
5.5.360.0100.07720.52
5.5.350.0170.07720.36
5.5.340.0070.08020.95
5.5.330.0130.06720.86
5.5.320.0070.08020.91
5.5.310.0130.07020.92
5.5.300.0130.07720.95
5.5.290.0100.05020.89
5.5.280.0130.07320.87
5.5.270.0000.09320.75
5.5.260.0100.07020.75
5.5.250.0070.08320.48
5.5.240.0070.08320.22
5.5.230.0200.06720.32
5.5.220.0070.08020.24
5.5.210.0100.07320.17
5.5.200.0100.07720.18
5.5.190.0100.08320.11
5.5.180.0070.07020.11
5.5.160.0100.07320.14
5.5.150.0070.07020.20
5.5.140.0070.08320.15
5.5.130.0170.07020.14
5.5.120.0130.07720.30
5.5.110.0100.07320.15
5.5.100.0100.07720.16
5.5.90.0100.07720.20
5.5.80.0000.08020.16
5.5.70.0130.07320.08
5.5.60.0170.07320.08
5.5.50.0200.07020.10
5.5.40.0070.08020.02
5.5.30.0070.07720.03
5.5.20.0070.08320.18
5.5.10.0130.07320.05
5.5.00.0130.07320.00
5.4.450.0130.07019.37
5.4.440.0100.07019.36
5.4.430.0100.07319.43
5.4.420.0070.05319.43
5.4.410.0200.06019.09
5.4.400.0070.06718.95
5.4.390.0070.05019.15
5.4.380.0000.07319.15
5.4.370.0170.06019.13
5.4.360.0070.07718.89
5.4.350.0070.07319.10
5.4.340.0130.06718.92
5.4.320.0170.07318.89
5.4.310.0070.07319.13
5.4.300.0070.07319.15
5.4.290.0030.08019.09
5.4.280.0070.08319.21
5.4.270.0030.07019.13
5.4.260.0100.07318.86
5.4.250.0130.07019.09
5.4.240.0070.07019.03
5.4.230.0030.08019.10
5.4.220.0100.08019.09
5.4.210.0130.07018.91
5.4.200.0100.07019.09
5.4.190.0070.07719.20
5.4.180.0130.07019.14
5.4.170.0130.07018.85
5.4.160.0100.07019.03
5.4.150.0070.08319.03
5.4.140.0100.07316.47
5.4.130.0100.07316.45
5.4.120.0100.06016.43
5.4.110.0070.07316.53
5.4.100.0070.06716.42
5.4.90.0100.07316.46
5.4.80.0130.06716.45
5.4.70.0130.06316.44
5.4.60.0030.07316.34
5.4.50.0070.07016.28
5.4.40.0130.07016.47
5.4.30.0070.07316.50
5.4.20.0100.06316.28
5.4.10.0100.07016.44
5.4.00.0070.07015.88
5.3.290.0170.06014.73
5.3.280.0100.07314.71
5.3.270.0000.05714.64
5.3.260.0100.07714.72
5.3.250.0030.07714.72
5.3.240.0170.06314.59
5.3.230.0130.06314.66
5.3.220.0100.07314.60
5.3.210.0130.07014.65
5.3.200.0030.07314.55
5.3.190.0030.08314.54
5.3.180.0200.05714.59
5.3.170.0070.07314.57
5.3.160.0100.07314.72
5.3.150.0030.07714.64
5.3.140.0070.04714.71
5.3.130.0130.06714.65
5.3.120.0070.07314.70
5.3.110.0070.08014.73
5.3.100.0000.08314.11
5.3.90.0070.08014.01
5.3.80.0030.07313.99
5.3.70.0100.06714.07
5.3.60.0000.08314.03
5.3.50.0030.08013.91
5.3.40.0100.07014.04
5.3.30.0130.06314.06
5.3.20.0030.07713.72
5.3.10.0070.07013.82
5.3.00.0030.06713.75
5.2.170.0030.06312.13
5.2.160.0100.05712.13
5.2.150.0100.06012.13
5.2.140.0070.05712.13
5.2.130.0070.06312.13
5.2.120.0000.04012.13
5.2.110.0070.06312.13
5.2.100.0100.05312.13
5.2.90.0070.05312.13
5.2.80.0170.03312.13
5.2.70.0100.05712.13
5.2.60.0000.06312.13
5.2.50.0070.06312.13
5.2.40.0000.03712.13
5.2.30.0130.04712.13
5.2.20.0030.06012.13
5.2.10.0070.04312.13
5.2.00.0030.06012.13
5.1.60.0030.03012.13
5.1.50.0030.05712.13
5.1.40.0030.05012.13
5.1.30.0030.05712.13
5.1.20.0070.05012.13
5.1.10.0030.05712.13
5.1.00.0070.05312.13
5.0.50.0030.03312.13
5.0.40.0100.03712.13
5.0.30.0030.05712.13
5.0.20.0000.04712.13
5.0.10.0100.03712.13
5.0.00.0100.05312.13
4.4.90.0030.03012.13
4.4.80.0000.02712.13
4.4.70.0000.02012.13
4.4.60.0000.03312.13
4.4.50.0030.03312.13
4.4.40.0000.05712.13
4.4.30.0030.03012.13
4.4.20.0030.03312.13
4.4.10.0100.02712.13
4.4.00.0000.05312.13
4.3.110.0070.03012.13
4.3.100.0000.03712.13
4.3.90.0000.03012.13
4.3.80.0030.05312.13
4.3.70.0000.03312.13
4.3.60.0070.03312.13
4.3.50.0030.02712.13
4.3.40.0030.03712.13
4.3.30.0000.03712.13
4.3.20.0000.02012.13
4.3.10.0000.03712.13
4.3.00.0000.03312.13

preferences:
33.82 ms | 400 KiB | 5 Q