Skip to content

Commit b99f57f

Browse files
committed
1.0.0
1 parent e202478 commit b99f57f

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Released Notes
22

3+
## v1.0.0 - (2021-10-12)
4+
5+
### Added
6+
7+
- Added settings in construct
8+
9+
### Changed
10+
11+
- Changed HashAlgorithm constant to public
12+
13+
### Fixed
14+
15+
- Fixed bugs
16+
17+
-----------------------------------------------------------
18+
319
## v0.2.0 - (2021-04-20)
420

521
### Changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Unlike just using `password_hash`, SecurePassword adds a secret entry (commonly
88

99
## Requirements
1010

11-
PHP >= 7.3
11+
PHP >= 7.4
1212

1313
## Installing via Composer
1414

@@ -30,6 +30,26 @@ $hash = $password->createHash('my_password');
3030
var_dump($hash);
3131
```
3232

33+
## Settings
34+
35+
You can change encryption settings without using the methods that will be listed below. To do this, enter the following code in the constructor:
36+
37+
```php
38+
use SecurePassword\HashAlgorithm;
39+
40+
$config = [
41+
'algo' => HashAlgorithm::DEFAULT,
42+
'cost' => 10,
43+
'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
44+
'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST,
45+
'threads' => PASSWORD_ARGON2_DEFAULT_THREADS
46+
];
47+
48+
$password = new SecurePassword($config);
49+
```
50+
51+
You can use the following encryptions: `HashAlgorithm::DEFAULT`, `HashAlgorithm::BCRYPT`, `HashAlgorithm::ARGON2I`, `HashAlgorithm::ARGON2ID`.
52+
3353
## Changing the encryption algorithm
3454

3555
You can change the type of algorithm used to generate the hash. It is possible to use `PASSWORD_BCRYPT`,` PASSWORD_ARGON2I`, `PASSWORD_ARGON2ID` and even `PASSWORD_DEFAULT`.
@@ -78,6 +98,8 @@ $res = $password->verifyHash('my_password', $hash);
7898
var_dump($res);
7999
```
80100

101+
**If you are using the settings passed in the constructor then you can ignore the code below.**
102+
81103
You can change the type of algorithm that will be used to check the hash.
82104

83105
```php
@@ -100,6 +122,8 @@ var_dump($res);
100122

101123
## Adding options
102124

125+
**If you are using the settings passed in the constructor then you can ignore the code below.**
126+
103127
Add options in the `useDefault`, `useBcrypt` and `useArgon2` methods.
104128

105129
- useDefault: default options, use an array.

src/HashAlgorithm.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
abstract class HashAlgorithm
66
{
7-
protected const DEFAULT = PASSWORD_DEFAULT;
8-
protected const BCRYPT = PASSWORD_BCRYPT;
9-
protected const ARGON2I = PASSWORD_ARGON2I;
10-
protected const ARGON2ID = PASSWORD_ARGON2ID;
7+
public const DEFAULT = PASSWORD_DEFAULT;
8+
public const BCRYPT = PASSWORD_BCRYPT;
9+
public const ARGON2I = PASSWORD_ARGON2I;
10+
public const ARGON2ID = PASSWORD_ARGON2ID;
1111

1212
/**
13-
* @var const
13+
* @var mixed
1414
*/
1515
protected $algo;
1616

src/HashException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44

55
class HashException extends \Exception
66
{
7-
87
}

src/SecurePassword.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,34 @@ class SecurePassword extends HashAlgorithm
1313
private string $pepper;
1414

1515
/**
16-
* Construct
16+
* @var array
1717
*/
18-
public function __construct()
18+
private array $config = [
19+
"algo" => self::DEFAULT,
20+
"cost" => "",
21+
"memory_cost" => "",
22+
"time_cost" => "",
23+
"threads" => ""
24+
];
25+
26+
/**
27+
* @param array $config
28+
*/
29+
public function __construct(array $config = [])
1930
{
20-
if (empty($this->algo)) {
21-
$this->algo = self::DEFAULT;
31+
if (!empty($config)) {
32+
foreach ($config as $key => $value) {
33+
if (!isset($this->config[$key])) {
34+
throw new HashException("Key '$key' not exists");
35+
} else {
36+
$this->options = $config;
37+
$this->algo = $this->options['algo'];
38+
}
39+
}
40+
} else {
41+
if (empty($this->algo)) {
42+
$this->algo = self::DEFAULT;
43+
}
2244
}
2345

2446
$this->setPepper();
@@ -147,7 +169,7 @@ public function needsRehash(string $password, string $hash)
147169
*/
148170
private function createPepper(string $pepper): string
149171
{
150-
$hash = openssl_encrypt($pepper, "AES-128-CBC", pack('a16', 'secret'), 0, pack('a16', 'secret2'));
172+
$hash = openssl_encrypt($pepper, "AES-128-CBC", pack('a16', 'secure_password_1'), 0, pack('a16', 'secure_password_2'));
151173

152174
return $hash;
153175
}

0 commit comments

Comments
 (0)