Codexportfolio intelligence

@philiprehberger/php-password-strength

Password strength validation with entropy calculation and common password detection

PHPPackagist

Capabilities

README

PHP Password Strength

Tests Latest Version on Packagist Last updated

Password strength validation with entropy calculation and common password detection.

Requirements

  • PHP 8.2+

Installation

composer require philiprehberger/php-password-strength

Usage

Checking password strength

use PhilipRehberger\PasswordStrength\PasswordStrength;

$result = PasswordStrength::check('MyP@ssw0rd!2026');

echo $result->score;       // 0-4
echo $result->label();     // "very weak", "weak", "fair", "strong", or "very strong"
echo $result->entropy;     // Shannon entropy in bits
echo $result->isCommon;    // true if found in common passwords list
echo $result->length;      // Password length

// Improvement suggestions
foreach ($result->suggestions as $suggestion) {
    echo $suggestion;
}

// Array representation
$array = $result->toArray();

Quick validation

use PhilipRehberger\PasswordStrength\PasswordStrength;

// Returns true if score >= 3 (strong)
if (PasswordStrength::isStrong('MyP@ssw0rd!2026')) {
    echo 'Password is strong enough.';
}

// Custom minimum score
if (PasswordStrength::isStrong('MyP@ssw0rd!2026', minScore: 4)) {
    echo 'Password is very strong.';
}

Detailed analysis

use PhilipRehberger\PasswordStrength\PasswordStrength;

$report = PasswordStrength::analyze('MyP@ssw0rd!2026');

echo $report->score;             // 0-4
echo $report->level;             // "very weak", "weak", "fair", "strong", or "very strong"
echo $report->length;            // Password length
echo $report->hasLowercase;      // true
echo $report->hasUppercase;      // true
echo $report->hasDigits;         // true
echo $report->hasSymbols;        // true
echo $report->hasRepeatedChars;  // false
echo $report->hasSequentialChars; // false
echo $report->hasKeyboardPattern; // false

Custom dictionary

use PhilipRehberger\PasswordStrength\PasswordStrength;

PasswordStrength::addDictionary(['company', 'acme', 'internal']);

$result = PasswordStrength::check('acmepassword');
// Score reduced, suggestion: "Avoid dictionary words."

PasswordStrength::clearDictionaries();

Personal context checking

use PhilipRehberger\PasswordStrength\PasswordStrength;

$report = PasswordStrength::withContext(['john', 'john@example.com'])
    ->analyze('john2024!');

echo $report->hasPersonalContext; // true
// Suggestion: "Avoid using personal information in your password"

Policy-based validation

use PhilipRehberger\PasswordStrength\PasswordPolicy;
use PhilipRehberger\PasswordStrength\PasswordStrength;

$policy = (new PasswordPolicy)
    ->minLength(10)
    ->requireUppercase()
    ->requireDigits()
    ->requireSymbols()
    ->minScore(3);

// Using the policy directly
$policy->check('MyP@ssw0rd!2026'); // true

// Using the main class
PasswordStrength::meetsPolicy('MyP@ssw0rd!2026', $policy); // true
PasswordStrength::meetsPolicy('weak', $policy);             // false

API

PasswordStrength

MethodDescription
PasswordStrength::check(string $password): StrengthResultAnalyse a password and return a result
PasswordStrength::isStrong(string $password, int $minScore = 3): boolReturns true if the score meets the minimum
PasswordStrength::analyze(string $password): StrengthReportReturn a detailed strength report with analysis flags
PasswordStrength::meetsPolicy(string $password, PasswordPolicy $policy): boolCheck if a password satisfies a policy
PasswordStrength::addDictionary(array $words): voidAdd custom dictionary words to check against
PasswordStrength::clearDictionaries(): voidClear all custom dictionaries
PasswordStrength::withContext(array $context): PendingAnalysisCreate a pending analysis with personal context

StrengthResult

Property / MethodTypeDescription
scoreintStrength score from 0 to 4
entropyfloatShannon entropy in bits
isCommonboolWhether the password is in the common list
lengthintPassword length in characters
suggestionsarrayList of improvement suggestions
label(): stringHuman label: very weak, weak, fair, strong, very strong
toArray(): arraySerialize to array

StrengthReport

PropertyTypeDescription
scoreintStrength score from 0 to 4
levelstringHuman-readable strength level
hasLowercaseboolWhether the password contains lowercase letters
hasUppercaseboolWhether the password contains uppercase letters
hasDigitsboolWhether the password contains digits
hasSymbolsboolWhether the password contains special characters
hasRepeatedCharsboolWhether the password has 3+ repeated characters in a row
hasSequentialCharsboolWhether the password has 3+ sequential characters
hasKeyboardPatternboolWhether the password contains keyboard patterns
lengthintPassword length in characters
hasPersonalContextboolWhether the password contains personal context information
suggestionsarrayList of improvement suggestions

PendingAnalysis

MethodDescription
analyze(string $password): StrengthReportAnalyze a password with personal context applied

PasswordPolicy

MethodDescription
minLength(int $length): selfSet minimum password length
requireUppercase(): selfRequire at least one uppercase letter
requireDigits(): selfRequire at least one digit
requireSymbols(): selfRequire at least one special character
minScore(int $score): selfSet minimum strength score (0-4)
check(string $password): boolCheck if a password meets the policy

Score Meanings

ScoreLabelDescription
0Very WeakTrivial or common password
1WeakLow entropy or very short
2FairModerate entropy, room to improve
3StrongGood entropy and character variety
4Very StrongExcellent entropy and length

Development

composer install
vendor/bin/phpunit
vendor/bin/pint --test

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT