Password::needsRehash()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Ds\Authenticate;
4
5
/**
6
 * Class Password
7
 * @package Ds\Authenticate
8
 */
9
class Password implements PasswordInterface
10
{
11
12
    /**
13
     * Verify Password hash.
14
     *
15
     * @param  string $password
16
     * @param  string $hash
17
     * @return bool
18
     * @throws \Exception
19
     */
20 23
    public function verify($password, $hash)
21
    {
22 23
        if (\password_verify($password, $hash)) {
23 13
            return true;
24
        }
25 10
        throw new \Exception('Verify failed. Non matching hash');
26
    }
27
28
    /**
29
     * Check if provided password needs rehashing with new Algorithm.
30
     *
31
     * @param  string $password
32
     * @param  string $hash
33
     * @param  int    $algo
34
     * @param  int    $cost
35
     * @return bool|string
36
     */
37 2
    public function needsRehash($password, $hash, $algo = PASSWORD_DEFAULT, $cost = 12)
38
    {
39 2
        if (\password_needs_rehash($hash, $algo, ['cost' => $cost])) {
40 1
            return $this->hash($password, $algo, $cost);
41
        }
42 1
        return false;
43
    }
44
45
    /**
46
     * Create hash from string.
47
     *
48
     * @param  string $password
49
     * @param  int    $algo
50
     * @param  int    $cost
51
     * @return string
52
     */
53 8
    public function hash($password, $algo = PASSWORD_DEFAULT, $cost = 12)
54
    {
55 8
        return \password_hash($password, $algo, ['cost' => $cost]);
56
    }
57
}
58