moviet /
php-encryption
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Pwsuit - A Password Decoration Library For PHP |
||||
| 4 | * |
||||
| 5 | * @category Password Hash |
||||
| 6 | * @package Rammy Labs |
||||
| 7 | * |
||||
| 8 | * @author Moviet |
||||
| 9 | * @license MIT Public License |
||||
| 10 | * |
||||
| 11 | * @version Build @@version@@ |
||||
| 12 | */ |
||||
| 13 | namespace Moviet\Heavy\Hash; |
||||
| 14 | |||||
| 15 | use \RuntimeException; |
||||
| 16 | use Moviet\Heavy\Speed\Verbal; |
||||
| 17 | use Moviet\Heavy\Exceptions\EqualsException; |
||||
| 18 | |||||
| 19 | class Pwsuit |
||||
| 20 | {
|
||||
| 21 | /** |
||||
| 22 | * @param int $cost |
||||
| 23 | */ |
||||
| 24 | protected static $cost; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * @param int $memory |
||||
| 28 | */ |
||||
| 29 | protected static $memory; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * @param int $time |
||||
| 33 | */ |
||||
| 34 | protected static $time; |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * @param int $thread |
||||
| 38 | */ |
||||
| 39 | protected static $thread; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Set default |
||||
| 43 | */ |
||||
| 44 | const DEFAULT = 'Default'; |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * Require minimum PHP 5.6+ |
||||
| 48 | * |
||||
| 49 | * @throws RuntimeException |
||||
| 50 | */ |
||||
| 51 | public function __construct() |
||||
| 52 | {
|
||||
| 53 | if (version_compare(PHP_VERSION, '5.6.0', '<')) {
|
||||
| 54 | throw new RuntimeException('You must upgrade your PHP version >= 5.6.0');
|
||||
| 55 | } |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | /** |
||||
| 59 | * Create cost length |
||||
| 60 | * |
||||
| 61 | * if it doesn't set by default a cost will set to 14 |
||||
| 62 | * do not set under native default by php |
||||
| 63 | * native default by php set to 10 |
||||
| 64 | * |
||||
| 65 | * @param int $length |
||||
| 66 | * @return array |
||||
| 67 | * |
||||
| 68 | */ |
||||
| 69 | public static function cost(int $length) |
||||
| 70 | {
|
||||
| 71 | static::$cost[Verbal::DEFAULT_COST] = $length; |
||||
| 72 | |||||
| 73 | return new static; |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 74 | } |
||||
| 75 | |||||
| 76 | /** |
||||
| 77 | * Create memory_cost length |
||||
| 78 | * |
||||
| 79 | * This is optional by default memory_cost will set to 1666 |
||||
| 80 | * do not set under native default by php |
||||
| 81 | * native default by php set to 1024 |
||||
| 82 | * |
||||
| 83 | * @param int $length |
||||
| 84 | * @return array |
||||
| 85 | * |
||||
| 86 | */ |
||||
| 87 | public static function memory(int $length) |
||||
| 88 | {
|
||||
| 89 | static::$memory[Verbal::MEMORY_KEY] = $length; |
||||
| 90 | |||||
| 91 | return new static; |
||||
|
0 ignored issues
–
show
|
|||||
| 92 | } |
||||
| 93 | |||||
| 94 | /** |
||||
| 95 | * Create time_cost length |
||||
| 96 | * |
||||
| 97 | * This is optional by default time_cost will set to 6 |
||||
| 98 | * do not set under native default by php |
||||
| 99 | * native default by php set to 2 |
||||
| 100 | * |
||||
| 101 | * @param int $length |
||||
| 102 | * @return array |
||||
| 103 | */ |
||||
| 104 | public function time(int $length) |
||||
| 105 | {
|
||||
| 106 | static::$time[Verbal::TIME_KEY] = $length; |
||||
| 107 | |||||
| 108 | return $this; |
||||
|
0 ignored issues
–
show
|
|||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * Create threads length |
||||
| 113 | * |
||||
| 114 | * This is optional by default threads will set to 6 |
||||
| 115 | * do not set under native default by php |
||||
| 116 | * native default by php set to 2 |
||||
| 117 | * |
||||
| 118 | * @param int $length |
||||
| 119 | * @return array |
||||
| 120 | */ |
||||
| 121 | public function thread(int $length) |
||||
| 122 | {
|
||||
| 123 | static::$thread[Verbal::THREAD_KEY] = $length; |
||||
| 124 | |||||
| 125 | return $this; |
||||
|
0 ignored issues
–
show
|
|||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Check attributes operation |
||||
| 130 | * |
||||
| 131 | * @return array |
||||
| 132 | */ |
||||
| 133 | protected static function getLength() |
||||
| 134 | {
|
||||
| 135 | return isset(static::$cost) ? static::$cost : static::costLength(); |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * Check attributes for eg. password Argon |
||||
| 140 | * |
||||
| 141 | * @return array |
||||
| 142 | */ |
||||
| 143 | protected static function getOptions() |
||||
| 144 | {
|
||||
| 145 | return isset(static::$memory) ? static::hashel() : static::options(); |
||||
| 146 | } |
||||
| 147 | |||||
| 148 | /** |
||||
| 149 | * Generate default values if attributes exist |
||||
| 150 | * eg. Password Argon |
||||
| 151 | * |
||||
| 152 | * @return array |
||||
| 153 | * |
||||
| 154 | */ |
||||
| 155 | protected static function hashel() |
||||
| 156 | {
|
||||
| 157 | return array_merge(static::$memory, static::$time, static::$thread); |
||||
| 158 | } |
||||
| 159 | |||||
| 160 | /** |
||||
| 161 | * Check the current attributes that use for compatible version |
||||
| 162 | * if doesn't set will return to default config |
||||
| 163 | * |
||||
| 164 | * @param string $hashmode |
||||
| 165 | * @param string $key |
||||
| 166 | * @return string |
||||
| 167 | */ |
||||
| 168 | public static function pwHash($hashmode, $key) |
||||
| 169 | {
|
||||
| 170 | $options = ($hashmode !== self::DEFAULT && version_compare(PHP_VERSION, '7.2.0', '>=')) ? static::getOptions() : static::getLength(); |
||||
| 171 | |||||
| 172 | return password_hash($key, Verbal::hashAlgo($hashmode), $options); |
||||
|
0 ignored issues
–
show
Moviet\Heavy\Speed\Verbal::hashAlgo($hashmode) of type string is incompatible with the type integer expected by parameter $algo of password_hash().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 173 | } |
||||
| 174 | |||||
| 175 | /** |
||||
| 176 | * Create new hashed with the old hashed password |
||||
| 177 | * and check for compatible php version |
||||
| 178 | * if match will produce new hashed |
||||
| 179 | * |
||||
| 180 | * @param string $hashmode |
||||
| 181 | * @param string $password |
||||
| 182 | * @param string $datahash |
||||
| 183 | * @return Moviet\Heavy\Exceptions\EqualsException |
||||
|
0 ignored issues
–
show
|
|||||
| 184 | */ |
||||
| 185 | public static function pwRehash($hashmode, $password, $datahash) |
||||
| 186 | {
|
||||
| 187 | $options = ($hashmode !== self::DEFAULT && version_compare(PHP_VERSION, '7.2.0', '>=')) ? static::getOptions() : static::getLength(); |
||||
| 188 | |||||
| 189 | if (password_verify($password, $datahash)) {
|
||||
| 190 | if (password_needs_rehash($datahash, Verbal::hashAlgo($hashmode), $options)) {
|
||||
|
0 ignored issues
–
show
Moviet\Heavy\Speed\Verbal::hashAlgo($hashmode) of type string is incompatible with the type integer expected by parameter $algo of password_needs_rehash().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 191 | return static::pwhash($hashmode, $password); |
||||
|
0 ignored issues
–
show
|
|||||
| 192 | } |
||||
| 193 | |||||
| 194 | } else {
|
||||
| 195 | throw new EqualsException("Your password invalid");
|
||||
| 196 | } |
||||
| 197 | } |
||||
| 198 | |||||
| 199 | /** |
||||
| 200 | * Generate default cost length |
||||
| 201 | * |
||||
| 202 | * @return array |
||||
| 203 | */ |
||||
| 204 | protected static function costLength() |
||||
| 205 | {
|
||||
| 206 | return [Verbal::DEFAULT_COST => Verbal::DEFAULT_COST_LENGTH]; |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | /** |
||||
| 210 | * Generate default memory, time, threads for password Argon |
||||
| 211 | * |
||||
| 212 | * @return array |
||||
| 213 | */ |
||||
| 214 | protected static function options() |
||||
| 215 | {
|
||||
| 216 | return [Verbal::MEMORY_KEY => Verbal::DEFAULT_MEMORY_COST, Verbal::TIME_KEY => Verbal::DEFAULT_TIME_COST, |
||||
| 217 | Verbal::THREAD_KEY => Verbal::DEFAULT_THREAD_LENGTH]; |
||||
| 218 | } |
||||
| 219 | |||||
| 220 | /** |
||||
| 221 | * Generate password verification |
||||
| 222 | * |
||||
| 223 | * @param string $password |
||||
| 224 | * @param string $hash |
||||
| 225 | * @return bool |
||||
| 226 | */ |
||||
| 227 | public static function pwTrust($password, $hash) |
||||
| 228 | {
|
||||
| 229 | return password_verify($password, $hash); |
||||
| 230 | } |
||||
| 231 | |||||
| 232 | /** |
||||
| 233 | * Generate password information |
||||
| 234 | * |
||||
| 235 | * @param string $hash |
||||
| 236 | * @return array |
||||
| 237 | */ |
||||
| 238 | public static function pwInfo($hash) |
||||
| 239 | {
|
||||
| 240 | return password_get_info($hash); |
||||
| 241 | } |
||||
| 242 | } |
||||
| 243 |