|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Safe; |
| 4 | + |
| 5 | +use Safe\Exceptions\PasswordException; |
| 6 | + |
| 7 | +/** |
| 8 | + * password_hash creates a new password hash using a strong one-way hashing |
| 9 | + * algorithm. password_hash is compatible with crypt. |
| 10 | + * Therefore, password hashes created by crypt can be used with |
| 11 | + * password_hash. |
| 12 | + * |
| 13 | + * |
| 14 | + * |
| 15 | + * |
| 16 | + * PASSWORD_DEFAULT - Use the bcrypt algorithm (default as of PHP 5.5.0). |
| 17 | + * Note that this constant is designed to change over time as new and stronger algorithms are added |
| 18 | + * to PHP. For that reason, the length of the result from using this identifier can change over |
| 19 | + * time. Therefore, it is recommended to store the result in a database column that can expand |
| 20 | + * beyond 60 characters (255 characters would be a good choice). |
| 21 | + * |
| 22 | + * |
| 23 | + * |
| 24 | + * |
| 25 | + * PASSWORD_BCRYPT - Use the CRYPT_BLOWFISH algorithm to |
| 26 | + * create the hash. This will produce a standard crypt compatible hash using |
| 27 | + * the "$2y$" identifier. The result will always be a 60 character string. |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * PASSWORD_ARGON2I - Use the Argon2i hashing algorithm to create the hash. |
| 33 | + * This algorithm is only available if PHP has been compiled with Argon2 support. |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * |
| 38 | + * PASSWORD_ARGON2ID - Use the Argon2id hashing algorithm to create the hash. |
| 39 | + * This algorithm is only available if PHP has been compiled with Argon2 support. |
| 40 | + * |
| 41 | + * |
| 42 | + * |
| 43 | + * |
| 44 | + * |
| 45 | + * |
| 46 | + * |
| 47 | + * salt (string) - to manually provide a salt to use when hashing the password. |
| 48 | + * Note that this will override and prevent a salt from being automatically generated. |
| 49 | + * |
| 50 | + * |
| 51 | + * If omitted, a random salt will be generated by password_hash for |
| 52 | + * each password hashed. This is the intended mode of operation. |
| 53 | + * |
| 54 | + * |
| 55 | + * |
| 56 | + * The salt option has been deprecated as of PHP 7.0.0. It is now |
| 57 | + * preferred to simply use the salt that is generated by default. |
| 58 | + * |
| 59 | + * |
| 60 | + * |
| 61 | + * |
| 62 | + * |
| 63 | + * cost (integer) - which denotes the algorithmic cost that should be used. |
| 64 | + * Examples of these values can be found on the crypt page. |
| 65 | + * |
| 66 | + * |
| 67 | + * If omitted, a default value of 10 will be used. This is a good |
| 68 | + * baseline cost, but you may want to consider increasing it depending on your hardware. |
| 69 | + * |
| 70 | + * |
| 71 | + * |
| 72 | + * |
| 73 | + * |
| 74 | + * |
| 75 | + * |
| 76 | + * memory_cost (integer) - Maximum memory (in kibibytes) that may |
| 77 | + * be used to compute the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_MEMORY_COST. |
| 78 | + * |
| 79 | + * |
| 80 | + * |
| 81 | + * |
| 82 | + * time_cost (integer) - Maximum amount of time it may |
| 83 | + * take to compute the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_TIME_COST. |
| 84 | + * |
| 85 | + * |
| 86 | + * |
| 87 | + * |
| 88 | + * threads (integer) - Number of threads to use for computing |
| 89 | + * the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_THREADS. |
| 90 | + * |
| 91 | + * |
| 92 | + * |
| 93 | + * |
| 94 | + * @param string $password The user's password. |
| 95 | + * |
| 96 | + * Using the PASSWORD_BCRYPT as the |
| 97 | + * algorithm, will result |
| 98 | + * in the password parameter being truncated to a |
| 99 | + * maximum length of 72 characters. |
| 100 | + * @param int|string|null $algo A password algorithm constant denoting the algorithm to use when hashing the password. |
| 101 | + * @param array $options An associative array containing options. See the password algorithm constants for documentation on the supported options for each algorithm. |
| 102 | + * |
| 103 | + * If omitted, a random salt will be created and the default cost will be |
| 104 | + * used. |
| 105 | + * @return string Returns the hashed password. |
| 106 | + * |
| 107 | + * The used algorithm, cost and salt are returned as part of the hash. Therefore, |
| 108 | + * all information that's needed to verify the hash is included in it. This allows |
| 109 | + * the password_verify function to verify the hash without |
| 110 | + * needing separate storage for the salt or algorithm information. |
| 111 | + * @throws PasswordException |
| 112 | + * @deprecated The Safe version of this function is no longer needed in PHP 8.0+ |
| 113 | + * |
| 114 | + */ |
| 115 | +function password_hash(string $password, $algo, array $options = null): string |
| 116 | +{ |
| 117 | + error_clear_last(); |
| 118 | + if ($options !== null) { |
| 119 | + $result = \password_hash($password, $algo, $options); |
| 120 | + } else { |
| 121 | + $result = \password_hash($password, $algo); |
| 122 | + } |
| 123 | + if ($result === false) { |
| 124 | + throw PasswordException::createFromPhpError(); |
| 125 | + } |
| 126 | + return $result; |
| 127 | +} |
0 commit comments