|
| 1 | +# How to Strengthen the Password |
| 2 | + |
| 3 | +Shield allows you to customize password-related settings to make your passwords more secure. |
| 4 | + |
| 5 | +## Minimum Password Length |
| 6 | + |
| 7 | +The most important factor when it comes to passwords is the number of characters in the password. |
| 8 | +You can check password strength with [Password Strength Testing Tool](https://bitwarden.com/password-strength/). |
| 9 | +Short passwords may be cracked in less than one day. |
| 10 | + |
| 11 | +In Shield, you can set the users' minimum password length. The setting is |
| 12 | +`$minimumPasswordLength` in `app/Config/Auth.php`. The default value is 8 characters. |
| 13 | +It is the recommended minimum value by NIST. However, some organizations recommend |
| 14 | +12 to 14 characters. |
| 15 | + |
| 16 | +The longer the password, the stronger it is. Consider increasing the value. |
| 17 | + |
| 18 | +> **Note** |
| 19 | +> |
| 20 | +> This checking works when you validate passwords with the `strong_password` |
| 21 | +> validation rule. |
| 22 | +> |
| 23 | +> If you disable `CompositionValidator` (enabled by default) in `$passwordValidators`, |
| 24 | +> this checking will not work. |
| 25 | +
|
| 26 | +## Password Hashing Algorithm |
| 27 | + |
| 28 | +You can change the password hashing algorithm by `$hashAlgorithm` in `app/Config/Auth.php`. |
| 29 | +The default value is `PASSWORD_DEFAULT` that is `PASSWORD_BCRYPT` at the time of writing. |
| 30 | + |
| 31 | +`PASSWORD_BCRYPT` means to create new password hashes using the bcrypt algorithm. |
| 32 | + |
| 33 | +You can use `PASSWORD_ARGON2ID` if your PHP has been compiled with Argon2 support. |
| 34 | + |
| 35 | +### PASSWORD_BCRYPT |
| 36 | + |
| 37 | +`PASSWORD_BCRYPT` has one configuration `$hashCost`. The bigger the cost, hashed passwords will be the stronger. |
| 38 | + |
| 39 | +You can find your appropriate cost with the following code: |
| 40 | + |
| 41 | +```php |
| 42 | +<?php |
| 43 | +/** |
| 44 | + * This code will benchmark your server to determine how high of a cost you can |
| 45 | + * afford. You want to set the highest cost that you can without slowing down |
| 46 | + * you server too much. 8-10 is a good baseline, and more is good if your servers |
| 47 | + * are fast enough. The code below aims for ≤ 50 milliseconds stretching time, |
| 48 | + * which is a good baseline for systems handling interactive logins. |
| 49 | + * |
| 50 | + * From: https://www.php.net/manual/en/function.password-hash.php#refsect1-function.password-hash-examples |
| 51 | + */ |
| 52 | +$timeTarget = 0.05; // 50 milliseconds |
| 53 | + |
| 54 | +$cost = 8; |
| 55 | +do { |
| 56 | + $cost++; |
| 57 | + $start = microtime(true); |
| 58 | + password_hash("test", PASSWORD_BCRYPT, ["cost" => $cost]); |
| 59 | + $end = microtime(true); |
| 60 | +} while (($end - $start) < $timeTarget); |
| 61 | + |
| 62 | +echo "Appropriate Cost Found: " . $cost; |
| 63 | +``` |
| 64 | + |
| 65 | +#### Limitations |
| 66 | + |
| 67 | +There are two limitations when you use `PASSWORD_BCRYPT`: |
| 68 | + |
| 69 | +1. the password will be truncated to a maximum length of 72 bytes. |
| 70 | +2. the password will be truncated at the first NULL byte (`\0`). |
| 71 | + |
| 72 | +##### 72 byte issue |
| 73 | + |
| 74 | +If a user submits a password longer than 72 bytes, the validation error will occur. |
| 75 | +If this behavior is unacceptable, consider: |
| 76 | + |
| 77 | +1. change the hashing algorithm to `PASSWORD_ARGON2ID`. It does not have such a limitation. |
| 78 | + |
| 79 | +##### NULL byte issue |
| 80 | + |
| 81 | +This is because `PASSWORD_BCRYPT` is not binary-safe. Normal users cannot |
| 82 | +send NULL bytes in a password string, so this is not a problem in most cases. |
| 83 | + |
| 84 | +But if this behavior is unacceptable, consider: |
| 85 | + |
| 86 | +1. adding a validation rule to prohibit NULL bytes or control codes. |
| 87 | +2. or change the hashing algorithm to `PASSWORD_ARGON2ID`. It is binary-safe. |
| 88 | + |
| 89 | +### PASSWORD_ARGON2ID |
| 90 | + |
| 91 | +`PASSWORD_ARGON2ID` has three configuration `$hashMemoryCost`, `$hashTimeCost`, |
| 92 | +and `$hashThreads`. |
| 93 | + |
| 94 | +If you use `PASSWORD_ARGON2ID`, you should use PHP's constants: |
| 95 | + |
| 96 | +```php |
| 97 | + public int $hashMemoryCost = PASSWORD_ARGON2_DEFAULT_MEMORY_COST; |
| 98 | + |
| 99 | + public int $hashTimeCost = PASSWORD_ARGON2_DEFAULT_TIME_COST; |
| 100 | + public int $hashThreads = PASSWORD_ARGON2_DEFAULT_THREADS; |
| 101 | +``` |
| 102 | + |
| 103 | +## Maximum Password Length |
| 104 | + |
| 105 | +By default, Shield has the validation rules for maximum password length. |
| 106 | + |
| 107 | +- 72 bytes for PASSWORD_BCRYPT |
| 108 | +- 255 characters for others |
| 109 | + |
| 110 | +You can customize the validation rule. See [Customizing Shield](../customization.md). |
| 111 | + |
| 112 | +## $supportOldDangerousPassword |
| 113 | + |
| 114 | +In `app/Config/Auth.php` there is `$supportOldDangerousPassword`, which is a |
| 115 | +setting for using passwords stored in older versions of Shield that were [vulnerable](https://github.com/codeigniter4/shield/security/advisories/GHSA-c5vj-f36q-p9vg). |
| 116 | + |
| 117 | +This setting is deprecated. If you have this setting set to `true`, you should change |
| 118 | +it to `false` as soon as possible, and remove old hashed password in your database. |
| 119 | + |
| 120 | +> **Note** |
| 121 | +> |
| 122 | +> This setting will be removed in v1.0.0 official release. |
0 commit comments