Skip to content

Getting Started

Welcome to Keyphrase, a modern PHP library for generating secure passwords, passphrases, and BIP39 mnemonics with an immutable, fluent API.

Install Keyphrase via Composer:

Terminal window
composer require cline/keyphrase
use Cline\Keyphrase\Generators\PasswordGenerator;
$password = PasswordGenerator::create()
->length(20)
->withSymbols()
->generate();
// Example: "K9#mPx$2nQwR@5vLjH8s"
use Cline\Keyphrase\Generators\PassphraseGenerator;
$passphrase = PassphraseGenerator::create()
->words(6)
->titleCase()
->generate();
// Example: "Correct-Horse-Battery-Staple-Cloud-Mint"
use Cline\Keyphrase\Generators\MnemonicGenerator;
$mnemonic = MnemonicGenerator::create()
->words(12)
->english()
->generate();
// Example: "abandon ability able about above absent absorb abstract absurd abuse access accident"

For convenient access to all generators, use the KeyphraseManager:

use Cline\Keyphrase\KeyphraseManager;
$manager = new KeyphraseManager();
// Quick generation methods
$password = $manager->quickPassword(16);
$securePassword = $manager->quickSecurePassword(24);
$passphrase = $manager->quickPassphrase(6);
$mnemonic = $manager->quickMnemonic(12);
$pin = $manager->pin(4);
// Access generators
$password = $manager->password()
->length(32)
->withSymbols()
->excludeAmbiguous()
->generate();

If you’re using Laravel, the package auto-registers a service provider and facade:

use Cline\Keyphrase\Facades\Keyphrase;
// Using the facade
$password = Keyphrase::password()->length(20)->generate();
$passphrase = Keyphrase::passphrase()->words(5)->generate();
$mnemonic = Keyphrase::mnemonic()->words(24)->generate();
// Quick methods
$pin = Keyphrase::pin(6);
$password = Keyphrase::quickSecurePassword(32);

All generators are immutable. Each method returns a new instance with the updated configuration:

$base = PasswordGenerator::create()->length(16);
$withSymbols = $base->withSymbols();
$withoutSymbols = $base->withSymbols(false);
// $base, $withSymbols, and $withoutSymbols are all different instances
// Original configuration is never modified

This makes generators safe to share and reuse:

$secureDefaults = PasswordGenerator::create()
->length(24)
->withSymbols()
->excludeAmbiguous();
// Create variations without modifying the original
$password1 = $secureDefaults->generate();
$password2 = $secureDefaults->length(32)->generate();
$password3 = $secureDefaults->withSymbols(false)->generate();

All generators can calculate the entropy (randomness) of generated values:

$entropy = PasswordGenerator::create()
->length(16)
->alphanumeric()
->entropy();
// Returns: 95.27 (bits of entropy)

See the Entropy Guide for more details on security strength.