juststeveking/password-generator

Generate random, memorable passwords easily.

1.0.0 2022-11-16 11:02 UTC

This package is auto-updated.

Last update: 2024-05-28 22:26:23 UTC


README

Latest Version on Packagist Test Suite Total Downloads

Disclaimer. This is not intended for use in a production environment to create your passwords. My use case for this is to actually generate one off use codes such as One Time Pass Codes. This is not the most secure as the list of words is quite small, and will leave you open to a potential dictionary attack.

Installation

You can install the package via composer:

composer require juststeveking/password-generator

If you are using Laravel, you can publish the config file with:

php artisan vendor:publish --tag="password-generator-config"

Set up - Laravel

To set this package up, you can replace the words within the config file with those of your own choosing. Then you are ready to go.

Usage

Using the Facade in Laravel

Call the facade directly to resolve the Password Generator from the container and call the static methods.

use JustSteveKing\PasswordGenerator\Facades\Generator;

// Generate a standard password
$password = Generator::generate(); // example 'strong-car-fast-snail'

// Generate a "secure" password
$password = Generator::generateSecure(); // example 'str0ng-c4r-f4st-sn41l'

Without the Facade in Laravel

Inject the contract into the constructor, and call either "generate" or "generateSecure" depending on your use case.

use JustSteveKing\PasswordGenerator\Contracts\GeneratorContract;
use Illuminate\Http\JsonResponse;

class GeneratePasswordController
{
    public function __construct(
        private readonly GeneratorContract $generator,
    ) {}
    
    public function __invoke(): JsonResponse
    {
        return new JsonResponse(
            data: [
                'password' => $this->generator->generate(), // or generateSecure() for "secure" passwords.
            ],
        );
    }
}

Using outside of Laravel

If you have a DI container all you will need to do is resolve the class using the instance. Be mindful that you will need to set up your own container instructions and load in your own words list.

// Once you have added this to your container, and injected
$password = $this->generator->generate();

// Or like the Laravel implementation, generate a secure password
$password = $this->generator->generateSecure();

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.