PhpPassword   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A hash() 0 3 1
A needsRehash() 0 3 1
A verify() 0 6 2
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Password;
9
10
/**
11
 * An {@link IPassword} that uses the various `password_*` functions.
12
 */
13
class PhpPassword implements IPassword {
14
    /**
15
     * @var int One of the `PASSWORD_*` constants supplied to {@link password_hash()}.
16
     */
17
    protected $algorithm;
18
19
    /**
20
     * Initialize an instance of this class.
21
     *
22
     * @param int $algorithm The crypt password to use when hashing passwords.
23
     */
24
    public function __construct($algorithm = PASSWORD_DEFAULT) {
25
        $this->algorithm = $algorithm;
26
    }
27
28
29
    /**
30
     * Hashes a plaintext password.
31
     *
32
     * @param string $password The password to hash.
33
     * @return string Returns the hashed password.
34
     */
35
    public function hash($password) {
36
        return password_hash($password, $this->algorithm);
37
    }
38
39
    /**
40
     * Checks if a given password hash needs to be re-hashed to to a stronger algorithm.
41
     *
42
     * @param string $hash The hash to check.
43
     * @return bool Returns `true`
44
     */
45
    public function needsRehash($hash) {
46
        return password_needs_rehash($hash, $this->algorithm);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function verify($password, $hash) {
53
        if (!$hash) {
54
            return false;
55
        }
56
        return password_verify((string)$password, (string)$hash);
57
    }
58
}
59