Skip to content

Commit 9869d16

Browse files
committed
init
0 parents  commit 9869d16

File tree

7 files changed

+432
-0
lines changed

7 files changed

+432
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor/
2+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Brenno Duarte de Lima
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# PHP SecurePassword
2+
3+
SecurePassword is a PHP component for creating strong passwords using modern encryption.
4+
5+
## Why use this component?
6+
7+
Unlike just using `password_hash`, SecurePassword adds a secret entry (commonly called a pepper) to make it difficult to break the generated hash.
8+
9+
## Requirements
10+
11+
PHP >= 7.3
12+
13+
## How to use
14+
15+
The code below shows an example for creating the hash. Simply use the `createHash` method by entering your password.
16+
17+
```php
18+
use SecurePassword\SecurePassword;
19+
20+
$password = new SecurePassword();
21+
$hash = $password->createHash('my_hash');
22+
23+
var_dump($hash);
24+
```
25+
26+
## Changing the encryption algorithm
27+
28+
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`.
29+
30+
`useDefault()` will use standard encryption
31+
`useBcrypt()` will use Bcrypt encryption
32+
`useArgon2()` will use Argon2 encryption
33+
`useArgon2(null)` passing `true` will use Argon2d encryption
34+
35+
```php
36+
# standard encryption
37+
$hash = $password->useDefault()->createHash('my_hash');
38+
39+
# Bcrypt encryption
40+
$hash = $password->useBcrypt()->createHash('my_hash');
41+
42+
# Argon2 encryption
43+
$hash = $password->useArgon2()->createHash('my_hash');
44+
45+
# Argon2d encryption (with `true`)
46+
$hash = $password->useArgon2(true)->createHash('my_hash');
47+
```
48+
49+
## Returns information about the given hash
50+
51+
To return the information of the created hash, use `$info` as `true`.
52+
53+
```php
54+
$hash = $password->createHash('my_hash', true);
55+
56+
var_dump($hash);
57+
```
58+
59+
## Verifies that a password matches a hash
60+
61+
Checks whether the hash in `$hash` is valid. If the hash entered does not match the options received in the `createHash` method, it is possible to regenerate a new hash in `$verify_needs_rehash`. This function also makes timing attacks difficult.
62+
63+
```php
64+
$hash = $password->createHash('my_hash');
65+
$res = $password->verifyHash('my_hash', $hash);
66+
67+
var_dump($res);
68+
```
69+
70+
You can change the type of algorithm that will be used to check the hash.
71+
72+
```php
73+
$hash = $password->useArgon2()->createHash('my_hash');
74+
$res = $password->useArgon2()->verifyHash('my_hash', $hash);
75+
76+
/** Return bool */
77+
var_dump($res);
78+
```
79+
80+
If the encryption type has been changed, you can generate a new hash with the new encryption. Use `true` for the last parameter.
81+
82+
```php
83+
$hash = $password->useArgon2()->createHash('my_hash');
84+
$res = $password->useArgon2()->verifyHash('my_hash', $hash, true);
85+
86+
/** Return string|new hash */
87+
var_dump($res);
88+
```
89+
90+
## Adding options
91+
92+
Add options in the `useDefault`, `useBcrypt` and `useArgon2` methods.
93+
94+
- useDefault: default options, use an array.
95+
- useBcrypt: you can change `$cost`. The default is `10`.
96+
- useArgon2: you can change `$memory_cost`, `$time_cost` and `$threads`. The default is the constants `PASSWORD_ARGON2_DEFAULT_MEMORY_COST`, `PASSWORD_ARGON2_DEFAULT_TIME_COST` and `PASSWORD_ARGON2_DEFAULT_THREADS`.
97+
98+
```php
99+
# standard encryption
100+
$hash = $password->useDefault([])->createHash('my_hash');
101+
102+
# Bcrypt encryption
103+
$hash = $password->useBcrypt(10)->createHash('my_hash');
104+
105+
# Argon2 encryption
106+
$hash = $password->useArgon2(false, PASSWORD_ARGON2_DEFAULT_MEMORY_COST, PASSWORD_ARGON2_DEFAULT_TIME_COST, PASSWORD_ARGON2_DEFAULT_THREADS)->createHash('my_hash');
107+
108+
# Argon2d encryption (with `true`)
109+
$hash = $password->useArgon2(true, PASSWORD_ARGON2_DEFAULT_MEMORY_COST, PASSWORD_ARGON2_DEFAULT_TIME_COST, PASSWORD_ARGON2_DEFAULT_THREADS)->createHash('my_hash');
110+
```
111+
112+
## Changing the secret entry (recommended)
113+
114+
It is recommended to change the secret entry (or pepper) that will be added to your password. Use `setPepper` to change.
115+
116+
```php
117+
$password = new SecurePassword();
118+
$password->setPepper('new_pepper');
119+
```
120+
121+
## Getting the ideal encryption cost
122+
123+
Here's a quick little function that will help you determine what cost parameter you should be using for your server to make sure you are within this range.
124+
125+
```php
126+
$password = new SecurePassword();
127+
$cost = $password->getOptimalBcryptCost();
128+
129+
var_dump($cost);
130+
```

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "brenno-duarte/php-secure-password",
3+
"description": "SecurePassword is a PHP component for creating strong passwords using modern encryption.",
4+
"keywords": [
5+
"php-security",
6+
"php",
7+
"php-password"
8+
],
9+
"require": {
10+
"php": "^7.3|8.0"
11+
},
12+
"autoload": {
13+
"psr-4": {
14+
"SecurePassword\\": "src/"
15+
}
16+
}
17+
}

src/HashAlgorithm.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace SecurePassword;
4+
5+
abstract class HashAlgorithm
6+
{
7+
protected const DEFAULT = PASSWORD_DEFAULT;
8+
protected const BCRYPT = PASSWORD_BCRYPT;
9+
protected const ARGON2I = PASSWORD_ARGON2I;
10+
protected const ARGON2ID = PASSWORD_ARGON2ID;
11+
12+
/**
13+
* @var const
14+
*/
15+
protected $algo;
16+
17+
/**
18+
* @var array
19+
*/
20+
protected array $options = [];
21+
22+
/**
23+
* @return SecurePassword
24+
*/
25+
public function useDefault(array $options = []): SecurePassword
26+
{
27+
$this->options = $options;
28+
$this->algo = self::DEFAULT;
29+
30+
return $this;
31+
}
32+
33+
/**
34+
* @param int $cost
35+
*
36+
* @return SecurePassword
37+
*/
38+
public function useBcrypt(int $cost = 10): SecurePassword
39+
{
40+
$this->options['cost'] = $cost;
41+
$this->algo = self::BCRYPT;
42+
43+
return $this;
44+
}
45+
46+
/**
47+
* @param bool $argon2d
48+
* @param int $memory_cost
49+
* @param int $time_cost
50+
* @param int $threads
51+
*
52+
* @return SecurePassword
53+
*/
54+
public function useArgon2(
55+
bool $use_argon2d = false,
56+
int $memory_cost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
57+
int $time_cost = PASSWORD_ARGON2_DEFAULT_TIME_COST,
58+
int $threads = PASSWORD_ARGON2_DEFAULT_THREADS
59+
): SecurePassword {
60+
$this->options = [
61+
'memory_cost' => $memory_cost,
62+
'time_cost' => $time_cost,
63+
'threads' => $threads
64+
];
65+
66+
if ($use_argon2d == true) {
67+
$this->algo = self::ARGON2ID;
68+
} else {
69+
$this->algo = self::ARGON2I;
70+
}
71+
72+
return $this;
73+
}
74+
}

src/HashException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace SecurePassword;
4+
5+
class HashException extends \Exception
6+
{
7+
8+
}

0 commit comments

Comments
 (0)