|
1
|
|
|
<?php namespace SimpleHash\Calculator; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Bcrypt hash calculator class |
|
5
|
|
|
* |
|
6
|
|
|
* @package SimpleHash |
|
7
|
|
|
* @author Kai Hempel <[email protected]> |
|
8
|
|
|
* @copyright 2016 Kai Hempel <[email protected]> |
|
9
|
|
|
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License |
|
10
|
|
|
* @link https://www.kuweh.de/ |
|
11
|
|
|
* @since Class available since Release 1.0.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
class BcryptCalculator implements HashCalculatorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_COST = '10'; |
|
16
|
|
|
const DEFAULT_SALT = 'Af13GgKoL503sCvf42dJ18'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Costs |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $cost = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Salt |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $salt = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor |
|
34
|
|
|
* |
|
35
|
|
|
* @param array $params |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function __construct(array $params = array()) |
|
38
|
|
|
{ |
|
39
|
4 |
|
$this->setCostFromParams($params); |
|
40
|
4 |
|
$this->setSaltFromParams($params); |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Returns the Bcrypt hash |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $data |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function getHash($data) |
|
50
|
|
|
{ |
|
51
|
3 |
|
return crypt($data, $this->buildSaltString()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Builds the final salt string for PHP crypt. |
|
56
|
|
|
* The blowfish type "$2y$" is used. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
3 |
|
private function buildSaltString() |
|
61
|
|
|
{ |
|
62
|
3 |
|
return '$2y$' . $this->cost . '$' . $this->salt . '$'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Sets the salt string from parameter array. |
|
67
|
|
|
* If no salt is given, the default value will be used! |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
4 |
|
private function setSaltFromParams(array $params) |
|
72
|
|
|
{ |
|
73
|
4 |
|
$salt = $this->getMatchingData($params, '/^[\.\\A-Za-z0-9]{22}$/'); |
|
74
|
4 |
|
$this->salt = (! empty($salt)) ? $salt : self::DEFAULT_SALT; |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sets the cost string from parameter array. |
|
79
|
|
|
* If no cost is given, the default value will be used! |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
4 |
|
private function setCostFromParams(array $params) |
|
84
|
|
|
{ |
|
85
|
4 |
|
$cost = (int)$this->getMatchingData($params, '/^[0-9]{1,2}$/'); |
|
86
|
4 |
|
$this->cost = (! empty($cost)) ? (string)$cost : self::DEFAULT_COST; |
|
87
|
4 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns the first match from parameter array |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $params |
|
93
|
|
|
* @param string $regex |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
4 |
|
private function getMatchingData(array $params, $regex) |
|
97
|
|
|
{ |
|
98
|
4 |
|
foreach ($params as $data) { |
|
99
|
1 |
|
if (preg_match($regex, $data)) { |
|
100
|
1 |
|
return $data; |
|
101
|
|
|
} |
|
102
|
4 |
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
return ''; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|