<?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) . '$';
//$salt = '$2y$09$anexampl$';
}
else
{
// Fake the salt result (a randomly created salt using https://github.com/paragonie/random_compat )
// $salt = '$2$9936b047ea8b$';
$salt = '$2y$09$anexampl$';
}
echo 'salt length = ' . strlen($salt) . "\n";
echo strlen(crypt($plaintext, $salt)) . "\n";
echo crypt('U*U', '$2a$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW') . "\n";
echo crypt($plaintext, $salt) . "\n";
echo strlen(getCryptedPassword($plaintext, $salt, 'crypt-blowfish')) . "\n";
echo getCryptedPassword($plaintext, $salt, 'crypt-blowfish');
preferences:
34.09 ms | 410 KiB | 5 Q