|
| 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 | +``` |
0 commit comments