Completed
Push — master ( 847eff...05e202 )
by Sebastian
05:45
created

Password::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authentication;
13
14
/**
15
 * Provide methods for manage password, this class use PHP password hashing function,
16
 * see php documentation for more information.
17
 * <a href="http://php.net/manual/en/book.password.php">http://php.net/manual/en/book.password.php</a>
18
 */
19
class Password
20
{
21
    /**
22
     * @var array An associative array containing options
23
     *
24
     * http://php.net/manual/en/function.password-hash.php
25
     */
26
    protected $options = [
27
        1 => ['cost' => 11],
28
        2 => [
29
            'memory_cost' => 1024,
30
            'time_cost' => 2,
31
            'threads' => 2
32
        ]
33
    ];
34
35
    /**
36
     * @var int Password default algorithm
37
     */
38
    protected $algo = 1;
39
40
    /**
41
     * Class constructor.
42
     * <p>For password algorithm constants see <a href="http://php.net/manual/en/password.constants.php">Password Constants</a>.</p>
43
     * <pre><code class="php">//Options passed to class constructor as ['key' => 'value'] array.
44
     * $password = new Password(PASSWORD_DEFAULT, [
45
     *     'cost' => 11
46
     * ]);
47
     * </code></pre>
48
     *
49
     * @param int   $algo
50
     * @param array $options
51
     */
52 18
    public function __construct(int $algo = PASSWORD_DEFAULT, array $options = [])
53
    {
54 18
        $this->algo = $algo;
55
56 18
        $this->options[$algo] = \array_replace_recursive($this->options[$algo], $options);
57 18
    }
58
59
    /**
60
     * Verifies if a password matches an hash and return the result as boolean.
61
     * <pre><code class="php">$password = new Password();
62
     *
63
     * $storedHash = '$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6';
64
     * $password = 'FooPassword';
65
     *
66
     * $verified = $password->verify($password, $storedHash);
67
     * </code></pre>
68
     *
69
     * @param string $password
70
     * @param string $hash
71
     *
72
     * @return bool True if password match, false if not.
73
     */
74 45
    public function verify(string $password, string $hash): bool
75
    {
76 45
        return \password_verify($password, $hash);
77
    }
78
79
    /**
80
     * Create password hash from the given string and return it.
81
     * <pre><code class="php">$password = new Password();
82
     *
83
     * $hash = $password->hash('FooPassword');
84
     *
85
     * //var_dump result
86
     * //$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6
87
     * var_dump($hash)
88
     * </code></pre>
89
     *
90
     * @param string $password
91
     *
92
     * @return string Hashed password.
93
     */
94 21
    public function hash(string $password): string
95
    {
96 21
        $hash = \password_hash($password, $this->algo, $this->options[$this->algo]);
97
98 21
        return $hash;
99
    }
100
101
    /**
102
     * Checks if the given hash matches the algorithm and the options provided.
103
     * <pre><code class="php">$password = new Password();
104
     *
105
     * $hash = '$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6';
106
     *
107
     * //true if rehash is needed, false if no
108
     * $rehashCheck = $password->needsRehash($hash);
109
     * </code></pre>
110
     *
111
     * @param string $hash
112
     *
113
     * @return bool
114
     */
115 2
    public function needsRehash(string $hash): bool
116
    {
117 2
        return \password_needs_rehash($hash, $this->algo, $this->options[$this->algo]);
118
    }
119
120
    /**
121
     * Returns information about the given hash.
122
     * <pre><code class="php">$password = new Password();
123
     *
124
     * $hash = '$2y$11$cq3ZWO18l68X7pGs9Y1fveTGcNJ/iyehrDZ10BAvbY8LaBXNvnyk6';
125
     *
126
     * $info = $password->getInfo($hash);
127
     *
128
     * //var_dump result
129
     * //[
130
     * //    'algo' => 1,
131
     * //    'algoName' => 'bcrypt',
132
     * //    'options' => [
133
     * //        'cost' => int 11
134
     * //    ]
135
     * //]
136
     * var_dump($info);
137
     * </code></pre>
138
     *
139
     * @param string $hash
140
     *
141
     * @return array
142
     */
143 2
    public function getInfo(string $hash): array
144
    {
145 2
        return \password_get_info($hash);
146
    }
147
}
148