Skip to content

Commit

Permalink
Merge pull request #8 from martbock/feature/add-number
Browse files Browse the repository at this point in the history
Feature: numbers prepending passphrases
  • Loading branch information
martbock committed Dec 23, 2020
2 parents 7d7ab57 + 49157b0 commit e168d6a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ your Laravel config directory once you install this package. Currently, the foll
'number_of_words' => 6,
'separator' => '-',
'capitalize' => false,
'add_number' => false,
'wordlist' => 'english',
'custom_wordlist_path' => null,
'number_of_dice' => 5,
Expand Down
14 changes: 13 additions & 1 deletion config/diceware.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@

'capitalize' => false,

/*
|--------------------------------------------------------------------------
| Add a Number
|--------------------------------------------------------------------------
|
| If you want to include a number in every generated password, set this
| option to `true`. The number will be prepended to the passphrase.
| Example: '93-neglector-silly-papaya-mankind-feel-portly'
|
*/

'add_number' => false,

/*
|--------------------------------------------------------------------------
| Wordlist
Expand Down Expand Up @@ -81,5 +94,4 @@
*/

'number_of_dice' => 5,

];
15 changes: 11 additions & 4 deletions src/WordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function rollDice(): int
public function generateDicedNumber(): string
{
$result = '';

for ($i = 0; $i < $this->getNumberOfDice(); $i++) {
$result .= strval($this->rollDice());
}
Expand Down Expand Up @@ -101,8 +102,8 @@ public function parseWord(string $line): string
*
* @param string $dicedNumber
*
* @throws WordlistInvalidException
* @throws InvalidConfigurationException
* @throws WordlistInvalidException
*
* @return string
*/
Expand Down Expand Up @@ -157,8 +158,8 @@ public function generateWords(int $numberOfWords): array
/**
* Generate a diceware passphrase.
*
* @param int $numberOfWords
* @param string $separator
* @param int|null $numberOfWords
* @param string|null $separator
*
* @throws \Exception Thrown if there is not enough entropy.
*
Expand All @@ -171,7 +172,13 @@ public function generatePassphrase(?int $numberOfWords = null, ?string $separato

$words = $this->generateWords($numberOfWords);

return implode($separator, $words);
$phrase = implode($separator, $words);

if ($this->config['add_number']) {
return random_int(1, 999).$this->config['separator'].$phrase;
}

return $phrase;
}

/**
Expand Down
42 changes: 41 additions & 1 deletion tests/Unit/WordGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function setUp(): void
'number_of_words' => 6,
'separator' => '-',
'capitalize' => false,
'add_number' => false,
'wordlist' => 'eff',
'custom_wordlist_path' => null,
'number_of_dice' => 5,
Expand Down Expand Up @@ -75,12 +76,51 @@ public function cannot_open_invalid_wordlist_file()
/** @test
* @throws \Exception
*/
public function should_capitalize()
public function should_capitalize_when_active()
{
$this->wordGenerator->setConfig('capitalize', true);
$words = $this->wordGenerator->generateWords(1);
foreach ($words as $word) {
$this->assertEquals(ucfirst($word), $word);
}
}

/** @test
* @throws \Exception
*/
public function should_not_capitalize_when_inactive()
{
$this->wordGenerator->setConfig('capitalize', false);
$words = $this->wordGenerator->generateWords(1);
foreach ($words as $word) {
$this->assertEquals(strtolower($word), $word);
}
}

/** @test
* @throws \Exception
*/
public function should_add_number_when_active()
{
$this->wordGenerator->setConfig('add_number', true);
$result = $this->wordGenerator->generatePassphrase();
$arr = explode($this->config['separator'], $result);
$this->assertIsNumeric($arr[0]);
for ($i = 1; $i < $this->config['number_of_words']; $i++) {
$this->assertIsNotNumeric($arr[$i]);
}
}

/** @test
* @throws \Exception
*/
public function should_not_add_number_when_inactive()
{
$this->wordGenerator->setConfig('add_number', false);
$result = $this->wordGenerator->generatePassphrase();
$arr = explode($this->config['separator'], $result);
for ($i = 0; $i < $this->config['number_of_words']; $i++) {
$this->assertIsNotNumeric($arr[$i]);
}
}
}

0 comments on commit e168d6a

Please sign in to comment.