PHP Classes

PHP Password Validation Helper: Generate and check a password according to rules

Recommend this page to a friend!
  Info   View files Documentation   View files View files (19)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 98 This week: 1All time: 9,810 This week: 560Up
Version License PHP version Categories
password-helper 1.0.1The PHP License5PHP 5, Validation, Security
Description 

Author

This package can generate and check a password according to rules.

It can take parameters that define rules of several types of validation to be applied to a password that can be generated by the package.

The package can also take a given password string and check if it is valid according to the same rules. Currently it supports rules like:

- The minimum length a password must be
- The minimum number of special characters required to be in the password
- The minimum number of uppercase letters required to be in the password
- The minimum number of lowercase letters required to be in the password
- The minimum number of total alphabetic characters required to be in the password
- The minimum number of numbers required to be in the password

Picture of John Conde
  Performance   Level  
Name: John Conde <contact>
Classes: 6 packages by
Country: United States United States
Age: 50
All time rank: 2410328 in United States United States
Week rank: 106 Up12 in United States United States Up
Innovation award
Innovation award
Nominee: 1x

Documentation

Latest Stable Version Total Downloads PHP Composer Scrutinizer Code Quality Maintainability License

Password Helper (password-helper)

A PHP library that makes using best practices with passwords easy _by default_.

Requirements

  • PHP 7.2+

Note: There is a PHP 5 compatible version available.

Installation

Simply add a dependency on stymiee/password-helper to your project's composer.json file if you use Composer to manage the dependencies of your project.

Here is a minimal example of a composer.json file that just defines a dependency on Password Helper:

{
    "require": {
        "stymiee/password-helper": "^2"
    }
}

To use the PHP 5 compatible version, use version 1.*:

{
    "require": {
        "stymiee/password-helper": "^1"
    }
}

Basic Usage

Configuration

To configure your Password Helper to suit your business requirements, you can set your password policy when creating your Password Helper object. There are six factors you can configure be required (or not) and, if required, the minimum criteria for that password characteristic. They are:


  1. minimumLength - Sets the minimum length a password must be. (Default: 10)
  2. minimumSpecialChars - Sets the minimum number of special characters required to be in the password (Default: 1)
  3. minimumUppercase - Sets the minimum number of uppercase letters required to be in the password (Default: 1)
  4. minimumLowercase - Sets the minimum number of lowercase letters required to be in the password (Default: 1)
  5. minimumLetters - Sets the minimum number of total alphabetic characters required to be in the password (Default: 1)
  6. minimumDigits - Sets the minimum number of numbers required to be in the password (Default: 1)

If you do not pass any custom policy rules when creating your Password Helper it will default to the values listed above.

$passwordHelper = new Password();

is equivalent to:

$passwordHelper = new Password([
    'minimumLetters' => 1,
    'minimumDigits' => 1,
    'minimumLowercase' => 1,
    'minimumUppercase' => 1,
    'minimumSpecialChars' => 1,
    'minimumLength' => 10 
]);

To modify a policy you can pass it by name, with its custom value, to the constructor. The code below sets all the rules to require two of each type and sets a minimum password length of twelve characters.

$passwordHelper = new Password([
    'minimumLetters' => 2,
    'minimumDigits' => 2,
    'minimumLowercase' => 2,
    'minimumUppercase' => 2,
    'minimumSpecialChars' => 2,
    'minimumLength' => 12 
]);

You only need to pass a custom value when you change its value from the default value. The code below only changes the values for minimumDigits and minimumLength.


$passwordHelper = new Password([
    'minimumDigits' => 2,
    'minimumLength' => 12 
]);

To remove a requirement give it a value of zero.


$passwordHelper = new Password([
    'minimumSpecialChars' => 0  // Special characters are not required
]);
    

Generate a new password

$password = (\PasswordHelper\new Password())->generate(); // 8TpKC>&nQA

Validate a password is acceptable under your password policy

$password = \PasswordHelper\new Password();
echo var_dump($password->validateComplexity('!aa34sDDdfg7dfgdsfg2gg'));
echo var_dump($password->validateComplexity('1234'));

Outputs

true
false    

Check the strength of a password

$password = \PasswordHelper\new Password();
echo $password->checkStrength('a');
echo $password->checkStrength('qr193');
echo $password->checkStrength('8TpKC>&nQA');

Outputs

Very Weak
Good
Very Strong

Hash a password

$hashedPassword = (\PasswordHelper\new Password())->hash('secret1234');

Validate a password

$password = \PasswordHelper\new Password();
if ($password->verify('secret1234', $row['password_hash'])) {
    // Let them in
} else {
    // Authentication failure
}   

Update the hash of a password

$password = \PasswordHelper\new Password();
if ($password->checkForRehash($row['password_hash'])) {
    $newHash = $password->hash('secret1234');
    // ... then save the new hash ...
}

Support

If you require assistance using this library start by viewing the HELP.md file included in this package. It includes common problems and their solutions.

If you need additional assistance, I can be found at Stack Overflow. Be sure when you ask a question pertaining to the usage of this library be sure to tag your question with the PHP and password tags. Make sure you follow their guide for asking a good question as poorly asked questions will be closed, and I will not be able to assist you.

A good question will include all the following: - A description of the problem (what are you trying to do? what results are you expecting? what results are you actually getting?) - The code you are using (only post the relevant code) - Any error message(s) you are getting

Do not use Stack Overflow to report bugs. Bugs may be reported here.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (1 directory)
Files folder imagetests (4 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file CHANGELOG Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file HELP.md Data Auxiliary data
Accessible without login Plain text file license.txt Doc. Documentation
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imagePasswordHelper (5 files)

  Files folder image Files  /  src  /  PasswordHelper  
File Role Description
  Plain text file Generator.php Class Class source
  Plain text file Password.php Class Class source
  Plain text file Policy.php Class Class source
  Plain text file StrengthChecker.php Class Class source
  Plain text file Validator.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file GeneratorTest.php Class Class source
  Plain text file PolicyTest.php Class Class source
  Plain text file StrengthCheckerTest.php Class Class source
  Plain text file ValidatorTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:98
This week:1
All time:9,810
This week:560Up