Skip to content

Commit 20c0c1b

Browse files
committed
changed needsHash and add CHANGELOG.md
1 parent 85dd430 commit 20c0c1b

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor/
2-
composer.lock
2+
composer.lock
3+
index.php

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Released Notes
2+
3+
## v0.2.0 - (2021-04-20)
4+
5+
### Changed
6+
7+
- Changed `needsHash` method
8+
9+
-----------------------------------------------------------
10+
## v0.1.1 - (2021-04-17)
11+
12+
### Added
13+
14+
- Added license
15+
16+
-----------------------------------------------------------
17+
## v0.1.0 - (2021-04-17)
18+
19+
### Added
20+
21+
- Added project

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use SecurePassword\SecurePassword;
2020
$password = new SecurePassword();
2121
$hash = $password->createHash('my_password');
2222

23+
/** Return string */
2324
var_dump($hash);
2425
```
2526

@@ -55,6 +56,7 @@ To return the information of the created hash, use `$info` as `true`.
5556
```php
5657
$hash = $password->createHash('my_password', true);
5758

59+
/** Return array */
5860
var_dump($hash);
5961
```
6062

@@ -66,6 +68,7 @@ Checks whether the hash in `$hash` is valid. If the hash entered does not match
6668
$hash = $password->createHash('my_password');
6769
$res = $password->verifyHash('my_password', $hash);
6870

71+
/** Return bool */
6972
var_dump($res);
7073
```
7174

@@ -79,11 +82,11 @@ $res = $password->useArgon2()->verifyHash('my_password', $hash);
7982
var_dump($res);
8083
```
8184

82-
If the encryption type has been changed, you can generate a new hash with the new encryption. Use `true` for the last parameter.
85+
If the encryption type has been changed, you can generate a new hash with the new encryption. The `needsHash()` method checks whether the reported hash needs to be regenerated. Otherwise, it will return false.
8386

8487
```php
8588
$hash = $password->useArgon2()->createHash('my_password');
86-
$res = $password->useArgon2()->verifyHash('my_password', $hash, true);
89+
$needs = $password->useDefault()->needsRehash('my_password', $hash);
8790

8891
/** Return bool or string */
8992
var_dump($res);
@@ -128,6 +131,7 @@ Here's a quick little function that will help you determine what cost parameter
128131
$password = new SecurePassword();
129132
$cost = $password->getOptimalBcryptCost();
130133

134+
/** Return int */
131135
var_dump($cost);
132136
```
133137

src/SecurePassword.php

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class SecurePassword extends HashAlgorithm
1717
*/
1818
public function __construct()
1919
{
20+
if (empty($this->algo)) {
21+
$this->algo = self::DEFAULT;
22+
}
23+
2024
$this->setPepper();
2125
}
2226

@@ -52,10 +56,6 @@ public function getPepper(): string
5256
*/
5357
public function createHash(string $password, bool $info = false)
5458
{
55-
if (empty($this->algo)) {
56-
$this->algo = self::DEFAULT;
57-
}
58-
5959
$pwd_peppered = $this->passwordPeppered($password);
6060
$pwd_hashed = password_hash($pwd_peppered, $this->algo, $this->options);
6161

@@ -73,32 +73,25 @@ public function createHash(string $password, bool $info = false)
7373
*
7474
* @param string $password
7575
* @param string $hash
76-
* @param bool $verify_needs_rehash
7776
*
7877
* @return mixed
7978
*/
80-
public function verifyHash(string $password, $hash, bool $verify_needs_rehash = false)
79+
public function verifyHash(string $password, $hash)
8180
{
8281
if (is_array($hash)) {
8382
throw new HashException("You are returning the hash information. Enter 'false' in the 'createHash' method");
8483
}
8584

86-
if (empty($this->algo)) {
87-
$this->algo = self::DEFAULT;
88-
}
89-
9085
$pph_strt = microtime(true);
9186
$pwd_peppered = $this->passwordPeppered($password);
9287

9388
if (password_verify($pwd_peppered, $hash)) {
9489
try {
95-
return $this->needsRehash($password, $hash, $verify_needs_rehash);
90+
return true;
9691
} finally {
9792
$end = (microtime(true) - $pph_strt);
9893
$wait = bcmul((1 - $end), 1000000); // usleep(250000) 1/4 of a second
9994
usleep($wait);
100-
101-
#echo "<br>Execution time:" . (microtime(true) - $pph_strt) . "; ";
10295
}
10396
} else {
10497
return false;
@@ -133,22 +126,17 @@ public function getOptimalBcryptCost(int $min_ms = 250, string $password = "test
133126
*
134127
* @param string $password
135128
* @param string $hash
136-
* @param bool $verify
137129
*
138130
* @return mixed
139131
*/
140-
private function needsRehash(string $password, string $hash, bool $verify)
132+
public function needsRehash(string $password, string $hash)
141133
{
142-
if ($verify == true) {
143-
if (password_needs_rehash($hash, $this->algo)) {
144-
$newHash = $this->createHash($password);
134+
if (password_needs_rehash($hash, $this->algo)) {
135+
$newHash = $this->createHash($password);
145136

146-
return $newHash;
147-
} else {
148-
return true;
149-
}
137+
return $newHash;
150138
} else {
151-
return true;
139+
return false;
152140
}
153141
}
154142

0 commit comments

Comments
 (0)