From c11b16a9714b872dbc8cc1c7d84469852f8b5f55 Mon Sep 17 00:00:00 2001 From: Martin Bock Date: Wed, 9 Feb 2022 21:40:56 +0100 Subject: [PATCH 1/5] Implement random uppercased word for capitalization --- src/WordGenerator.php | 11 ++++++----- tests/Unit/WordGeneratorTest.php | 15 +++++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/WordGenerator.php b/src/WordGenerator.php index 52aa341..3c90242 100644 --- a/src/WordGenerator.php +++ b/src/WordGenerator.php @@ -144,11 +144,12 @@ public function generateWords(int $numberOfWords): array { $words = []; for ($i = 0; $i < $numberOfWords; $i++) { - $word = $this->getWord($this->generateDicedNumber()); - if ($this->config['capitalize']) { - $word = ucfirst($word); - } - array_push($words, $word); + $words[] = strtolower($this->getWord($this->generateDicedNumber())); + } + + if ($this->config['capitalize']) { + $i = array_rand($words); + $words[$i] = strtoupper($words[$i]); } return $words; diff --git a/tests/Unit/WordGeneratorTest.php b/tests/Unit/WordGeneratorTest.php index 0316da7..24989a4 100644 --- a/tests/Unit/WordGeneratorTest.php +++ b/tests/Unit/WordGeneratorTest.php @@ -6,7 +6,6 @@ use Martbock\Diceware\Exceptions\WordlistInvalidException; use Martbock\Diceware\Tests\TestCase; use Martbock\Diceware\WordGenerator; -use function ucfirst; class WordGeneratorTest extends TestCase { @@ -65,17 +64,25 @@ public function cannot_open_invalid_wordlist_file() public function should_capitalize_when_active() { $this->wordGenerator->setConfig('capitalize', true); - $words = $this->wordGenerator->generateWords(1); + $words = $this->wordGenerator->generateWords(3); + $uppercaseCounter = 0; + $lowercaseCounter = 0; foreach ($words as $word) { - $this->assertEquals(ucfirst($word), $word); + if (strtoupper($word) === $word) { + $uppercaseCounter += 1; + } else { + $lowercaseCounter += 1; + } } + $this->assertEquals(1, $uppercaseCounter); + $this->assertEquals(2, $lowercaseCounter); } /** @test */ public function should_not_capitalize_when_inactive() { $this->wordGenerator->setConfig('capitalize', false); - $words = $this->wordGenerator->generateWords(1); + $words = $this->wordGenerator->generateWords(2); foreach ($words as $word) { $this->assertEquals(strtolower($word), $word); } From f2125fe60b88f35f9bf7973e196a103eae3d42d8 Mon Sep 17 00:00:00 2001 From: Martin Bock Date: Wed, 9 Feb 2022 21:42:03 +0100 Subject: [PATCH 2/5] Update capitalize default, change config description --- config/diceware.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/diceware.php b/config/diceware.php index 4530dc8..45bd953 100644 --- a/config/diceware.php +++ b/config/diceware.php @@ -34,12 +34,13 @@ | Capitalize |-------------------------------------------------------------------------- | - | If you want to capitalize each individual word in your passphrase, + | If you want to capitalize one random word in your passphrase, | set this option to `true`. + | Example: 'landscape-FAVORING-cover-mauve' | */ - 'capitalize' => false, + 'capitalize' => true, /* |-------------------------------------------------------------------------- From e919c2ab37b62e8d0fd9fb276e0c5f026a36ba42 Mon Sep 17 00:00:00 2001 From: Martin Bock Date: Wed, 9 Feb 2022 21:45:04 +0100 Subject: [PATCH 3/5] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b75f272..2032fb6 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,12 @@ It uses [Diceware](http://world.std.com/~reinhold/diceware.html) wordlists and i ## Usage -It is very easy to generate a random diceware password, simply use the Facade like this: +It's a breeze to generate a random diceware password, simply use the Facade like this: ```php $passphrase = Diceware::generate(); -// returns 'unwind-cosmic-entryway-magnetic-stardust-ligament' +// returns 'unwind-cosmic-entryway-MAGNETIC-stardust-ligament' return $passphrase; ``` @@ -43,13 +43,13 @@ php artisan vendor:publish --provider 'Martbock\Diceware\DicewareServiceProvider ## Configuration -You may change the default settings in the [diceware.php config file](config/diceware.php) that will be published to -your Laravel config directory once you install this package. Currently, the following options are supported: +You may change the default settings in the [diceware.php config file](config/diceware.php) that you can publish to +your Laravel config directory after you installed this package. Currently, the following options are supported: ```php 'number_of_words' => 6, 'separator' => '-', -'capitalize' => false, +'capitalize' => true, 'add_number' => false, 'wordlist' => 'english', 'custom_wordlist_path' => null, From 726948c22ee34f04e05c79d9946f2d18e646e459 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 9 Feb 2022 20:45:25 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- src/WordGenerator.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/WordGenerator.php b/src/WordGenerator.php index 3c90242..5640f53 100644 --- a/src/WordGenerator.php +++ b/src/WordGenerator.php @@ -2,7 +2,6 @@ namespace Martbock\Diceware; -use function array_push; use function fclose; use function feof; use function fgets; @@ -15,7 +14,6 @@ use function random_int; use function strpos; use function strval; -use function ucfirst; class WordGenerator { From 01678ffb9a28b45c116cc12243006fbaf77e5afc Mon Sep 17 00:00:00 2001 From: Martin Bock Date: Wed, 9 Feb 2022 22:00:17 +0100 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2032fb6..289123c 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ It uses [Diceware](http://world.std.com/~reinhold/diceware.html) wordlists and i ## Usage -It's a breeze to generate a random diceware password, simply use the Facade like this: +It's a breeze to generate a random diceware passphrase, simply use the Facade like this: ```php $passphrase = Diceware::generate();