diff --git a/src/PasswordGenerator.php b/src/PasswordGenerator.php index 2ee39a8..605e1a9 100644 --- a/src/PasswordGenerator.php +++ b/src/PasswordGenerator.php @@ -11,20 +11,18 @@ class PasswordGenerator extends RandomGenerator implements PasswordGeneratorInte * one uppercase letter, one digit, and one special character. The remaining characters * in the password are chosen at random from those four sets. * - * The available characters in each set are user friendly - there are no ambiguous + * The available characters in each set are user-friendly - there are no ambiguous * characters such as i, l, 1, o, 0, etc. * - * @return string - * * @see https://gist.github.com/tylerhall/521810 */ - public function generatePassword(int $length = 12) + public function generatePassword(int $length = 16): string { $sets = []; $sets[] = 'abcdefghjkmnpqrstuvwxyz'; $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ'; $sets[] = '23456789'; - $sets[] = '!@#$%&*?'; + $sets[] = '!@#$%&*?-'; $all = ''; $password = ''; @@ -38,8 +36,6 @@ public function generatePassword(int $length = 12) $password .= $all[array_rand($all)]; } - $password = str_shuffle($password); - - return $password; + return str_shuffle($password); } } diff --git a/src/PasswordGeneratorInterface.php b/src/PasswordGeneratorInterface.php index 4fc48a8..88bbc53 100644 --- a/src/PasswordGeneratorInterface.php +++ b/src/PasswordGeneratorInterface.php @@ -6,8 +6,5 @@ interface PasswordGeneratorInterface { - /** - * @return string - */ - public function generatePassword(int $length = 12); + public function generatePassword(int $length = 16): string; } diff --git a/src/RandomGenerator.php b/src/RandomGenerator.php index 4913c0a..7257851 100644 --- a/src/RandomGenerator.php +++ b/src/RandomGenerator.php @@ -8,40 +8,22 @@ class RandomGenerator { - protected ?LoggerInterface $logger; - protected bool $useOpenSsl; - - public function __construct(?LoggerInterface $logger = null) + public function __construct(protected readonly LoggerInterface $logger) { - $this->logger = $logger; - // determine whether to use OpenSSL - if (defined('PHP_WINDOWS_VERSION_BUILD') && version_compare(PHP_VERSION, '5.3.4', '<')) { - $this->useOpenSsl = false; - } elseif (!function_exists('openssl_random_pseudo_bytes')) { - if (null !== $this->logger) { - $this->logger->notice('It is recommended that you enable the "openssl" extension for random number generation.'); - } - $this->useOpenSsl = false; - } else { - $this->useOpenSsl = true; + if (!function_exists('openssl_random_pseudo_bytes')) { + throw new \RuntimeException('You must enable the "openssl" extension for secure random number generation.'); } } public function getRandomNumber(int $nbBytes = 32): string { // try OpenSSL - if ($this->useOpenSsl) { - $bytes = \openssl_random_pseudo_bytes($nbBytes, $strong); - - if (false !== $bytes && true === $strong) { - return $bytes; - } + $bytes = \openssl_random_pseudo_bytes($nbBytes, $strong); - if (null !== $this->logger) { - $this->logger->info('OpenSSL did not produce a secure random number.'); - } + if (false !== $bytes && true === $strong) { + return $bytes; } - return hash('sha256', uniqid((string) mt_rand(), true), true); + throw new \RuntimeException('Unable to generate a secure random number.'); } } diff --git a/src/SaltGenerator.php b/src/SaltGenerator.php index 55dd79d..be00380 100644 --- a/src/SaltGenerator.php +++ b/src/SaltGenerator.php @@ -6,10 +6,7 @@ class SaltGenerator extends RandomGenerator implements SaltGeneratorInterface { - /** - * @return string - */ - public function generateSalt() + public function generateSalt(): string { return strtr(base64_encode($this->getRandomNumber(24)), '{}', '-_'); } diff --git a/src/SaltGeneratorInterface.php b/src/SaltGeneratorInterface.php index 52f3b97..b117ef2 100644 --- a/src/SaltGeneratorInterface.php +++ b/src/SaltGeneratorInterface.php @@ -6,8 +6,5 @@ interface SaltGeneratorInterface { - /** - * @return string - */ - public function generateSalt(); + public function generateSalt(): string; } diff --git a/src/TokenGenerator.php b/src/TokenGenerator.php index 98f51e4..8535004 100644 --- a/src/TokenGenerator.php +++ b/src/TokenGenerator.php @@ -6,10 +6,7 @@ class TokenGenerator extends RandomGenerator implements TokenGeneratorInterface { - /** - * @return string - */ - public function generateToken() + public function generateToken(): string { return rtrim(strtr(base64_encode($this->getRandomNumber()), '+/', '-_'), '='); } diff --git a/src/TokenGeneratorInterface.php b/src/TokenGeneratorInterface.php index 6e9c53f..6e5e278 100644 --- a/src/TokenGeneratorInterface.php +++ b/src/TokenGeneratorInterface.php @@ -6,8 +6,5 @@ interface TokenGeneratorInterface { - /** - * @return string - */ - public function generateToken(); + public function generateToken(): string; }