Password Generation
The PasswordGenerator creates secure random passwords with customizable character sets, lengths, and exclusions.
Basic Usage
Section titled “Basic Usage”use Cline\Keyphrase\Generators\PasswordGenerator;
// Using static factory method$password = PasswordGenerator::create()->generate();
// Using constructor$generator = new PasswordGenerator();$password = $generator->generate();Setting Length
Section titled “Setting Length”// Default is 16 characters$password = PasswordGenerator::create()->generate();
// Custom length$password = PasswordGenerator::create() ->length(32) ->generate();Character Sets
Section titled “Character Sets”Default Character Sets
Section titled “Default Character Sets”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();Disabling Character Sets
Section titled “Disabling Character Sets”// Uppercase only$password = PasswordGenerator::create() ->withLowercase(false) ->withDigits(false) ->generate();
// Digits only$password = PasswordGenerator::create() ->withLowercase(false) ->withUppercase(false) ->generate();Presets
Section titled “Presets”Common configurations are available as presets:
Alphanumeric
Section titled “Alphanumeric”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"Hexadecimal
Section titled “Hexadecimal”Lowercase hex characters (0-9, a-f):
$hex = PasswordGenerator::create() ->hex() ->length(32) ->generate();
// Example: "a1b2c3d4e5f67890abcdef1234567890"Customization
Section titled “Customization”Excluding Ambiguous Characters
Section titled “Excluding Ambiguous Characters”Remove visually similar characters (0, O, 1, l, I):
$password = PasswordGenerator::create() ->excludeAmbiguous() ->generate();
// Never contains: 0, O, 1, l, IExcluding Specific Characters
Section titled “Excluding Specific Characters”Remove any characters you want to exclude:
$password = PasswordGenerator::create() ->exclude('aeiou') ->generate();
// Never contains vowelsAdding Custom Characters
Section titled “Adding Custom Characters”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, !, @, #Generating Multiple Passwords
Section titled “Generating Multiple Passwords”$passwords = PasswordGenerator::create() ->length(20) ->withSymbols() ->generateMany(10);
// Returns array of 10 unique passwordsEntropy Calculation
Section titled “Entropy Calculation”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 bitsFull Example
Section titled “Full Example”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);Security Recommendations
Section titled “Security Recommendations”| Use Case | Minimum Length | Recommended Settings |
|---|---|---|
| PIN | 6 | ->pin()->length(6) |
| Web Account | 16 | ->length(16)->alphanumeric() |
| High Security | 24+ | ->length(24)->withSymbols()->excludeAmbiguous() |
| API Key | 32+ | ->length(32)->alphanumeric() |
| Master Password | 20+ | ->length(20)->withSymbols() |