Skip to content

Commit

Permalink
Update code samples
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmaguire committed Feb 14, 2020
1 parent 691be22 commit 0651cb9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ $ composer require mdhearing/aspnetcore-identity
## Usage

``` php
$skeleton = new MDHearing\AspNetCore\Identity();
echo $skeleton->echoPhrase('Hello, League!');
$hasher = new MDHearing\AspNetCore\Identity\PasswordHasher();
$hashedPassword = $hasher->hashPassword('very strong password');
$result = $hasher->verifyHashedPassword($hashedPassword, 'very strong password');
```

## Change log
Expand Down Expand Up @@ -70,15 +71,15 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio

[ico-version]: https://img.shields.io/packagist/v/mdhearing/aspnetcore-identity.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/mdhearing/aspnetcore-identity/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/mdhearing/aspnetcore-identity.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/mdhearing/aspnetcore-identity.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/mdhearingaid/aspnetcore-identity/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/mdhearingaid/aspnetcore-identity.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/mdhearingaid/aspnetcore-identity.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/mdhearing/aspnetcore-identity.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/mdhearing/aspnetcore-identity
[link-travis]: https://travis-ci.org/mdhearing/aspnetcore-identity
[link-scrutinizer]: https://scrutinizer-ci.com/g/mdhearing/aspnetcore-identity/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/mdhearing/aspnetcore-identity
[link-travis]: https://travis-ci.org/mdhearingaid/aspnetcore-identity
[link-scrutinizer]: https://scrutinizer-ci.com/g/mdhearingaid/aspnetcore-identity/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/mdhearingaid/aspnetcore-identity
[link-downloads]: https://packagist.org/packages/mdhearing/aspnetcore-identity
[link-author]: https://github.com/stevenmaguire
[link-contributors]: ../../contributors
4 changes: 2 additions & 2 deletions src/IPasswordHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface IPasswordHasher
*
* @returns A hashed representation of the supplied password.
*/
function HashPassword($password);
function hashPassword($password);

/**
* Returns a PasswordVerificationResult indicating the result of a password hash comparison.
Expand All @@ -24,6 +24,6 @@ function HashPassword($password);
*
* @returns A PasswordVerificationResult indicating the result of a password hash comparison.
*/
function VerifyHashedPassword($hashedPassword, $providedPassword);
function verifyHashedPassword($hashedPassword, $providedPassword);
}

2 changes: 1 addition & 1 deletion src/KeyDerivationPrf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MDHearing\AspNetCore\Identity;

class KeyDerivationPrf
abstract class KeyDerivationPrf
{
/**
* The HMAC algorithm (RFC 2104) using the SHA-1 hash function (FIPS 180-4).
Expand Down
30 changes: 15 additions & 15 deletions src/PasswordHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ public function __construct($compatibilityMode = PasswordHasherCompatibilityMode
*
* @returns A hashed representation of the supplied password for the specified user.
*/
public function HashPassword($password)
public function hashPassword($password)
{
if ($password == null) {
throw new ArgumentNullException('password');
}

if ($this->_compatibilityMode == PasswordHasherCompatibilityMode::IdentityV2) {
return base64_encode(self::HashPasswordV2($password));
return base64_encode(self::hashPasswordV2($password));
} else {
return base64_encode($this->HashPasswordV3($password));
return base64_encode($this->hashPasswordV3($password));
}
}

private static function HashPasswordV2($password)
private static function hashPasswordV2($password)
{
$Pbkdf2Prf = KeyDerivationPrf::HMACSHA1; // default for Rfc2898DeriveBytes
$Pbkdf2IterCount = 1000; // default for Rfc2898DeriveBytes
Expand All @@ -86,7 +86,7 @@ private static function HashPasswordV2($password)
return $outputBytes;
}

private function HashPasswordV3($password)
private function hashPasswordV3($password)
{
$prf = KeyDerivationPrf::HMACSHA256;
$iterCount = $this->_iterCount;
Expand Down Expand Up @@ -119,7 +119,7 @@ private function HashPasswordV3($password)
*
* Implementations of this method should be time consistent.
*/
public function VerifyHashedPassword($hashedPassword, $providedPassword)
public function verifyHashedPassword($hashedPassword, $providedPassword)
{
if ($hashedPassword == null) {
throw new \InvalidArgumentException('hashedPassword is null');
Expand All @@ -138,7 +138,7 @@ public function VerifyHashedPassword($hashedPassword, $providedPassword)

switch (ord($decodedHashedPassword{0})) {
case 0x00:
if (self::VerifyHashedPasswordV2($decodedHashedPassword, $providedPassword)) {
if (self::verifyHashedPasswordV2($decodedHashedPassword, $providedPassword)) {
// This is an old password hash format - the caller needs to rehash if we're not running in an older compat mode.
return ($this->_compatibilityMode == PasswordHasherCompatibilityMode::IdentityV3)
? PasswordVerificationResult::SuccessRehashNeeded
Expand All @@ -149,7 +149,7 @@ public function VerifyHashedPassword($hashedPassword, $providedPassword)

case 0x01:
$embeddedIterCount;
if (self::VerifyHashedPasswordV3($decodedHashedPassword, $providedPassword, $embeddedIterCount)) {
if (self::verifyHashedPasswordV3($decodedHashedPassword, $providedPassword, $embeddedIterCount)) {
// If this hasher was configured with a higher iteration count, change the entry now.
return ($embeddedIterCount < $this->_iterCount)
? PasswordVerificationResult::SuccessRehashNeeded
Expand All @@ -163,7 +163,7 @@ public function VerifyHashedPassword($hashedPassword, $providedPassword)
}
}

private static function VerifyHashedPasswordV2($hashedPassword, $password)
private static function verifyHashedPasswordV2($hashedPassword, $password)
{
$Pbkdf2Prf = KeyDerivationPrf::HMACSHA1; // default for Rfc2898DeriveBytes
$Pbkdf2IterCount = 1000; // default for Rfc2898DeriveBytes
Expand All @@ -185,14 +185,14 @@ private static function VerifyHashedPasswordV2($hashedPassword, $password)
return $actualSubkey === $expectedSubkey;
}

private static function VerifyHashedPasswordV3($hashedPassword, $password, &$iterCount)
private static function verifyHashedPasswordV3($hashedPassword, $password, &$iterCount)
{
$iterCount = 0;

// Read header information
$prf = self::ReadNetworkByteOrder($hashedPassword, 1);
$iterCount = self::ReadNetworkByteOrder($hashedPassword, 5);
$saltLength = self::ReadNetworkByteOrder($hashedPassword, 9);
$prf = self::readNetworkByteOrder($hashedPassword, 1);
$iterCount = self::readNetworkByteOrder($hashedPassword, 5);
$saltLength = self::readNetworkByteOrder($hashedPassword, 9);

// Read the salt: must be >= 128 bits
if ($saltLength < intdiv(128, 8)) {
Expand All @@ -215,15 +215,15 @@ private static function VerifyHashedPasswordV3($hashedPassword, $password, &$ite
return $actualSubkey === $expectedSubkey;
}

private static function WriteNetworkByteOrder(&$buffer, $offset, $value)
private static function writeNetworkByteOrder(&$buffer, $offset, $value)
{
$buffer{$offset} = chr($value >> 24);
$buffer{$offset + 1} = chr(($value >> 16) & 0xFF);
$buffer{$offset + 2} = chr(($value >> 8) & 0xFF);
$buffer{$offset + 3} = chr($value & 0xFF);
}

private static function ReadNetworkByteOrder($buffer, $offset)
private static function readNetworkByteOrder($buffer, $offset)
{
return ord($buffer{$offset}) << 24
| ord($buffer{$offset + 1}) << 16
Expand Down
2 changes: 1 addition & 1 deletion src/PasswordHasherCompatibilityMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Specifies the format used for hashing passwords.
*/
class PasswordHasherCompatibilityMode
abstract class PasswordHasherCompatibilityMode
{
/**
* Indicates hashing passwords in a way that is compatible with ASP.NET Identity versions 1 and 2.
Expand Down
2 changes: 1 addition & 1 deletion src/PasswordVerificationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Specifies the results for password verification.
*/
class PasswordVerificationResult
abstract class PasswordVerificationResult
{
/**
* Indicates password verification failed.
Expand Down
6 changes: 3 additions & 3 deletions tests/PasswordHasherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ public function testSimpleV3Passwords()
foreach ($tests as $test) {
$original = $test;

$test['actual'] = $hasher->VerifyHashedPassword($test['hash'], $test['password']);
$test['actual'] = $hasher->verifyHashedPassword($test['hash'], $test['password']);
$this->assertEquals($test['expected'], $test['actual']);


$test['hash'] = $original['hash'].'+bogus';
$test['password'] = $original['password'];
$test['expected'] = 0;
$test['actual'] = $hasher->VerifyHashedPassword($test['hash'], $test['password']);
$test['actual'] = $hasher->verifyHashedPassword($test['hash'], $test['password']);
$this->assertEquals($test['expected'], $test['actual']);

$test['hash'] = $original['hash'];
$test['password'] = $original['password'].'+bogus';
$test['expected'] = 0;
$test['actual'] = $hasher->VerifyHashedPassword($test['hash'], $test['password']);
$test['actual'] = $hasher->verifyHashedPassword($test['hash'], $test['password']);
$this->assertEquals($test['expected'], $test['actual']);
}
}
Expand Down

0 comments on commit 0651cb9

Please sign in to comment.