Implement random password generation (create/edit account)
This commit is contained in:
parent
48925f283f
commit
2ccee2169b
9 changed files with 127 additions and 54 deletions
|
|
@ -24,4 +24,20 @@ class PasswordHelper
|
|||
$passwordHashInfo = password_get_info($passwordHash);
|
||||
return $passwordHashInfo['algoName'] ?? 'unknown';
|
||||
}
|
||||
|
||||
public function generateRandomPassword(int $length = 24): string
|
||||
{
|
||||
// We want a random string of alphanumeric characters. Simple way to do this is to get random bytes, convert them to base64,
|
||||
// strip all characters from it that aren't alphanumeric (+, /, =), and take the first $length characters from it.
|
||||
|
||||
// Get lots of random bytes (will be much more than we need, but this way we don't need to worry about the string being too short
|
||||
// after stripping the non-alphanumeric base64 characters).
|
||||
$randomBytes = random_bytes(2 * $length);
|
||||
|
||||
// Convert to base64 and strip non-alphanumeric characters
|
||||
$randomAlphaNumeric = str_replace(['+', '/', '='], '', base64_encode($randomBytes));
|
||||
|
||||
// Shorten string
|
||||
return substr($randomAlphaNumeric, 0, $length);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue