This is a continuation of previous post, Generating Random Passwords 4 – Emoji.
What we need to do now is to save the passwords we have been creating. As we have seen, the console does not always represent the characters correctly. Given that, we cannot rely that copy-and-paste will work correctly either.
We will just update the RandomCommand.php
file with a new option and the code to go with it.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?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') ->addArgument('Character Set', InputArgument::OPTIONAL, 'The set of characters to use') ->addOption('u', null, InputOption::VALUE_NONE, 'Use untypable characters') ->addOption('e', null, InputOption::VALUE_NONE, 'Use emoji characters') ->addOption('o', null, InputOption::VALUE_REQUIRED, 'The output file') ; } protected function execute(InputInterface $input, OutputInterface $output) { $RP = new RandomPassword(); if ($input->getArgument('Character Set')) { $pwd = $RP::generateRandomPassword($input->getArgument('Password Length'), $input->getArgument('Character Set')); } elseif ($input->getOption('u')) { $pwd = $RP::generateRandomPasswordUntypable($input->getArgument('Password Length')); } elseif ($input->getOption('e')) { $pwd = $RP::generateRandomPasswordEmoji($input->getArgument('Password Length')); } else { $pwd = $RP::generateRandomPassword($input->getArgument('Password Length')); } $output->writeln($pwd); if ($outputFile = $input->getOption('o')) { \file_put_contents($outputFile, $pwd); $output->writeln("Password saved to '" . \realpath($outputFile) . "'"); } } } |
What I have done here is to add a new option flag, --o
, which will serve as the output file. We then just dump the password into that file, raw.
Let’s try that out.
1 2 3 4 |
$ php pwdrnd gen 32 --u --o="pass.txt" �Q�l��k.n��|Dˮ�:�%/��A���HH�j�jj Password saved to '~/symfony-console-random-password/pass.txt' |
Now, to open that file and see what is in there.
¤QÏl
k.nÀ|DË®:ó%/çÆAáËî¼HHî¹j
That’s fairly close to what we saw in the console. But an important thing to note here is that the encoding you open the file in will make all the difference.
Above was opened with ISO-8859-1 encoding, and here is Windows-1252
¤QÏl…k.n”À|DË®„:ó%/çÆAáËî¼HHî¹j
There are a few differences. Opening with UTF-8 encoding shows the same thing as the console window. Since we want usable characters, pick which encoding you want, and remember that when ever you open your password file again.
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 .