Skip to content

A random passphrase generator inspired by Randall Munroes XKCD #936

License

Notifications You must be signed in to change notification settings

gregor-j/correct-horse

Repository files navigation

correct horse passphrase generator

License: MIT Maintainability Test Coverage Packagist Version

A random passphrase generator inspired by Randall Munroes XKCD #936, bbusschots/hsxkpasswd, and matt-allan/battery-staple. Thanks!

Why??

I needed a generator for handing out initial passwords in PHP. The password should not be easy to guess, especially not for a computer, and the person receiving the password should be able to enter the password fast and without mistakes.

The comic that inspired a lot of people

To anyone who understands information theory and security and is in an infuriating argument with someone who does not (possibly involving mixed case), I sincerely apologize.

Dictionary

This repository contains a German word list based on the GPL-licensed German dictionary for WinEdit by Juergen Vierheilig, copied from Crypt::HSXKPasswd::Dictionary::DE. I tried to rid the word list of NSFW words.

In order to add dictionaries, either implement the \GregorJ\CorrectHorse\DictionaryInterface or just copy files into the dict directory and use \GregorJ\CorrectHorse\Dictionaries\DictionaryFile.

Usage

composer require gregorj/correct-horse
<?php

require_once 'vendor/autoload.php';

use GregorJ\CorrectHorse\Dictionaries\DictionaryFile;
use GregorJ\CorrectHorse\Generators\RandomNumbers;
use GregorJ\CorrectHorse\Generators\RandomWord;
use GregorJ\CorrectHorse\Generators\RandomWords;
use GregorJ\CorrectHorse\Generators\RandomCharacter;
use GregorJ\CorrectHorse\PassphraseGenerator;

$passphrase = new PassphraseGenerator();
// separate the random items of the passphrase with a special character
$passphrase->setSeparator(new RandomCharacter(['-', '#', '.', ' ']));
// random numbers should be between 1 and 99
$randomNumbers = new RandomNumbers();
$randomNumbers->setMinAndMax(1,99);
// add one random number at the beginning
$passphrase->add($randomNumbers, 1);
// add 4 random lower or upper case words
$passphrase->add(
    new RandomWords(
        new RandomWord(new DictionaryFile('dict-de-lc.txt')),
        new RandomWord(new DictionaryFile('dict-de-uc.txt'))
    ),
    4
);
// finally, add another random number
$passphrase->add($randomNumbers, 1);
// now let's generate a random passphrase
echo $passphrase->generate().PHP_EOL;
//9 korrekt Pferd Batterie Heftklammer 36

Testing

There are unit tests for every class.

docker run \
    --init \
    --rm \
    --volume $(pwd):/app \
    --workdir /app \
    php:7.2 vendor/bin/phpunit