Skip to content

Commit

Permalink
Merge pull request #18 from tacovandenbroek/use_sha1_for_password_check
Browse files Browse the repository at this point in the history
Add method that does not make use of the real password
  • Loading branch information
DivineOmega committed Dec 6, 2018
2 parents 3d63dd6 + 35ead79 commit cc48b59
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ If you prefer to avoid using helper functions, the following syntax is also avai
```php
$passwordStatus = (new PasswordExposedChecker())->passwordExposed($password);
```

You can also supply the SHA1 hash instead of the plain text password, by using the following method.

```php
$passwordStatus = (new PasswordExposedChecker())->passwordExposedByHash($hash);
```
11 changes: 9 additions & 2 deletions src/PasswordExposedChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ private function getBundleFromCertainty()
*/
public function passwordExposed($password)
{
$hash = sha1($password);
unset($password);
return $this->passwordExposedByHash(sha1($password));
}

/**
* @param string $hash Hexadecimal SHA-1 hash of the password
*
* @return string (see PasswordStatus)
*/
public function passwordExposedByHash($hash)
{
$cacheKey = substr($hash, 0, 2).'_'.substr($hash, 2, 3);

$cacheItem = $this->cache->getItem($cacheKey);
Expand Down
59 changes: 59 additions & 0 deletions tests/Unit/PasswordExposedByHashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace DivineOmega\PasswordExposed\Tests;

use DivineOmega\PasswordExposed\PasswordExposedChecker;
use DivineOmega\PasswordExposed\PasswordStatus;
use Faker\Factory;
use PHPUnit\Framework\TestCase;

class PasswordExposedByHashTest extends TestCase
{
/** @var PasswordExposedChecker */
private $checker;

protected function setUp()
{
$this->checker = new PasswordExposedChecker();
}

/**
* @return array
*/
public function exposedPasswordHashProvider()
{
return [
[sha1('test')],
[sha1('password')],
[sha1('hunter2')],
];
}

/**
* @dataProvider exposedPasswordHashProvider
*
* @param string $hash
*/
public function testExposedPasswords($hash)
{
$this->assertEquals($this->checker->passwordExposedByHash($hash), PasswordStatus::EXPOSED);
}

public function testNotExposedPasswords()
{
$this->assertEquals(
$this->checker->passwordExposedByHash($this->getPasswordHashUnlikelyToBeExposed()),
PasswordStatus::NOT_EXPOSED
);
}

/**
* @return string
*/
private function getPasswordHashUnlikelyToBeExposed()
{
$faker = Factory::create();

return sha1($faker->words(6, true));
}
}

0 comments on commit cc48b59

Please sign in to comment.