Test Failed
Pull Request — master (#160)
by Maximo
07:08
created

Password::needsRehash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Hashing;
6
7
use Canvas\Models\Users;
8
9
class Password
10
{
11
    /**
12
     * The default cost factor.
13
     *
14
     * @var int
15
     */
16
    protected static $rounds = 12;
17
18
    /**
19
     * Has for the user password.
20
     *
21
     * @param string $password
22
     * @return string
23
     */
24
    public static function make(string $password) : string
25
    {
26
        $options = [
27
            //'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM), // Never use a static salt or one that is not randomly generated.
28
            'cost' => self::$rounds, // the default cost is 10
29
        ];
30
31
        return  password_hash($password, PASSWORD_DEFAULT, $options);
32
    }
33
34
    /**
35
     * Check the given plain value against a hash.
36
     *
37
     * @param  string  $value
38
     * @param  string  $hashedValue
39
     * @param  array  $options
40
     * @return bool
41
     */
42
    public static function check($value, $hashedValue, array $options = []): bool
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
    public static function check($value, $hashedValue, /** @scrutinizer ignore-unused */ array $options = []): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        if (strlen($hashedValue) === 0) {
45
            return false;
46
        }
47
48
        return password_verify($value, $hashedValue);
49
    }
50
51
    /**
52
     * Check if the user password needs to ve rehash.
53
     *
54
     * @param string $password
55
     * @return boolean
56
     */
57
    public static function needsRehash(string $password) : bool
58
    {
59
        $options = [
60
            //'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM), // Never use a static salt or one that is not randomly generated.
61
            'cost' => self::$rounds, // the default cost is 10
62
        ];
63
64
        return password_needs_rehash($password, PASSWORD_DEFAULT, $options);
65
    }
66
67
    /**
68
     * Given any entity with password , verify if the password need rehash and update it.
69
     *
70
     * @param string $password
71
     * @param object $entity
72
     * @return boolean
73
     */
74
    public static function rehash(string $password, object $entity) : bool
75
    {
76
        if (self::needsRehash($entity->password)) {
77
            $entity->password = self::make($password);
78
            $entity->updateOrFail();
79
80
            return true;
81
        }
82
83
        return false;
84
    }
85
}
86