In Generating Random Passwords 3 – Untypable Characters we experimented with generating characters that are not on our keyboard. We can take that another step and use emoji characters.
To handle this, I’m going to use another library that has emoji loaded in it already. I’ll add this to my composer.json
, and then run composer update
.
1 2 3 4 5 6 7 8 9 10 11 |
{ "require": { "symfony/console": "~3.3", "juanparati/emoji": "*@dev" }, "autoload": { "psr-4": { "RndPwd\\": "src/" } } } |
This library has a dictionary of emoji, and a converter to be able to output them to HTML or the console. Some of these do not print correctly in the console, especially if you have a monochromatic font on your console, and 99% of us do.
Let’s add the dictionary. This is long, so if you want to see it, you will need to take a look on GitHub , GitLab , or Our GitLab .
43 44 45 46 47 48 49 50 51 |
/** * Emoji list * * @link http://unicode.org/emoji/charts/full-emoji-list.html * @var array */ protected static $dictionary = [ //This dictionary is huge. Check out the repository link at the end of this post for the full thing. ]; |
I’ve commented out the color variations and the flags, as they were not rendering correctly.
And the function that will make use of that dictionary.
31 32 33 34 35 36 37 38 39 40 41 |
public static function generateRandomPasswordEmoji(int $length = 12) { $pool = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; $pool = \str_split($pool); foreach (self::$dictionary as $k => $hex) { $pool[] = Emoji::char($k) . ''; } return static::generateRandomPassword($length, $pool); } |
Here I am taking the base pool, and adding the emoji character to it.
Now we will add the flag, --e
to the application.
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 |
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') ; } 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); } |
Alright, now let’s run that and see what happens!
This is on Windows 7, PHP 7.0.
1 2 3 |
$ php pwdrnd gen 32 --e 😦🆘🔢👍💉🕰👅d🍗🚅📗🍫!✊❌🚨⬇♍🐄⚖☔🎼🥕🌆🍜💽📹🌨🎱❤🌑🚌 |
Okay, better than I was expecting, to be honest.
This is on Windows 8.1, PHP 7.1
1 2 3 |
$ php pwdrnd gen 32 --e ?⛵✴??↩???????????????39????S?◻5?�??????39????S?◻5?�???S?◻5??◻5?�5?�� |
This is really strange. In the console, I get a bunch of squares, as if it did not render correctly. But when I copy it to the blog, it works, almost. The number of characters is not 32, like I had requested of the program.
This is Ubuntu 17.04, PHP 7.1
1 2 3 |
$ php pwdrnd gen 32 --e ??⚙????????⬆?????♉???S9??E????)⛴��??♉???S9??E????)⛴�?E????)⛴??)⛴��)⛴�� |
In the console, none of the emoji have color; I touched on that at the beginning of this post. But we have 32 characters, and they render in the console. We do have a winner.
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 .