Skip to content

Password Generation

The PasswordGenerator creates secure random passwords with customizable character sets, lengths, and exclusions.

use Cline\Keyphrase\Generators\PasswordGenerator;
// Using static factory method
$password = PasswordGenerator::create()->generate();
// Using constructor
$generator = new PasswordGenerator();
$password = $generator->generate();
// Default is 16 characters
$password = PasswordGenerator::create()->generate();
// Custom length
$password = PasswordGenerator::create()
->length(32)
->generate();

By default, passwords include:

  • Lowercase letters (a-z)
  • Uppercase letters (A-Z)
  • Digits (0-9)

Symbols are not included by default.

$generator = PasswordGenerator::create();
// Toggle character sets
$password = $generator
->withLowercase(true) // Include a-z (default: true)
->withUppercase(true) // Include A-Z (default: true)
->withDigits(true) // Include 0-9 (default: true)
->withSymbols(true) // Include !@#$%^&*... (default: false)
->generate();
// Uppercase only
$password = PasswordGenerator::create()
->withLowercase(false)
->withDigits(false)
->generate();
// Digits only
$password = PasswordGenerator::create()
->withLowercase(false)
->withUppercase(false)
->generate();

Common configurations are available as presets:

Letters and digits only (no symbols):

$password = PasswordGenerator::create()
->alphanumeric()
->length(20)
->generate();
// Example: "Kj8mNpQ2xR5vLwY9sT3z"

Digits only:

$pin = PasswordGenerator::create()
->pin()
->length(6)
->generate();
// Example: "847291"

Lowercase hex characters (0-9, a-f):

$hex = PasswordGenerator::create()
->hex()
->length(32)
->generate();
// Example: "a1b2c3d4e5f67890abcdef1234567890"

Remove visually similar characters (0, O, 1, l, I):

$password = PasswordGenerator::create()
->excludeAmbiguous()
->generate();
// Never contains: 0, O, 1, l, I

Remove any characters you want to exclude:

$password = PasswordGenerator::create()
->exclude('aeiou')
->generate();
// Never contains vowels

Add your own characters to the pool:

$password = PasswordGenerator::create()
->withLowercase(false)
->withUppercase(false)
->withDigits(false)
->withCustomCharacters('ABC123!@#')
->length(10)
->generate();
// Only uses: A, B, C, 1, 2, 3, !, @, #
$passwords = PasswordGenerator::create()
->length(20)
->withSymbols()
->generateMany(10);
// Returns array of 10 unique passwords

Calculate the entropy (bits of randomness) for the current configuration:

$generator = PasswordGenerator::create()
->length(16)
->alphanumeric();
$entropy = $generator->entropy();
// Returns: 95.27 bits
// More characters = more entropy
$entropy = PasswordGenerator::create()
->length(16)
->withSymbols()
->entropy();
// Returns: ~105 bits
use Cline\Keyphrase\Generators\PasswordGenerator;
// Create a secure password generator with common settings
$generator = PasswordGenerator::create()
->length(24)
->withSymbols()
->excludeAmbiguous();
// Generate passwords
$password1 = $generator->generate();
$password2 = $generator->generate();
// Check entropy
$entropy = $generator->entropy();
echo "Password entropy: {$entropy} bits";
// Generate multiple
$passwords = $generator->generateMany(5);
Use CaseMinimum LengthRecommended Settings
PIN6->pin()->length(6)
Web Account16->length(16)->alphanumeric()
High Security24+->length(24)->withSymbols()->excludeAmbiguous()
API Key32+->length(32)->alphanumeric()
Master Password20+->length(20)->withSymbols()