16
16
17
17
/**
18
18
* @author Elnur Abdurrakhimov <[email protected] >
19
- * @author Terje Bråten <[email protected] >
19
+ * @author Terje Bråten <[email protected] >
20
20
*/
21
21
class BCryptPasswordEncoder extends BasePasswordEncoder
22
22
{
@@ -33,8 +33,10 @@ class BCryptPasswordEncoder extends BasePasswordEncoder
33
33
private static $ prefix = null ;
34
34
35
35
/**
36
- * @param SecureRandomInterface $secureRandom
37
- * @param int $cost
36
+ * Constructor.
37
+ *
38
+ * @param SecureRandomInterface $secureRandom A SecureRandomInterface instance
39
+ * @param integer $cost The algorithmic cost that should be used
38
40
*
39
41
* @throws \InvalidArgumentException if cost is out of range
40
42
*/
@@ -44,13 +46,12 @@ public function __construct(SecureRandomInterface $secureRandom, $cost)
44
46
45
47
$ cost = (int ) $ cost ;
46
48
if ($ cost < 4 || $ cost > 31 ) {
47
- throw new \InvalidArgumentException ('Cost must be in the range of 4-31 ' );
49
+ throw new \InvalidArgumentException ('Cost must be in the range of 4-31. ' );
48
50
}
49
- $ this ->cost = sprintf (" %02d " , $ cost );
51
+ $ this ->cost = sprintf (' %02d ' , $ cost );
50
52
51
53
if (!self ::$ prefix ) {
52
- self ::$ prefix = '$ ' .(version_compare (phpversion (), '5.3.7 ' , '>= ' )
53
- ? '2y ' : '2a ' ).'$ ' ;
54
+ self ::$ prefix = '$ ' .(version_compare (phpversion (), '5.3.7 ' , '>= ' ) ? '2y ' : '2a ' ).'$ ' ;
54
55
}
55
56
}
56
57
@@ -63,8 +64,7 @@ public function encodePassword($raw, $salt = null)
63
64
return password_hash ($ raw , PASSWORD_BCRYPT , array ('cost ' => $ this ->cost ));
64
65
}
65
66
66
- $ salt = self ::$ prefix .$ this ->cost .'$ ' .
67
- $ this ->encodeSalt ($ this ->getRawSalt ());
67
+ $ salt = self ::$ prefix .$ this ->cost .'$ ' .$ this ->encodeSalt ($ this ->getRawSalt ());
68
68
$ encoded = crypt ($ raw , $ salt );
69
69
if (!is_string ($ encoded ) || strlen ($ encoded ) <= 13 ) {
70
70
return false ;
@@ -91,7 +91,8 @@ public function isPasswordValid($encoded, $raw, $salt = null)
91
91
}
92
92
93
93
/**
94
- * Correctly encode the salt to be used by Bcrypt.
94
+ * Encodes the salt to be used by Bcrypt.
95
+ *
95
96
* The blowfish/bcrypt algorithm used by PHP crypt expects a different
96
97
* set and order of characters than the usual base64_encode function.
97
98
* Regular b64: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
@@ -104,6 +105,7 @@ public function isPasswordValid($encoded, $raw, $salt = null)
104
105
* of entropy.
105
106
*
106
107
* @param bytes $random a string of 16 random bytes
108
+ *
107
109
* @return string Properly encoded salt to use with php crypt function
108
110
*
109
111
* @throws \InvalidArgumentException if string of random bytes is too short
@@ -112,7 +114,7 @@ protected function encodeSalt($random)
112
114
{
113
115
$ len = strlen ($ random );
114
116
if ($ len < 16 ) {
115
- throw new \InvalidArgumentException ('The bcrypt salt needs 16 random bytes ' );
117
+ throw new \InvalidArgumentException ('The bcrypt salt needs 16 random bytes. ' );
116
118
}
117
119
if ($ len > 16 ) {
118
120
$ random = substr ($ random , 0 , 16 );
@@ -121,7 +123,7 @@ protected function encodeSalt($random)
121
123
$ base64raw = str_replace ('+ ' , '. ' , base64_encode ($ random ));
122
124
$ salt128bit = substr ($ base64raw , 0 , 21 );
123
125
$ lastchar = substr ($ base64raw , 21 , 1 );
124
- $ lastchar = strtr ($ lastchar , 'AQgw ' ,'.Oeu ' );
126
+ $ lastchar = strtr ($ lastchar , 'AQgw ' , '.Oeu ' );
125
127
$ salt128bit .= $ lastchar ;
126
128
127
129
return $ salt128bit ;
0 commit comments