|
| 1 | +# PasswordStrength |
| 2 | + |
| 3 | +Validates that the given password has reached the minimum strength required by the constraint. |
| 4 | +The strength is calculated by measuring the entropy of the password (in bits) based on its length and the number of unique characters. |
| 5 | + |
| 6 | +```php |
| 7 | +PasswordStrength( |
| 8 | + string $minStrength = 'medium', |
| 9 | + ?string $minMessage = null |
| 10 | +); |
| 11 | +``` |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +```php |
| 16 | +Validator::passwordStrength()->validate('password'); // false |
| 17 | +Validator::passwordStrength()->validate('i8Kq*MBob~2W"=p'); // true |
| 18 | +Validator::passwordStrength(minStrength: 'very-strong')->validate('i8Kq*MBob~2W"=p'); // false |
| 19 | +``` |
| 20 | + |
| 21 | +> [!NOTE] |
| 22 | +> An `UnexpectedValueException` will be thrown when a `minStrength` option is invalid. |
| 23 | +
|
| 24 | +> [!NOTE] |
| 25 | +> An `UnexpectedValueException` will be thrown when the input value is not a `string`. |
| 26 | +
|
| 27 | +## Options |
| 28 | + |
| 29 | +### `minStrength` |
| 30 | + |
| 31 | +type: `string` default: `medium` |
| 32 | + |
| 33 | +Sets the minimum strength of the password in entropy bits. |
| 34 | +The entropy is calculated using the formula [here](https://www.pleacher.com/mp/mlessons/algebra/entropy.html). |
| 35 | + |
| 36 | +Available options are: |
| 37 | + |
| 38 | +- `weak` entropy between `64` and `79` bits. |
| 39 | +- `medium` entropy between `80` and `95` bits. |
| 40 | +- `strong` entropy between `96` and `127` bits. |
| 41 | +- `very-strong` entropy greater than `128` bits. |
| 42 | + |
| 43 | +All measurements less than `64` bits will fail. |
| 44 | + |
| 45 | +### `message` |
| 46 | + |
| 47 | +type: `?string` default: `The password strength is not strong enough.` |
| 48 | + |
| 49 | +Message that will be shown when the password is not strong enough. |
| 50 | + |
| 51 | +The following parameters are available: |
| 52 | + |
| 53 | +| Parameter | Description | |
| 54 | +|---------------------|---------------------------| |
| 55 | +| `{{ name }}` | Name of the invalid value | |
| 56 | +| `{{ minStrength }}` | Selected minimum strength | |
| 57 | + |
| 58 | +## Changelog |
| 59 | + |
| 60 | +- `0.8.0` Created |
0 commit comments