3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getCryptedPassword($plaintext, $salt = '', $encryption = 'crypt-blowfish', $show_encrypt = false) { // mimic the getSalt function so we can use it in the example. if ($salt) { $salt = substr(preg_replace('|^{crypt}|i', '', $salt), 0, 16); } else { if (function_exists('random_bytes')) { $salt = '$2$' . substr(md5(random_bytes(16)), 0, 12) . '$'; } else { // Fake the salt result (a previously generated salt using https://github.com/paragonie/random_compat ) $salt = '$2$9936b047ea8b$'; } } // Encrypt the password. switch ($encryption) { case 'plain': return $plaintext; case 'sha': $encrypted = base64_encode(mhash(MHASH_SHA1, $plaintext)); return ($show_encrypt) ? '{SHA}' . $encrypted : $encrypted; case 'crypt': case 'crypt-des': case 'crypt-md5': case 'crypt-blowfish': return ($show_encrypt ? '{crypt}' : '') . crypt($plaintext, $salt); case 'md5-hex': default: $encrypted = ($salt) ? md5($plaintext . $salt) : md5($plaintext); return ($show_encrypt) ? '{MD5}' . $encrypted : $encrypted; } } $plaintext = 'mySuperSecretPassword'; if (function_exists('random_bytes')) { $salt = '$2$' . substr(md5(random_bytes(16)), 0, 12) . '$'; } else { // Fake the salt result (a previously generated salt using https://github.com/paragonie/random_compat ) $salt = '$2$9936b047ea8b$'; } echo strlen(crypt($plaintext, $salt)) . "\n"; echo crypt($plaintext, '$2$' . substr(md5(random_bytes(16)), 0, 12) . '$') . "\n"; echo strlen(getCryptedPassword($plaintext, $salt, 'crypt-blowfish')) . "\n"; echo getCryptedPassword($plaintext, $salt, 'crypt-blowfish');
based on sCXPi
Output for 7.0.0 - 7.2.0
Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/40vi4 on line 52 13 Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/40vi4 on line 53 $2xzaiMREaf7o Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/40vi4 on line 33 13 Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/40vi4 on line 33 $2xzaiMREaf7o
Output for 5.6.0 - 5.6.26
13 Fatal error: Call to undefined function random_bytes() in /in/40vi4 on line 53
Process exited with code 255.