Passed
Push — master ( ebf3ed...efb65a )
by Koen
02:15
created

Argon2Hasher::setMemoryCost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace KoenHoeijmakers\LaravelArgon2;
4
5
use RuntimeException;
6
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
7
8
class Argon2Hasher implements HasherContract
9
{
10
    /**
11
     * Default memory cost factor.
12
     *
13
     * @var int
14
     */
15
    protected $memoryCost = 1024;
16
17
    /**
18
     * Default time cost factor.
19
     *
20
     * @var int
21
     */
22
    protected $timeCost = 2;
23
24
    /**
25
     * Default threads factor.
26
     *
27
     * @var int
28
     */
29
    protected $threads = 2;
30
31
    /**
32
     * Hash the given value.
33
     *
34
     * @param  string $value
35
     * @param  array  $options
36
     * @return string
37
     * @throws RuntimeException
38
     */
39
    public function make($value, array $options = [])
40
    {
41
        $memoryCost = $options['memory_cost'] ?? $this->memoryCost;
42
        $timeCost = $options['time_cost'] ?? $this->timeCost;
43
        $threads = $options['threads'] ?? $this->threads;
44
45
        $hash = password_hash($value, PASSWORD_ARGON2I, [
46
            'memory_cost' => $memoryCost,
47
            'time_cost'   => $timeCost,
48
            'threads'     => $threads,
49
        ]);
50
51
        if ($hash === false) {
0 ignored issues
show
introduced by
The condition $hash === false can never be true.
Loading history...
52
            throw new RuntimeException('Argon2i hashing not supported.');
53
        }
54
55
        return $hash;
56
    }
57
58
    /**
59
     * Check the given plain value against a hash.
60
     *
61
     * @param  string $value
62
     * @param  string $hashedValue
63
     * @param  array  $options
64
     * @return bool
65
     */
66
    public function check($value, $hashedValue, array $options = [])
67
    {
68
        if (strlen($hashedValue) === 0) {
69
            return false;
70
        }
71
72
        return password_verify($value, $hashedValue);
73
    }
74
75
    /**
76
     * Check if the given hash has been hashed using the given options.
77
     *
78
     * @param  string $hashedValue
79
     * @param  array  $options
80
     * @return bool
81
     */
82
    public function needsRehash($hashedValue, array $options = [])
83
    {
84
        $memoryCost = $options['memory_cost'] ?? $this->memoryCost;
85
        $timeCost = $options['time_cost'] ?? $this->timeCost;
86
        $threads = $options['threads'] ?? $this->threads;
87
88
        return password_needs_rehash($hashedValue, PASSWORD_ARGON2I, [
89
            'memory_cost' => $memoryCost,
90
            'time_cost'   => $timeCost,
91
            'threads'     => $threads,
92
        ]);
93
    }
94
95
    /**
96
     * Set the default memory cost factor.
97
     *
98
     * @param $memoryCost
99
     * @return $this
100
     */
101
    public function setMemoryCost($memoryCost)
102
    {
103
        $this->memoryCost = (int) $memoryCost;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Set the default time cost factor.
110
     *
111
     * @param $timeCost
112
     * @return $this
113
     */
114
    public function setTimeCost($timeCost)
115
    {
116
        $this->timeCost = (int) $timeCost;
117
118
        return $this;
119
    }
120
121
    /**
122
     * Set the default threads factor.
123
     *
124
     * @param $threads
125
     * @return $this
126
     */
127
    public function setThreads($threads)
128
    {
129
        $this->threads = (int) $threads;
130
131
        return $this;
132
    }
133
}
134