@@ -33,16 +33,16 @@ class Blowfish implements HasherInterface
3333 /**
3434 * Work factor for blowfish
3535 *
36+ * Defaults to '15' (32768 iterations)
37+ *
3638 * @var int
3739 **/
3840 private $ workFactor = 15 ;
3941
4042 /**
41- * Constructor
43+ * Detect Blowfish support
4244 *
4345 * @throws RuntimeException
44- *
45- * @return void
4646 **/
4747 public function __construct ()
4848 {
@@ -66,7 +66,7 @@ public function getWorkFactor()
6666 /**
6767 * Set the blowfish work factor
6868 *
69- * @param int $workFactor
69+ * @param int $workFactor
7070 * @return Blowfish
7171 */
7272 public function setWorkFactor ($ workFactor )
@@ -76,7 +76,7 @@ public function setWorkFactor($workFactor)
7676 'Work factor needs to be greater than 3 and smaller than 32 '
7777 );
7878 }
79- $ this ->workFactor = $ workFactor ;
79+ $ this ->workFactor = ( int ) $ workFactor ;
8080
8181 return $ this ;
8282 }
@@ -97,7 +97,7 @@ public function hash($data, $salt)
9797 * Generate a bcrypt salt from a string salt
9898 *
9999 * @param string $salt
100- * @return string
100+ * @return string Format: "$2y$[workfactor]$[salt]$"
101101 **/
102102 private function bcryptSalt ($ salt )
103103 {
@@ -112,17 +112,22 @@ private function bcryptSalt($salt)
112112 }
113113
114114 /**
115- * Get valid salt substr for blowfish
115+ * Get valid salt string for Blowfish usage
116116 *
117- * Blowfish accepts 22 chars as a salt
118- *
119- * Will take a hash of $salt to take changes over 22 chars into account
117+ * Blowfish accepts 22 chars (./0-9A-Za-z) as a salt if anything else is passed,
118+ * this method will take a hash of $salt to transform it into 22 supported characters
120119 *
121120 * @param string $salt
122121 * @return string
123122 **/
124123 private static function getSaltSubstr ($ salt )
125124 {
125+ // Return salt when it is a valid Blowfish salt
126+ if (preg_match ('!^[\./0-9A-Za-z]{22}$! ' , $ salt ) === 1 ) {
127+ return $ salt ;
128+ }
129+
130+ // fallback to md5() to make the salt valid
126131 return substr (
127132 md5 ($ salt ),
128133 0 , 22
0 commit comments