Password toolkit is a simple library that will help you handling passwords with PHP without any dependencies.
This library is a PHP porting from the "Password toolkit" library available for Node.js.
You can use this library to generate suggested passwords, analyse user provided passwords in order to get a strength score and create a hash that can be stored within the database.
Note that this library require PHP version 7.0 or greater., (*1)
Password analysis
First, you need to create an instance of the "Analyzer" class as following:, (*2)
$analyzer = new PHPPasswordToolBox\Analyzer();
, (*3)
Simple analysis:, (*4)
$analyzer->analyze($password);
, (*5)
Complete analysis:, (*6)
$analyzer->setDictionaryPath('rockyou.txt')->completeAnalysis($password);
, (*7)
Note that the complete analysis require a dictionary containing a list of weak passwords, passwords in this list must be separated by a break line (\n).
You can download dictionaries here.
Both methods will return an associative array containing informations about chars count, keywords and the score., (*8)
Password generation
First, you need to create an instance of the "Generator" class as following:, (*9)
$generator = new PHPPasswordToolBox\Generator();
, (*10)
Random password:, (*11)
$generator->generate(12);
, (*12)
Human readable password generation:, (*13)
$generator->setDictionaryPath('dictionary.txt')->generateHumanReadable(12, 2);
, (*14)
Note that in order to generate human readable passwords you need a dictionary, words in the dictionary must be separated by a break line (\n).
If you are looking for an English word list, give a look here., (*15)
Password hashing
Simple hash generation:, (*16)
PHPPasswordToolBox\Hash::createSimpleHash($password);
, (*17)
More complex hash generation:, (*18)
PHPPasswordToolBox\Hash::createHash($password);
, (*19)
The first method will return the hash as a string, the second one will return an associative array with the hash and its parameters (salts, algorithm, loop number).
If you need to compare a given password and a hash generated with the first method you can use this method:, (*20)
PHPPasswordToolBox\Hash::compareSimpleHash($password, $hash);
, (*21)
While if you used the second method you can do this:, (*22)
PHPPasswordToolBox\Hash::compareHash($password, $hash);
, (*23)
Are you looking for the Node.js version? Give a look here., (*24)