summaryrefslogtreecommitdiff
path: root/classes/authenticator/PasswordTrait.php
blob: 22976d8525dbb053b1651d72276a527199156441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php


// namespace DevCoder\Authentication\Core;
// 
// use DevCoder\Authentication\UserInterface;

/**
 * Trait PasswordTrait
 * @package DevCoder\Authentication\Core
 */
trait PasswordTrait
{
    private $cost = 10;

    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 > 12) {
            throw new \InvalidArgumentException('Cost must be in the range of 4-31.');
        }
        $this->cost = $cost;
    }
}