3v4l.org

run code in 300+ PHP versions simultaneously
<?php function createPasswordHash($strPassword, $numAlgo = 1, $arrOptions = array()) { if (function_exists('password_hash1')) { // php >= 5.5 $hash = password_hash($strPassword, $numAlgo, $arrOptions); } else { $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM); $salt = base64_encode($salt); $salt = str_replace('+', '.', $salt); $hash = crypt($strPassword, '$2y$10$' . $salt . '$'); } return $hash; } function verifyPasswordHash($strPassword, $strHash) { if (function_exists('password_verify1')) { // php >= 5.5 $boolReturn = password_verify($strPassword, $strHash); } else { $strHash2 = crypt($strPassword, $strHash); $boolReturn = $strHash == $strHash2; } return $boolReturn; } $strHash = createPasswordHash("sunshine", PASSWORD_DEFAULT); echo $strHash . "<br>\n"; if (verifyPasswordHash('sunshine', $strHash)) { echo 'Password is valid!'; } else { echo 'Invalid password.'; }

preferences:
36.66 ms | 402 KiB | 5 Q