|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Blowfish.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\Hash; |
|
14
|
|
|
use AngryBytes\Hash\HasherInterface; |
|
15
|
|
|
|
|
16
|
|
|
use \RuntimeException; |
|
17
|
|
|
use \InvalidArgumentException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Blowfish Hasher |
|
21
|
|
|
* |
|
22
|
|
|
* Generate and verify Blowfish bcrypt/crypt() hashes. |
|
23
|
|
|
* |
|
24
|
|
|
* @see AngryBytes\Hasher\Password For a password hasher |
|
25
|
|
|
* @category AngryBytes |
|
26
|
|
|
* @package Hash |
|
27
|
|
|
* @subpackage Hasher |
|
28
|
|
|
*/ |
|
29
|
|
|
class Blowfish implements HasherInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Work factor for blowfish |
|
33
|
|
|
* |
|
34
|
|
|
* Defaults to '15' (32768 iterations) |
|
35
|
|
|
* |
|
36
|
|
|
* @var int |
|
37
|
|
|
**/ |
|
38
|
|
|
private $workFactor = 15; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Construct |
|
42
|
|
|
* |
|
43
|
|
|
* Detect Blowfish support |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \RuntimeException |
|
46
|
|
|
* @param null|int $workFactor Override workfactor |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($workFactor = null) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!defined("CRYPT_BLOWFISH") || CRYPT_BLOWFISH !== 1) { |
|
51
|
|
|
throw new RuntimeException( |
|
52
|
|
|
'Blowfish hashing not available on this installation' |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (is_int($workFactor)) { |
|
57
|
|
|
$this->setWorkFactor($workFactor); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the blowfish work factor |
|
63
|
|
|
* |
|
64
|
|
|
* @return int |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getWorkFactor() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->workFactor; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set the blowfish work factor |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $workFactor |
|
75
|
|
|
* @return Blowfish |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setWorkFactor($workFactor) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($workFactor < 4 || $workFactor > 31) { |
|
80
|
|
|
throw new InvalidArgumentException( |
|
81
|
|
|
'Work factor needs to be greater than 3 and smaller than 32' |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
$this->workFactor = (int) $workFactor; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritDoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function hash($string, array $options = []) |
|
93
|
|
|
{ |
|
94
|
|
|
$salt = isset($options['salt']) ? $this->bcryptSalt($options['salt']) : null; |
|
95
|
|
|
|
|
96
|
|
|
return crypt($string, $salt); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
* |
|
102
|
|
|
* @see Hash::compare() |
|
103
|
|
|
*/ |
|
104
|
|
|
public function verify($string, $hash, array $options = []) |
|
105
|
|
|
{ |
|
106
|
|
|
return Hash::compare( |
|
107
|
|
|
$this->hash($string, $options), |
|
108
|
|
|
$hash |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Generate a bcrypt salt from a string salt |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $salt |
|
116
|
|
|
* @return string Format: "$2y$[workfactor]$[salt]$" |
|
117
|
|
|
**/ |
|
118
|
|
|
private function bcryptSalt($salt) |
|
119
|
|
|
{ |
|
120
|
|
|
return '$2y$' |
|
121
|
|
|
// Pad workfactor with 0's to the left, max 2 chars long |
|
122
|
|
|
. str_pad($this->getWorkFactor(), 2, '0', STR_PAD_LEFT) |
|
123
|
|
|
// Add salt itself |
|
124
|
|
|
. '$' . |
|
125
|
|
|
self::getSaltSubstr($salt) |
|
126
|
|
|
. '$' |
|
127
|
|
|
; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Get valid salt string for Blowfish usage |
|
132
|
|
|
* |
|
133
|
|
|
* Blowfish accepts 22 chars (./0-9A-Za-z) as a salt if anything else is passed, |
|
134
|
|
|
* this method will take a hash of $salt to transform it into 22 supported characters |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $salt |
|
137
|
|
|
* @return string |
|
138
|
|
|
**/ |
|
139
|
|
|
private static function getSaltSubstr($salt) |
|
140
|
|
|
{ |
|
141
|
|
|
// Return salt when it is a valid Blowfish salt |
|
142
|
|
|
if (preg_match('!^[\./0-9A-Za-z]{22}$!', $salt) === 1) { |
|
143
|
|
|
return $salt; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
// fallback to md5() to make the salt valid |
|
147
|
|
|
return substr( |
|
148
|
|
|
md5($salt), |
|
149
|
|
|
0, 22 |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|