diff --git a/classes/security/Password.class.php b/classes/security/Password.class.php index 965185184..39407b97b 100644 --- a/classes/security/Password.class.php +++ b/classes/security/Password.class.php @@ -275,6 +275,15 @@ class Password { return substr(bin2hex($output), 0, $length); } + elseif($format === 'printable') + { + $salt = ''; + for($i = 0; $i < $length; $i++) + { + $salt .= chr(33 + (crc32(sha1($i . $output)) % 94)); + } + return $salt; + } else { $salt = substr(base64_encode($output), 0, $length); @@ -283,6 +292,30 @@ class Password } } + /** + * @brief Generate a temporary password using the secure salt generator + * @param int $length The number of bytes to return + * @return string + */ + public function createTemporaryPassword($length = 16) + { + while(true) + { + $source = $this->createSecureSalt(128, 'printable'); + $source = preg_replace('/[iIoOjl10\'"!?<>\(\)\{\}\[\]:;.,`\\\\]/', '', $source); + $source_length = strlen($source); + for($i = 0; $i < $source_length - $length; $i++) + { + $candidate = substr($source, $i, $length); + if(preg_match('/[a-z]/', $candidate) && preg_match('/[A-Z]/', $candidate) && + preg_match('/[0-9]/', $candidate) && preg_match('/[!a-zA-Z0-9]/', $candidate)) + { + return $candidate; + } + } + } + } + /** * @brief Generate the PBKDF2 hash of a string using a salt * @param string $password The password