diff options
Diffstat (limited to 'core/classes/authentication/Core/PasswordTrait.php')
| -rw-r--r-- | core/classes/authentication/Core/PasswordTrait.php | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/core/classes/authentication/Core/PasswordTrait.php b/core/classes/authentication/Core/PasswordTrait.php new file mode 100644 index 0000000..40a7381 --- /dev/null +++ b/core/classes/authentication/Core/PasswordTrait.php @@ -0,0 +1,38 @@ +<?php +/** Trait PasswordTrait + * + * @package DevCoder\Authentication\Core + * + */ +# namespace DevCoder\Authentication\Core; +# +# use DevCoder\Authentication\UserInterface; + +trait PasswordTrait +{ + + private $cost = 10; // Cost must be in the range of 4-31 + // a cost value in the range of 8-11 + // is a good balance between performance and security + + + public function cryptPassword(string $plainPassword): string + { + return password_hash($plainPassword, PASSWORD_BCRYPT, ['cost' => $this->cost]); + } + + + public function isPasswordValid(UserInterface $user, string $plainPassword): bool + { + return password_verify($plainPassword, $user->getPassword()); + } + + + public function setCost(int $cost): void + { + if ($cost < 4 || $cost > 31) { + throw new \InvalidArgumentException('Cost must be in the range of 4-31.'); + } + $this->cost = $cost; + } +} |
