hackzilla / password-generator

PHP Library to generate random passwords
https://hackzilla.org
MIT License
293 stars 37 forks source link

Ability to set max length of HumanPasswordGenerator #12

Closed tomsommer closed 8 years ago

tomsommer commented 9 years ago

You can set the number of words, but it would be nice if you could set the minimum length of the words, and the max length of the password.

Or be able to set the max length of the password, and the minimum number of words in it.

hackzilla commented 9 years ago

I believe I already have the option to set min and max length of the words.

The default is between 3 and 20 characters.

An example would be configured like this:

use Hackzilla\PasswordGenerator\Generator\HumanPasswordGenerator;

$generator = new HumanPasswordGenerator();

$generator
  ->setWordList('/usr/share/dict/words')
  ->setMinWordLength(3)
  ->setMaxWordLength(20)
  ->setWordCount(3)
  ->setWordSeparator('-');

$password = $generator->generatePasswords(10);
tomsommer commented 9 years ago

Right, I know. But I can't get a 20 char password consisting of 3 or more random words with the above. As far as I can tell, anyway.

hackzilla commented 9 years ago

I'll have a think.

It'll be possible to limit the password length.

hackzilla commented 8 years ago

I've added a branch with all the boiler plate code.

If you can think of a good way to generate a password of a fixed length, then I'm open to suggestions.

HumanPasswordGenerator.php#L123

This is my current work in progress.

        $desiredLength = $this->getLength() || $this->getMaxPasswordLength();

        for ($i = 0; $i < $wordCount; $i++) {
            if ($i) {
                $password .= $this->getWordSeparator();
            }

            if ($this->getLength()) {
                $minLength = $this->getMinWordLength();
                $maxLength = $this->getMaxWordLength();

                $password .= $this->randomWord($minLength, $maxLength);
            } else {
                $password .= $this->randomWord();
            }
        }
hackzilla commented 8 years ago

This feature is currently in the develop branch, waiting for release.

Though I need to tweak it. Currently if you request a password of 30 characters, with 3 words; it is possible it will look for 28 character. I need check the maximum word length before release.

hackzilla commented 8 years ago

In release 1.2.0