Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ Optionally you can specify *wordCacheFile* to control the location and name of t

To a custom instance you can pass an optional boolean parameter *fetch*. When false, the cached wordlist will be preferred but falls back to fetching if it could not be loaded.

You can also mix different RSS sources into the same wordlist file with the `appendWordlist` parameter, increasing entropy, or regularly rebuild your cache file from time to time to feed it with new words. Altought having more words is always desirable,
you can limit the number of items in the wordlist with the `limitWordlist` configuration parameter. When using append, wordlist is shuffled before being limited to the desired quantity.

```php
include 'PasswordGenerator.php';

Expand All @@ -90,6 +93,16 @@ $gen = PasswordGenerator::EN();
echo 'Password 1: ', $gen->generate();
echo 'Password 2: ', $gen->generate();
echo 'Password 3: ', $gen->generate();

// Append NYTimes feed to the specified wordlist, limiting to 7000 items max
$gen = new PasswordGenerator([
'wordCacheFile' => 'mywords.json',
'url' => 'https://rss.nytimes.com/services/xml/rss/nyt/World.xml',
'minLength' => 3,
'maxLength' => 8,
'appendWordlist' => true,
'limitWordlist' => 7000,
]);
```

### Caching
Expand Down Expand Up @@ -155,6 +168,15 @@ When creating an instance of PasswordGenerator you can provide the following par

Controls if and how many HTTP redirects should be followed during URL access. Defaults to *2*. To prevent following redirects set its value to *0*.

* **appendWordlist**

If true, the fetched wordlist will be appended to the current one. If false, wordlist is generated from scratch.

* **limitWordlist**

Int value declaring the maximum words the wordlist must contain. Useful when using appendWordList.


## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
12 changes: 12 additions & 0 deletions example/PasswordGenerator.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@

// generate a password with custom pattern
echo $gen->generate('wiwsw'), "\n";

// Append NYTimes feed to the last wordlist, limiting to 700 items max
$gen = new PasswordGenerator([
'wordCacheFile' => 'mywords.json',
'url' => 'https://rss.nytimes.com/services/xml/rss/nyt/World.xml',
'minLength' => 3,
'maxLength' => 8,
'appendWordlist' => true,
'limitWordlist' => 700,
]);

echo $gen->generate('wiws'), "\n";
30 changes: 26 additions & 4 deletions src/PasswordGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class PasswordGenerator
/** @var string $url Url of word ressource */
private string $url;

/** @var int $minLength Minimum length of a password */
/** @var int $minLength Minimum length of a word to be considered for the list */
private int $minLength;

/** @var int $maxLength Maximum length of a password */
/** @var int $maxLength Maximum length of a word to be considered for the list */
private int $maxLength;

/** @var array $wordList Cache of the words */
Expand All @@ -52,6 +52,12 @@ class PasswordGenerator
/** @var bool $verbose */
private bool $verbose;

/** @var bool $appendWordlist If true the fetched words will be appended to the cached wordlist, rather than replacing the current file */
private bool $appendWordlist = false;

/** @var int $limitWordlist Limit the max number of words in the wordlist files, defaults to no limit */
private int $limitWordlist = 0;

/**
* Creates an instance of the password generator. You can pass an optional
* array with values that should override the default values for the keys:
Expand All @@ -65,6 +71,10 @@ class PasswordGenerator
* <dd>The maxinum length of characters a word must have.</dd>
* <dt>wordCacheFile</dt>
* <dd>Filename of the cache file.</dd>
* <dt>appendWordlist</dt>
* <dd>If true, the fetched wordlist will be appended to the current one. If false, wordlist is generated from scratch.</dd>
* <dt>limitWordlist</dt>
* <dd>Int value declaring the maximum words the wordlist must contain. Useful when using appendWordList.</dd>
* <dt>httpRedirects</dt>
* <dd>Number of maximum HTTP redirects the script will follow.</dd>
* </dl>
Expand Down Expand Up @@ -143,9 +153,21 @@ private function populate_wordlist(): void
}
}
}
$this->wordList = array_keys($wordlist);
if ((bool)$this->appendWordlist) {
$this->wordList = array_unique($this->wordList + array_keys($wordlist));
} else {
$this->wordList = array_keys($wordlist);
}
$wordlistLimit = (int)$this->limitWordlist;
if ($wordlistLimit > 0) {
// Shuffle the array to mix existing and new words, if in append mode
if ((bool)$this->appendWordlist) {
shuffle($this->wordList);
}
$this->wordList = array_slice($this->wordList, 0, $wordlistLimit);
}

if (count($wordlist) > 0) {
if (count($this->wordList) > 0) {
$this->save_wordlist();
}
} catch (Exception $e) {
Expand Down