Skip to content

Commit 659ef32

Browse files
committed
Add password hashing info to README.md
1 parent 3c93b35 commit 659ef32

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ Only users with accounts in our application can be authenticated.
1212

1313
## Working with Passwords
1414

15+
### Hashing Passwords
16+
17+
Passwords can be hashed using one of the three mechanisms:
18+
19+
1. PHP's built in `password_*` functions. This is default and recommended method
20+
1. Using PBKDF2
21+
1. Using SHA1
22+
23+
Later two are there for compatibility resons only, so you can transition your hashed passwords to PHP's password management system if you have not done that already. Password manager's `needsRehash()` method will always recommend rehashing for PBKDF2 and SHA1 hashed passwords.
24+
25+
Example:
26+
27+
```php
28+
$manager = new PasswordManager('global salt, if needed');
29+
30+
$hash = $manager->hash('easy to remember, hard to guess');
31+
32+
if ($manager->verify('easy to remember, hard to guess', $hash, PasswordManagerInterface::HASHED_WITH_PHP)) {
33+
print "All good\n";
34+
} else {
35+
print "Not good\n";
36+
}
37+
```
38+
1539
### Password Policy
1640

1741
All passwords are validated against password policies. By default, policy will accept any non-empty string:

src/Password/Manager/PasswordManager.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PasswordManager implements PasswordManagerInterface
2323
/**
2424
* @param string $global_salt
2525
*/
26-
public function __construct($global_salt)
26+
public function __construct($global_salt = '')
2727
{
2828
$this->global_salt = (string) $global_salt;
2929
}

test/src/PasswordManagerTest.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Active Collab Authentication project.
5+
*
6+
* (c) A51 doo <[email protected]>. All rights reserved.
7+
*/
8+
9+
namespace ActiveCollab\Authentication\Test;
10+
11+
use ActiveCollab\Authentication\Password\Manager\PasswordManager;
12+
use ActiveCollab\Authentication\Password\Manager\PasswordManagerInterface;
13+
use ActiveCollab\Authentication\Test\TestCase\TestCase;
14+
15+
/**
16+
* @package ActiveCollab\Authentication\Test
17+
*/
18+
class PasswordManagerTest extends TestCase
19+
{
20+
/**
21+
* @expectedException \InvalidArgumentException
22+
* @expectedExceptionMessage Hashing mechanism 'unknown hash algo' is not supported
23+
*/
24+
public function testVerifyExceptionOnInvalidMechanism()
25+
{
26+
(new PasswordManager())->verify('123', '1234567890', 'unknown hash algo');
27+
}
28+
29+
/**
30+
* @expectedException \InvalidArgumentException
31+
* @expectedExceptionMessage Hashing mechanism 'unknown hash algo' is not supported
32+
*/
33+
public function testHashExceptionOnInvalidMechanism()
34+
{
35+
(new PasswordManager())->hash('123', 'unknown hash algo');
36+
}
37+
38+
/**
39+
* Test if PHP password hashing works as expected.
40+
*/
41+
public function testPhp()
42+
{
43+
$manager = new PasswordManager('salt');
44+
45+
$hash = $manager->hash('123', PasswordManagerInterface::HASHED_WITH_PHP);
46+
47+
$this->assertInternalType('string', $hash);
48+
$this->assertGreaterThan(40, strlen($hash));
49+
50+
$this->assertTrue($manager->verify('123', $hash, PasswordManagerInterface::HASHED_WITH_PHP));
51+
}
52+
53+
/**
54+
* Test if PBKDF2 hashing works as expected.
55+
*/
56+
public function testPbkdf2()
57+
{
58+
$manager = new PasswordManager('salt');
59+
60+
$hash = $manager->hash('123', PasswordManagerInterface::HASHED_WITH_PBKDF2);
61+
62+
$this->assertInternalType('string', $hash);
63+
$this->assertGreaterThan(40, strlen($hash));
64+
$this->assertStringEndsWith('==', $hash);
65+
66+
$this->assertTrue($manager->verify('123', $hash, PasswordManagerInterface::HASHED_WITH_PBKDF2));
67+
}
68+
69+
/**
70+
* Test if SHA1 hashing works as expected.
71+
*/
72+
public function testSha1()
73+
{
74+
$manager = new PasswordManager('salt');
75+
76+
$hash = $manager->hash('123', PasswordManagerInterface::HASHED_WITH_SHA1);
77+
78+
$this->assertInternalType('string', $hash);
79+
$this->assertEquals(40, strlen($hash));
80+
81+
$this->assertTrue($manager->verify('123', $hash, PasswordManagerInterface::HASHED_WITH_SHA1));
82+
}
83+
84+
/**
85+
* Test if PBKDF2 and SHA1 hashed password always recommend rehashing (using PHP password hashing system).
86+
*/
87+
public function testSha1AndPbkdf2NeedRehash()
88+
{
89+
$manager = new PasswordManager('salt');
90+
91+
$this->assertFalse($manager->needsRehash($manager->hash('123', PasswordManagerInterface::HASHED_WITH_PHP), PasswordManagerInterface::HASHED_WITH_PHP));
92+
$this->assertTrue($manager->needsRehash($manager->hash('123', PasswordManagerInterface::HASHED_WITH_PBKDF2), PasswordManagerInterface::HASHED_WITH_PBKDF2));
93+
$this->assertTrue($manager->needsRehash($manager->hash('123', PasswordManagerInterface::HASHED_WITH_SHA1), PasswordManagerInterface::HASHED_WITH_SHA1));
94+
}
95+
}

0 commit comments

Comments
 (0)