Giter Site home page Giter Site logo

php-password-generator's Introduction

php-password-generator

The PHP class PasswordGenerator serves as a password generator to create memorable passwords like the keychain of macOS โ‰ค 10.14 did.

Getting Started

Add via composer

Use composer to install the generator into your project:

composer require darkv/php-password-generator

Direct Import

Copy the file PasswordGenerator.php into your project and include it in your own PHP file(s) with include 'PasswordGenerator.php';. Then create an instance either by using the predefined static methods for specific languages or customize it yourself by using the standard constructor.

include 'PasswordGenerator.php';

// Import the namespace
use \Darkv\PhpPasswordGenerator\PasswordGenerator;

// create instance with an English word list
$gen = PasswordGenerator::EN();

// generate a password
echo $gen->generate();

Prerequisites

This class works with PHP >= 7.4 and needs a working internet connection.

Password Syntax

The syntax of generated passwords can be defined by a pattern. That pattern consists of control characters that define the construction of the password string. The available control characters are:

  • i
    An integer between 1 and 999.
  • s
    A punctuation character (ASCII codes 33 to 47).
  • w
    A word from the wordlist.

If you don't provide your own pattern the default pattern wisw is used. Some examples of generated passwords with that default pattern are:

  • Theyre778+Breakthrough
  • Reforms13)Translated
  • When249*Awards

Word Lists

The class uses RSS feeds to build a word list from which random words are used for password generation. The class has some predefined configurations for the languages English and German but can be customized too:

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

// create instance with English word list
$gen = PasswordGenerator::EN();

// create instance with German word list
$gen = PasswordGenerator::DE();

// create instance with custom parameters
$gen = new PasswordGenerator([
	'url'       => 'https://www.tagesschau.de/newsticker.rdf',
	'minLength' => 3,
	'maxLength' => 6,
]);

The params minLength and maxLength denote the allowed lengths of the words from the URL source to get into the word list. If a word list has been successfully built, that list is saved into the file wordlist.json. The next time you create an instance of PasswordGenerator and the URL source cannot be contacted or does not contain any usable words that cached list is loaded instead. If you reuse the very same instance the word list is also reused so no further HTTP requests are generated.

Optionally you can specify wordCacheFile to control the location and name of the cached wordlist.

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.

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

$gen = PasswordGenerator::EN();

// reuse word list without rebuilding
echo 'Password 1: ', $gen->generate();
echo 'Password 2: ', $gen->generate();
echo 'Password 3: ', $gen->generate();

Caching

If you need to use that class in contexts where you do not have an internet connection you can prebuild a word list and copy the generated wordlist.json file into your project. When using the PasswordGenerator you can tell it to only use that cached list and skip the URL source request:

include 'PasswordGenerator.php';

use \Darkv\PhpPasswordGenerator\PasswordGenerator;

$gen = PasswordGenerator::CACHED();

echo $gen->generate();

Location of the cache file can be passed as parameter to CACHED, by default wordlist.json in work dir will be used.

URL Sources

As a source for word lists, this class uses a configurable RSS feed. The feed has to be in XML format and contain description tags from which the textual content is extracted.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.