|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Password.php |
|
4
|
|
|
* |
|
5
|
|
|
* @category AngryBytes |
|
6
|
|
|
* @package Hash |
|
7
|
|
|
* @subpackage Hasher |
|
8
|
|
|
* @copyright Copyright (c) 2007-2016 Angry Bytes BV (http://www.angrybytes.com) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace AngryBytes\Hash\Hasher; |
|
12
|
|
|
|
|
13
|
|
|
use AngryBytes\Hash\HasherInterface; |
|
14
|
|
|
|
|
15
|
|
|
use \InvalidArgumentException; |
|
16
|
|
|
use \RuntimeException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Password Hasher Using Native PHP Hash Methods |
|
20
|
|
|
* |
|
21
|
|
|
* Generate and verify hashes using the `password_*` functions. |
|
22
|
|
|
* The hashing algorithm and salting is handled by these functions. |
|
23
|
|
|
* |
|
24
|
|
|
* @category AngryBytes |
|
25
|
|
|
* @package Hash |
|
26
|
|
|
* @subpackage Hasher |
|
27
|
|
|
*/ |
|
28
|
|
|
class Password implements HasherInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Cost factor for the algorithm |
|
32
|
|
|
* |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $cost; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Password constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param null|int $cost |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($cost = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->setCost($cost); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
* |
|
50
|
|
|
* Supported options in $options array: |
|
51
|
|
|
* - 'salt': Override the salt generated by password_hash() (discouraged) |
|
52
|
|
|
* - 'cost': Override the default cost (not advised) |
|
53
|
|
|
* |
|
54
|
|
|
* @throws RuntimeException If the hashing fails |
|
55
|
|
|
*/ |
|
56
|
|
|
public function hash($data, array $options = []) |
|
57
|
|
|
{ |
|
58
|
|
|
$hash = password_hash($data, PASSWORD_DEFAULT, $this->parsePasswordOptions($options)); |
|
59
|
|
|
if ($hash === false) { |
|
60
|
|
|
throw new RuntimeException('Failed to hash password'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $hash; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function verify($string, $hash, array $options = []) |
|
70
|
|
|
{ |
|
71
|
|
|
return password_verify($string, $hash); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Determine if the password needs to be rehashed based on the hash options |
|
76
|
|
|
* |
|
77
|
|
|
* If true, the password should be rehashed after verification. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $hash |
|
80
|
|
|
* @param array $options Password options, @see hash() |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public function needsRehash($hash, array $options = []) |
|
84
|
|
|
{ |
|
85
|
|
|
return password_needs_rehash($hash, PASSWORD_DEFAULT, $this->parsePasswordOptions($options)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get info for the given hash |
|
90
|
|
|
* |
|
91
|
|
|
* @see password_get_info() |
|
92
|
|
|
* @param string $hash |
|
93
|
|
|
* @return mixed[] |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getInfo($hash) |
|
96
|
|
|
{ |
|
97
|
|
|
return password_get_info($hash); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set cost |
|
102
|
|
|
* |
|
103
|
|
|
* @throws InvalidArgumentException if the cost is too high or low |
|
104
|
|
|
* @param int|null $cost |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setCost($cost) |
|
107
|
|
|
{ |
|
108
|
|
|
if (is_null($cost)) { |
|
109
|
|
|
$this->cost = $cost; |
|
110
|
|
|
|
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($cost < 4 || $cost > 31) { |
|
115
|
|
|
throw new InvalidArgumentException(sprintf( |
|
116
|
|
|
'Cost value "%d" needs to be greater than 3 and smaller than 32', (int) $cost |
|
117
|
|
|
)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$this->cost = (int) $cost; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Parse password options for hash methods |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $options |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
|
|
private function parsePasswordOptions(array $options) |
|
130
|
|
|
{ |
|
131
|
|
|
// Parse options |
|
132
|
|
|
if (!isset($options['cost']) && is_int($this->cost)) { |
|
133
|
|
|
$options['cost'] = $this->cost; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $options; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|