Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Hashing;
4
 
5
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
6
use RuntimeException;
7
 
8
class ArgonHasher extends AbstractHasher implements HasherContract
9
{
10
    /**
11
     * The default memory cost factor.
12
     *
13
     * @var int
14
     */
15
    protected $memory = 1024;
16
 
17
    /**
18
     * The default time cost factor.
19
     *
20
     * @var int
21
     */
22
    protected $time = 2;
23
 
24
    /**
25
     * The default threads factor.
26
     *
27
     * @var int
28
     */
29
    protected $threads = 2;
30
 
31
    /**
32
     * Indicates whether to perform an algorithm check.
33
     *
34
     * @var bool
35
     */
36
    protected $verifyAlgorithm = false;
37
 
38
    /**
39
     * Create a new hasher instance.
40
     *
41
     * @param  array  $options
42
     * @return void
43
     */
44
    public function __construct(array $options = [])
45
    {
46
        $this->time = $options['time'] ?? $this->time;
47
        $this->memory = $options['memory'] ?? $this->memory;
48
        $this->threads = $this->threads($options);
49
        $this->verifyAlgorithm = $options['verify'] ?? $this->verifyAlgorithm;
50
    }
51
 
52
    /**
53
     * Hash the given value.
54
     *
55
     * @param  string  $value
56
     * @param  array  $options
57
     * @return string
58
     *
59
     * @throws \RuntimeException
60
     */
61
    public function make($value, array $options = [])
62
    {
63
        $hash = @password_hash($value, $this->algorithm(), [
64
            'memory_cost' => $this->memory($options),
65
            'time_cost' => $this->time($options),
66
            'threads' => $this->threads($options),
67
        ]);
68
 
69
        if (! is_string($hash)) {
70
            throw new RuntimeException('Argon2 hashing not supported.');
71
        }
72
 
73
        return $hash;
74
    }
75
 
76
    /**
77
     * Get the algorithm that should be used for hashing.
78
     *
79
     * @return int
80
     */
81
    protected function algorithm()
82
    {
83
        return PASSWORD_ARGON2I;
84
    }
85
 
86
    /**
87
     * Check the given plain value against a hash.
88
     *
89
     * @param  string  $value
90
     * @param  string  $hashedValue
91
     * @param  array  $options
92
     * @return bool
93
     *
94
     * @throws \RuntimeException
95
     */
96
    public function check($value, $hashedValue, array $options = [])
97
    {
98
        if ($this->verifyAlgorithm && $this->info($hashedValue)['algoName'] !== 'argon2i') {
99
            throw new RuntimeException('This password does not use the Argon2i algorithm.');
100
        }
101
 
102
        return parent::check($value, $hashedValue, $options);
103
    }
104
 
105
    /**
106
     * Check if the given hash has been hashed using the given options.
107
     *
108
     * @param  string  $hashedValue
109
     * @param  array  $options
110
     * @return bool
111
     */
112
    public function needsRehash($hashedValue, array $options = [])
113
    {
114
        return password_needs_rehash($hashedValue, $this->algorithm(), [
115
            'memory_cost' => $this->memory($options),
116
            'time_cost' => $this->time($options),
117
            'threads' => $this->threads($options),
118
        ]);
119
    }
120
 
121
    /**
122
     * Set the default password memory factor.
123
     *
124
     * @param  int  $memory
125
     * @return $this
126
     */
127
    public function setMemory(int $memory)
128
    {
129
        $this->memory = $memory;
130
 
131
        return $this;
132
    }
133
 
134
    /**
135
     * Set the default password timing factor.
136
     *
137
     * @param  int  $time
138
     * @return $this
139
     */
140
    public function setTime(int $time)
141
    {
142
        $this->time = $time;
143
 
144
        return $this;
145
    }
146
 
147
    /**
148
     * Set the default password threads factor.
149
     *
150
     * @param  int  $threads
151
     * @return $this
152
     */
153
    public function setThreads(int $threads)
154
    {
155
        $this->threads = $threads;
156
 
157
        return $this;
158
    }
159
 
160
    /**
161
     * Extract the memory cost value from the options array.
162
     *
163
     * @param  array  $options
164
     * @return int
165
     */
166
    protected function memory(array $options)
167
    {
168
        return $options['memory'] ?? $this->memory;
169
    }
170
 
171
    /**
172
     * Extract the time cost value from the options array.
173
     *
174
     * @param  array  $options
175
     * @return int
176
     */
177
    protected function time(array $options)
178
    {
179
        return $options['time'] ?? $this->time;
180
    }
181
 
182
    /**
183
     * Extract the thread's value from the options array.
184
     *
185
     * @param  array  $options
186
     * @return int
187
     */
188
    protected function threads(array $options)
189
    {
190
        if (defined('PASSWORD_ARGON2_PROVIDER') && PASSWORD_ARGON2_PROVIDER === 'sodium') {
191
            return 1;
192
        }
193
 
194
        return $options['threads'] ?? $this->threads;
195
    }
196
}