Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Generates and returns a random password, of a random length between min and max.
- *
- * Hard limits are minimum 10 chars and maximum 72.
- *
- * @author Christian Fagerheim (Fagerheim Software)
- * @link www.fagsoft.no
- * @license Creative Commons Attribution-ShareAlike 3.0. http://creativecommons.org/licenses/by-sa/3.0/.
- *
- * @param int[optional] $minLen = 10
- * @param int[optional] $maxLen = 14
- * @return string
- */
- function generatePassword ($minLen = 10, $maxLen = 14) {
- if ($minLen < 10) {
- $minLen = 10;
- }
- // Discard everything above 72 characters for the password (bcrypt limitation).
- if ($maxLen > 72) {
- $maxLen = 72;
- }
- $numChars = mt_rand ($minLen, $maxLen);
- // Create an secure random password, and cut it down to length.
- $password = base64_encode (mcrypt_create_iv (256, MCRYPT_DEV_URANDOM));
- $password = substr ($password, 0, $numChars);
- // Define the replacements sets and values for strtr ().
- $find = "10lIO";
- $replace = "_-*!?";
- // Replace the similar-looking characters with special characters.
- $password = strtr ($find, $replace, $password);
- // Save the hashed password in the object, and return it to calling method.
- return $password;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement