1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Hashing;
|
4
|
|
5
|
use RuntimeException;
|
6
|
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
|
7
|
|
8
|
class BcryptHasher implements HasherContract
|
9
|
{
|
10
|
/**
|
11
|
* Default crypt cost factor.
|
12
|
*
|
13
|
* @var int
|
14
|
*/
|
15
|
protected $rounds = 10;
|
16
|
|
17
|
/**
|
18
|
* Hash the given value.
|
19
|
*
|
20
|
* @param string $value
|
21
|
* @param array $options
|
22
|
* @return string
|
23
|
*
|
24
|
* @throws \RuntimeException
|
25
|
*/
|
26
|
public function make($value, array $options = [])
|
27
|
{
|
28
|
$cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;
|
29
|
|
30
|
$hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);
|
31
|
|
32
|
if ($hash === false) {
|
33
|
throw new RuntimeException('Bcrypt hashing not supported.');
|
34
|
}
|
35
|
|
36
|
return $hash;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Check the given plain value against a hash.
|
41
|
*
|
42
|
* @param string $value
|
43
|
* @param string $hashedValue
|
44
|
* @param array $options
|
45
|
* @return bool
|
46
|
*/
|
47
|
public function check($value, $hashedValue, array $options = [])
|
48
|
{
|
49
|
if (strlen($hashedValue) === 0) {
|
50
|
return false;
|
51
|
}
|
52
|
|
53
|
return password_verify($value, $hashedValue);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Check if the given hash has been hashed using the given options.
|
58
|
*
|
59
|
* @param string $hashedValue
|
60
|
* @param array $options
|
61
|
* @return bool
|
62
|
*/
|
63
|
public function needsRehash($hashedValue, array $options = [])
|
64
|
{
|
65
|
$cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;
|
66
|
|
67
|
return password_needs_rehash($hashedValue, PASSWORD_BCRYPT, ['cost' => $cost]);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Set the default password work factor.
|
72
|
*
|
73
|
* @param int $rounds
|
74
|
* @return $this
|
75
|
*/
|
76
|
public function setRounds($rounds)
|
77
|
{
|
78
|
$this->rounds = (int) $rounds;
|
79
|
|
80
|
return $this;
|
81
|
}
|
82
|
}
|