As a programmer, I will often need a password for a service account. A service account being an account that a computer program uses, and not a human. As such, the password can be long, and does not exactly need to be type-able.
I tried looking online for a random password generator, and none of them, I felt, were up to the task of creating passwords for my service accounts.
Having experience with Symfony Console, I thought I could put together a simple application for generating passwords of a given length.
Easy right?
Well, let’s begin with the composer.json
file
1 2 3 4 5 6 7 8 9 10 |
{ "require": { "symfony/console": "~3.3" }, "autoload": { "psr-4": { "RndPwd\\": "src/" } } } |
I’ve decided to have the namespace of RndPwd
for this one, a shortening of Random Password
.
First up is the password class. The one that will do the generating.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace RndPwd; class RandomPassword { public static function generateRandomPassword(int $length = 12, $pool = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~') { if (is_string($pool)) { $pool = \str_split($pool); } $password = ''; $poolLength = \count($pool) - 1; for ($iterator = 0; $iterator < $length; $iterator++) { $password .= $pool[\random_int(0, $poolLength)]; } return $password; } } |
I’m going to take either a string or an array for $pool and use that to pick random characters.
Alright, let’s make the command class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php namespace RndPwd; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RandomCommand extends Command { protected function configure() { $this->setName("gen") ->setDescription("Generates a randomized password") ->addArgument('Password Length', InputArgument::REQUIRED, 'The length of the resulting password') ; } protected function execute(InputInterface $input, OutputInterface $output) { $RP = new RandomPassword(); $pwd = $RP::generateRandomPassword($input->getArgument('Password Length')); $output->writeln($pwd); } } |
Simple, right? It is for now.
We just need the driver file so that we can execute it and get our random passwords.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#!/usr/bin/env php <?php require_once __DIR__ . '/vendor/autoload.php'; use RndPwd\RandomCommand; use Symfony\Component\Console\Application; $app = new Application(); $app->add(new RandomCommand()); $app->run(); |
Let’s test that bad boy.
1 2 3 4 5 6 7 |
$ php pwdrnd gen 32 JyH&4~zM_Sj*ROy!W%3S+-]%n~0V`c"1 $ php pwdrnd gen 32 a7YH4_d.#CuSOb7?"#%cUw{Y4>+ymZ"j |
Alright, that works!
Although there are some characters in there that may cause trouble, especially if we are connecting to an API that does not or cannot escape those characters.
The repository for this post can be found on GitHub here .
The repository for this post can be found on GitLab here .
The repository for this post can be found on Lupe Code’s GitLab mirror here .