Passed
Push — master ( 702623...797654 )
by Alexander
01:45
created

PasswordHasher::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
rs 10
ccs 3
cts 4
cp 0.75
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
namespace Yiisoft\Security;
3
4
/**
5
 * PasswordHasher allows generating password hash and verifying passwords against a hash.
6
 */
7
class PasswordHasher
8
{
9
    private $algorithm;
10
    private $parameters;
11
12
    private const SAFE_PARAMETERS = [
13
        PASSWORD_BCRYPT => [
14
            'cost' => 13,
15
        ],
16
    ];
17
18
    /**
19
     * @see https://www.php.net/manual/en/function.password-hash.php
20
     */
21 1
    public function __construct(int $algorithm = PASSWORD_DEFAULT, array $parameters = null)
22
    {
23 1
        $this->algorithm = $algorithm;
24
25 1
        if ($parameters === null) {
26
            $parameters = self::SAFE_PARAMETERS[$algorithm] ?? null;
27
        }
28 1
        $this->parameters = $parameters;
29 1
    }
30
31
32
    /**
33
     * Generates a secure hash from a password and a random salt.
34
     *
35
     * The generated hash can be stored in database.
36
     * Later when a password needs to be validated, the hash can be fetched and passed
37
     * to {@see validate()}. For example,
38
     *
39
     * ```php
40
     * // generates the hash (usually done during user registration or when the password is changed)
41
     * $hash = (new PasswordHasher())->hash($password);
42
     * // ...save $hash in database...
43
     *
44
     * // during login, validate if the password entered is correct using $hash fetched from database
45
     * if ((new PasswordHasher())->validate($password, $hash)) {
46
     *     // password is good
47
     * } else {
48
     *     // password is bad
49
     * }
50
     * ```
51
     *
52
     * @param string $password The password to be hashed.
53
     * @return string The password hash string. The output length might increase
54
     * in future versions of PHP (http://php.net/manual/en/function.password-hash.php)
55
     * @see validate()
56
     */
57 1
    public function hash(string $password): string
58
    {
59 1
        return password_hash($password, $this->algorithm, $this->parameters);
60
    }
61
62
    /**
63
     * Verifies a password against a hash.
64
     * @param string $password The password to verify.
65
     * @param string $hash The hash to verify the password against.
66
     * @return bool whether the password is correct.
67
     * @throws \InvalidArgumentException on bad password/hash parameters or if crypt() with Blowfish hash is not
68
     * available.
69
     * @see hash()
70
     */
71 1
    public function validate(string $password, string $hash): bool
72
    {
73 1
        if ($password === '') {
74
            throw new \InvalidArgumentException('Password must be a string and cannot be empty.');
75
        }
76
77 1
        return password_verify($password, $hash);
78
    }
79
}
80