3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * A Compatibility library with PHP 5.5's simplified password hashing API. * * @author Anthony Ferrara <ircmaxell@php.net> * @license http://www.opensource.org/licenses/mit-license.html MIT License * @copyright 2012 The Authors */ if (!defined('PASSWORD_DEFAULT')) { define('PASSWORD_BCRYPT', 1); define('PASSWORD_DEFAULT', PASSWORD_BCRYPT); /** * Hash the password using the specified algorithm * * @param string $password The password to hash * @param int $algo The algorithm to use (Defined by PASSWORD_* constants) * @param array $options The options for the algorithm to use * * @return string|false The hashed password, or false on error. */ function password_hash($password, $algo, array $options = array()) { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_hash to function", E_USER_WARNING); return null; } if (!is_string($password)) { trigger_error("password_hash(): Password must be a string", E_USER_WARNING); return null; } if (!is_int($algo)) { trigger_error("password_hash() expects parameter 2 to be long, " . gettype($algo) . " given", E_USER_WARNING); return null; } switch ($algo) { case PASSWORD_BCRYPT: // Note that this is a C constant, but not exposed to PHP, so we don't define it here. $cost = 10; if (isset($options['cost'])) { $cost = $options['cost']; if ($cost < 4 || $cost > 31) { trigger_error(sprintf("password_hash(): Invalid bcrypt cost parameter specified: %d", $cost), E_USER_WARNING); return null; } } // The length of salt to generate $raw_salt_len = 16; // The length required in the final serialization $required_salt_len = 22; $hash_format = sprintf("$2y$%02d$", $cost); break; default: trigger_error(sprintf("password_hash(): Unknown password hashing algorithm: %s", $algo), E_USER_WARNING); return null; } if (isset($options['salt'])) { switch (gettype($options['salt'])) { case 'NULL': case 'boolean': case 'integer': case 'double': case 'string': $salt = (string) $options['salt']; break; case 'object': if (method_exists($options['salt'], '__tostring')) { $salt = (string) $options['salt']; break; } case 'array': case 'resource': default: trigger_error('password_hash(): Non-string salt parameter supplied', E_USER_WARNING); return null; } if (strlen($salt) < $required_salt_len) { trigger_error(sprintf("password_hash(): Provided salt is too short: %d expecting %d", strlen($salt), $required_salt_len), E_USER_WARNING); return null; } elseif (0 == preg_match('#^[a-zA-Z0-9./]+$#D', $salt)) { $salt = str_replace('+', '.', base64_encode($salt)); } } else { $buffer = ''; $buffer_valid = false; if (function_exists('mcrypt_create_iv') && !defined('PHALANGER')) { $buffer = mcrypt_create_iv($raw_salt_len, MCRYPT_DEV_URANDOM); if ($buffer) { $buffer_valid = true; } } if (!$buffer_valid && function_exists('openssl_random_pseudo_bytes')) { $buffer = openssl_random_pseudo_bytes($raw_salt_len); if ($buffer) { $buffer_valid = true; } } if (!$buffer_valid && is_readable('/dev/urandom')) { $f = fopen('/dev/urandom', 'r'); $read = strlen($buffer); while ($read < $raw_salt_len) { $buffer .= fread($f, $raw_salt_len - $read); $read = strlen($buffer); } fclose($f); if ($read >= $raw_salt_len) { $buffer_valid = true; } } if (!$buffer_valid || strlen($buffer) < $raw_salt_len) { $bl = strlen($buffer); for ($i = 0; $i < $raw_salt_len; $i++) { if ($i < $bl) { $buffer[$i] = $buffer[$i] ^ chr(mt_rand(0, 255)); } else { $buffer .= chr(mt_rand(0, 255)); } } } $salt = str_replace('+', '.', base64_encode($buffer)); } $salt = substr($salt, 0, $required_salt_len); $hash = $hash_format . $salt; $ret = crypt($password, $hash); if (!is_string($ret) || strlen($ret) <= 13) { return false; } return $ret; } /** * Get information about the password hash. Returns an array of the information * that was used to generate the password hash. * * array( * 'algo' => 1, * 'algoName' => 'bcrypt', * 'options' => array( * 'cost' => 10, * ), * ) * * @param string $hash The password hash to extract info from * * @return array The array of information about the hash. */ function password_get_info($hash) { $return = array( 'algo' => 0, 'algoName' => 'unknown', 'options' => array(), ); if (substr($hash, 0, 4) == '$2y$' && strlen($hash) == 60) { $return['algo'] = PASSWORD_BCRYPT; $return['algoName'] = 'bcrypt'; list($cost) = sscanf($hash, "$2y$%d$"); $return['options']['cost'] = $cost; } return $return; } /** * Determine if the password hash needs to be rehashed according to the options provided * * If the answer is true, after validating the password using password_verify, rehash it. * * @param string $hash The hash to test * @param int $algo The algorithm used for new password hashes * @param array $options The options array passed to password_hash * * @return boolean True if the password needs to be rehashed. */ function password_needs_rehash($hash, $algo, array $options = array()) { $info = password_get_info($hash); if ($info['algo'] != $algo) { return true; } switch ($algo) { case PASSWORD_BCRYPT: $cost = isset($options['cost']) ? $options['cost'] : 10; if ($cost != $info['options']['cost']) { return true; } break; } return false; } /** * Verify a password against a hash using a timing attack resistant approach * * @param string $password The password to verify * @param string $hash The hash to verify against * * @return boolean If the password matches the hash */ function password_verify($password, $hash) { if (!function_exists('crypt')) { trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING); return false; } $ret = crypt($password, $hash); if (!is_string($ret) || strlen($ret) != strlen($hash) || strlen($ret) <= 13) { return false; } $status = 0; for ($i = 0; $i < strlen($ret); $i++) { $status |= (ord($ret[$i]) ^ ord($hash[$i])); } return $status === 0; } } echo password_hash('Admin',PASSWORD_DEFAULT); if(password_verify('Admin', password_hash('Admin',PASSWORD_DEFAULT))){ echo "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)
7.2.00.0030.19119.34
7.1.70.0030.18917.09
7.1.60.0130.26119.17
7.1.50.0070.26716.89
7.1.00.0070.25722.54
7.0.200.0400.18414.76
7.0.140.0030.28322.09
7.0.100.0170.27720.13
7.0.90.0070.28020.12
7.0.80.0270.20720.04
7.0.70.0000.19720.10
7.0.60.0100.19720.11
7.0.50.0070.19320.39
7.0.40.0000.23720.09
7.0.30.0000.19020.08
7.0.20.0030.21319.98
7.0.10.0100.19720.07
7.0.00.0070.28319.93
5.6.280.0100.28320.96
5.6.250.0030.27720.80
5.6.240.0070.26320.63
5.6.230.0170.24020.67
5.6.220.0070.25020.56
5.6.210.0000.22720.72
5.6.200.0030.28321.08
5.6.190.0000.20021.14
5.6.180.0100.22021.18
5.6.170.0030.19021.07
5.6.160.0200.26721.08
5.6.150.0130.28321.05
5.6.140.0270.25021.11
5.6.130.0030.26021.05
5.6.120.0170.27320.97
5.6.110.0070.29320.96
5.6.100.0030.27021.07
5.6.90.0130.26321.06
5.6.80.0100.26020.59
5.6.70.0030.22320.54
5.6.60.0170.28320.54
5.6.50.0070.28720.50
5.6.40.0070.20720.41
5.6.30.0070.24720.47
5.6.20.0030.28720.43
5.6.10.0100.29320.38
5.6.00.0100.20720.45
5.5.380.0070.28020.44
5.5.370.0070.26720.61
5.5.360.0070.28320.45
5.5.350.0070.23720.36
5.5.340.0030.19020.96
5.5.330.0100.22720.84
5.5.320.0070.18320.94
5.5.310.0100.18020.92
5.5.300.0170.25720.84
5.5.290.0130.27320.87
5.5.280.0070.27020.91
5.5.270.0070.26720.79
5.5.260.0130.26720.88
5.5.250.0100.27720.63
5.5.240.0100.22320.25
5.5.230.0170.26020.30
5.5.220.0030.19320.24
5.5.210.0130.28320.27
5.5.200.0100.25020.19
5.5.190.0030.23320.24
5.5.180.0130.27320.16
5.5.160.0070.19020.25
5.5.150.0100.27720.29
5.5.140.0070.27020.24
5.5.130.0130.23320.21
5.5.120.0000.30020.15
5.5.110.0170.26720.23
5.5.100.0070.27020.20
5.5.90.0130.25320.10
5.5.80.0100.29720.04
5.5.70.0070.21020.19
5.5.60.0070.27320.09
5.5.50.0070.19319.99
5.5.40.0100.27720.18
5.5.30.0130.27320.18
5.5.20.0000.30019.99
5.5.10.0030.28020.01
5.5.00.0070.26020.14
5.4.450.0100.23319.18
5.4.440.0070.26719.20
5.4.430.0200.30019.48
5.4.420.0070.27319.28
5.4.410.0130.26719.40
5.4.400.0030.21318.88
5.4.390.0100.26719.13
5.4.380.0000.29718.88
5.4.370.0000.27019.13
5.4.360.0100.28318.90
5.4.350.0170.18018.87
5.4.340.0000.22719.04
5.4.320.0070.24019.23
5.4.310.0070.28719.03
5.4.300.0070.30018.84
5.4.290.0100.29719.18
5.4.280.0000.26019.14
5.4.270.0070.27019.23
5.4.260.0070.27019.11
5.4.250.0100.28319.11
5.4.240.0030.27319.20
5.4.230.0070.27019.20
5.4.220.0070.26319.20
5.4.210.0070.25019.18
5.4.200.0100.26018.84
5.4.190.0100.27319.21
5.4.180.0030.27018.89
5.4.170.0100.30318.89
5.4.160.0070.27318.83
5.4.150.0030.27019.03
5.4.140.0070.23016.45
5.4.130.0030.27016.30
5.4.120.0030.22016.27
5.4.110.0130.26316.54
5.4.100.0100.26716.56
5.4.90.0130.26716.48
5.4.80.0070.18716.43
5.4.70.0030.29316.46
5.4.60.0030.27716.35
5.4.50.0070.24316.45
5.4.40.0070.25716.50
5.4.30.0070.28016.46
5.4.20.0100.25316.47
5.4.10.0030.23716.29
5.4.00.0070.28015.94
5.3.290.0000.21314.66
5.3.280.0200.26314.59
5.3.270.0070.26714.64
5.3.260.0130.23714.60
5.3.250.0130.22714.68
5.3.240.0100.24714.76
5.3.230.0170.25314.73
5.3.220.0070.28314.75
5.3.210.0070.20314.66
5.3.200.0000.26714.77
5.3.190.0030.26314.72
5.3.180.0100.25714.69
5.3.170.0000.27314.68
5.3.160.0030.27314.73
5.3.150.0070.22714.64
5.3.140.0070.27314.68
5.3.130.0030.25714.68
5.3.120.0100.26714.70
5.3.110.0130.28314.67
5.3.100.0170.28014.19
5.3.90.0200.27314.14
5.3.80.0070.29014.22
5.3.70.0100.25314.05
5.3.60.0030.08014.18
5.3.50.0100.06714.19
5.3.40.0030.06713.98
5.3.30.0070.03713.96
5.3.20.0030.04313.93
5.3.10.0000.04313.91
5.3.00.0000.06313.91

preferences:
34.56 ms | 400 KiB | 5 Q