Add method to create a password that includes all character classes

This commit is contained in:
Kijin Sung 2015-03-06 16:15:45 +09:00
parent 8da6ddcfe8
commit 8e0b310e2a

View file

@ -292,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