Password : manage all the existing PHP hash types. Allow more salt methods. Add more OOP with the list of the allowed hashes
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5978 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
@@ -6,7 +6,7 @@ class passwordTest extends PHPUnit_Framework_TestCase
|
||||
public function test_cryptPasswd_1 ()
|
||||
{
|
||||
$res = \password::cryptPasswd ("AAA");
|
||||
$this->assertSame (substr ($res, 0, 4), "$2y$");
|
||||
$this->assertSame ($res[0] == "$" && strlen ($res) > 8, true);
|
||||
}
|
||||
|
||||
public function test_cryptPasswd_2 ()
|
||||
@@ -22,6 +22,101 @@ class passwordTest extends PHPUnit_Framework_TestCase
|
||||
// Three passwords : each must have a different result
|
||||
}
|
||||
|
||||
public function test_cryptPasswd_3 ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::cryptPasswd (false);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_MYSQL ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "MYSQL");
|
||||
$this->assertSame ($res, "*5AF9D0EA5F6406FB0EDD0507F81C1D5CEBE8AC9C");
|
||||
}
|
||||
|
||||
public function test_cryptPassword_CRYPT_STD_DES ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "CRYPT_STD_DES");
|
||||
$this->assertSame (strlen ($res), 13);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_CRYPT_EXT_DES ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "CRYPT_EXT_DES");
|
||||
$this->assertSame (strlen ($res), 13);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_CRYPT_MD5 ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "CRYPT_MD5");
|
||||
$this->assertSame (substr ($res, 0, 3) === "$1$" && strlen ($res) === 34,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_CRYPT_BLOWFISH ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "CRYPT_BLOWFISH");
|
||||
$this->assertSame (substr ($res, 0, 4) === "$2y$" && strlen ($res) === 24,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_CRYPT_SHA256 ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "CRYPT_SHA256");
|
||||
$this->assertSame (
|
||||
substr ($res, 0, 10) === "$5\$rounds=" && strlen ($res) === 75,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_CRYPT_SHA512 ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "CRYPT_SHA512");
|
||||
$this->assertSame (
|
||||
substr ($res, 0, 10) === "$6\$rounds=" && strlen ($res) === 118,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_PASSWORD_BCRYPT ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "PASSWORD_BCRYPT");
|
||||
$this->assertSame (
|
||||
substr ($res, 0, 7) === "$2y\$10\$" && strlen ($res) === 60,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_PASSWORD_ARGON2I ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "PASSWORD_ARGON2I");
|
||||
$this->assertSame (
|
||||
substr ($res, 0, 11) === "\$argon2i\$v=" && strlen ($res) === 96,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_PASSWORD_ARGON2ID ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "PASSWORD_ARGON2ID");
|
||||
$this->assertSame (
|
||||
substr ($res, 0, 12) === "\$argon2id\$v=" && strlen ($res) === 97,
|
||||
true);
|
||||
}
|
||||
|
||||
public function test_cryptPassword_UNKNOWN ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$password = new password ();
|
||||
$res = $password->cryptPassword ("AAA", "UNKNOWN");
|
||||
}
|
||||
|
||||
public function test_checkPassword_1 ()
|
||||
{
|
||||
$res = \password::checkPassword ("AAA", "AAA");
|
||||
@@ -30,7 +125,8 @@ class passwordTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
public function test_checkPassword_2 ()
|
||||
{
|
||||
$res = \password::checkPassword ("AAA", \password::cryptPasswd ("AAA"));
|
||||
$crypt = \password::cryptPasswd ("AAA");
|
||||
$res = \password::checkPassword ("AAA", $crypt);
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
|
||||
@@ -46,4 +142,85 @@ class passwordTest extends PHPUnit_Framework_TestCase
|
||||
'$2y$11$Y.E98jbjgDpV61eK..9MT.klzTeg7ulO4WH/B5yA8cAGMIh.zoNXq');
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
|
||||
public function test_checkPassword_invalid1 ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::checkPassword (false,
|
||||
'$2y$11$Y.E98jbjgDpV61eK..9MT.klzTeg7ulO4WH/B5yA8cAGMIh.zoNXq');
|
||||
}
|
||||
|
||||
public function test_checkPassword_invalid2 ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::checkPassword ("AAA", false);
|
||||
}
|
||||
|
||||
public function test_generateASCII_defaultsize ()
|
||||
{
|
||||
$res = \password::generateASCII ();
|
||||
$this->assertSame (strlen ($res), 12);
|
||||
}
|
||||
|
||||
public function test_generateASCII_toobig ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::generateASCII (112);
|
||||
}
|
||||
|
||||
public function test_generateASCII_toosmall ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::generateASCII (0);
|
||||
}
|
||||
|
||||
public function test_generateAlphanum_defaultsize ()
|
||||
{
|
||||
$res = \password::generateAlphanum ();
|
||||
$this->assertSame (strlen ($res), 12);
|
||||
}
|
||||
|
||||
public function test_generateAlphanum_toobig ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::generateAlphanum (112);
|
||||
}
|
||||
|
||||
public function test_generateAlphanum_toosmall ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::generateAlphanum (0);
|
||||
}
|
||||
|
||||
public function test_generateAlphabetical_defaultsize ()
|
||||
{
|
||||
$res = \password::generateAlphabetical ();
|
||||
$this->assertSame (strlen ($res), 12);
|
||||
}
|
||||
|
||||
public function test_generateAlphabetical_toobig ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::generateAlphabetical (112);
|
||||
}
|
||||
|
||||
public function test_generateAlphabetical_toosmall ()
|
||||
{
|
||||
$this->expectException ();
|
||||
$res = \password::generateAlphabetical (0);
|
||||
}
|
||||
|
||||
public function test_listMethods_1 ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->listMethods();
|
||||
$this->assertSame (count ($res) > 7, true);
|
||||
}
|
||||
|
||||
public function test_salt_1 ()
|
||||
{
|
||||
$password = new password ();
|
||||
$res = $password->salt();
|
||||
$this->assertSame (strlen ($res) > 20, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user