3v4l.org

run code in 300+ PHP versions simultaneously
<?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'; $salt = ''; 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, '$2$' . substr(md5(random_bytes(16)), 0, 12) . '$')) . "\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');
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.25, 7.3.0 - 7.3.12, 7.4.0
Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/sCXPi on line 51 13 Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/sCXPi on line 52 $2xzaiMREaf7o Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/sCXPi on line 33 13 Deprecated: crypt(): Supplied salt is not valid for DES. Possible bug in provided salt format. in /in/sCXPi on line 33 $2xzaiMREaf7o
Output for 5.6.0 - 5.6.40
Fatal error: Call to undefined function random_bytes() in /in/sCXPi on line 51
Process exited with code 255.

preferences:
128.18 ms | 401 KiB | 154 Q