Skip to content

Commit

Permalink
Merge pull request #16 from martbock/feature/randomize-capitalization
Browse files Browse the repository at this point in the history
Randomize capitalization
  • Loading branch information
martbock authored Feb 9, 2022
2 parents 681b11d + 01678ff commit 51355ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 passphrase, 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;
```

Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions config/diceware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,

/*
|--------------------------------------------------------------------------
Expand Down
13 changes: 6 additions & 7 deletions src/WordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Martbock\Diceware;

use function array_push;
use function fclose;
use function feof;
use function fgets;
Expand All @@ -15,7 +14,6 @@
use function random_int;
use function strpos;
use function strval;
use function ucfirst;

class WordGenerator
{
Expand Down Expand Up @@ -144,11 +142,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;
Expand Down
15 changes: 11 additions & 4 deletions tests/Unit/WordGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 51355ef

Please sign in to comment.