Search by

zdenekgebauer / password-strength-meter

zdenekgebauer

check strength of passwords

Package info

gitlab.com/zdenekgebauer/password-strength-meter

pkg:composer/zdenekgebauer/password-strength-meter

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

2.1.0 2026-12-01 11:39 UTC

This package is auto-updated.

Last update: 2026-12-01 10:44:28 UTC


README

PHP 8.4+ library for validating password strength by configurable rules.

Requirements

PHP 8.4 or newer. No runtime dependencies beyond ext-mbstring.

Installation

composer require zdenekgebauer/password-strength-meter

Upgrading from 1.x: version 2.0.0 contains breaking changes, see the BC break entries in CHANGELOG.md.

Usage

use ZdenekGebauer\PasswordStrengthMeter\Constraints;
use ZdenekGebauer\PasswordStrengthMeter\Validator;

Check password by rules

$constraints = new Constraints();
$constraints->minLength = 10;          // default: Constraints::MIN_LENGTH (4)
$constraints->requiredNumber = true;
$constraints->requiredUppercase = true;
$constraints->requiredLowercase = true;
$constraints->requiredSpecialChar = true;

$result = (new Validator($constraints))->validate('aikfF56$');

if ($result->isStrong()) {
    echo 'password is OK';
} else {
    echo 'password is WEAK';
    foreach ($result->getViolations() as $violation) {
        echo $violation->value;
    }
}

Violations

Result::getViolations() returns list<Violation>. Violation is a backed enum; its value is a stable identifier safe to use as a translation key — it will not change without a major version bump. Possible values:

Violation caseValueCondition
TOO_SHORTtoo-shortlength < minLength
FORBIDDEN_TEXTforbidden-textcontains a forbidden text (case-insensitive)
REPEATED_CHARSrepeated-chars3+ identical characters in a row
NO_NUMBERno-numberno digit, requiredNumber = true
NO_UPPERno-upper-charno uppercase, requiredUppercase = true
NO_LOWERno-lower-charno lowercase, requiredLowercase = true
NO_SPECIALno-special-charno special char, requiredSpecialChar = true

All rules work with characters, not bytes, so multibyte passwords are evaluated correctly: Ť counts as an uppercase letter and ššš as three repeated characters.

A single rule can be checked with Result::hasViolation():

if ($result->hasViolation(Violation::TOO_SHORT)) {
    echo 'password is too short';
}

Forbidden texts

Default forbidden list is available as Constraints::DEFAULT_FORBIDDEN_TEXTS. Additional texts can be added (matching is case-insensitive):

$constraints = new Constraints();
$constraints->addForbiddenTexts(['heslo', 'iloveyou']);

The current list is readable through $constraints->forbiddenTexts, but it is read-only from the outside — addForbiddenTexts() is the only way to extend it. Because matching ignores case, texts differing only in case are treated as duplicates and added just once.

Special characters

Default set is available as Constraints::DEFAULT_SPECIAL_CHARS. Can be replaced with a custom set:

$constraints = new Constraints();
$constraints->specialChars = '!@#$%';

The value is a plain string; the validator splits it into characters itself, multibyte characters such as € included.

Exceptions

\InvalidArgumentException is thrown when:

  • minLength is set below Constraints::MIN_LENGTH (4),
  • specialChars is assigned an empty string.

Validator::validate() never throws. The password parameter is marked with #[\SensitiveParameter], so it is not exposed in stack traces.

Development

Local environment runs on Lando (Docker, PHP 8.5 + xdebug).

lando install              # install dependencies
lando tests                # run all tests
lando coverage             # code coverage
lando dependency-analyser  # composer.json dependencies against their usage in code
lando ecs                  # coding standards (lando ecs-fix to apply)
lando infection            # mutation testing, requires 100 % MSI
lando phpstan              # static analysis, level max
lando rector               # code modernization dry-run (lando rector-fix to apply)

License

MIT