Skip to content

Commit f0f3af2

Browse files
committed
Merge pull request #17 from ivarb/feat-salting
Fixed salting
2 parents bfd9f54 + 3e518cc commit f0f3af2

4 files changed

Lines changed: 44 additions & 17 deletions

File tree

src/AngryBytes/Hash/Hash.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class Hash
4545
*
4646
* @param HasherInterface $hasher
4747
* @param string $salt
48-
* @return void
4948
**/
5049
public function __construct(HasherInterface $hasher, $salt)
5150
{
@@ -97,10 +96,10 @@ public function setSalt($salt)
9796
{
9897
// Make sure it's of sufficient length
9998
if (strlen($salt) < 20) {
100-
throw new InvalidArgumentException('
101-
Provided salt "' . $salt . '" is not long enough.
102-
A minimum length of 20 characters is required
103-
');
99+
throw new InvalidArgumentException(sprintf(
100+
'Provided salt "%s" is not long enough. A minimum length of 20 characters is required',
101+
$salt
102+
));
104103
}
105104

106105
$this->salt = $salt;
@@ -168,7 +167,7 @@ public function shortHash()
168167
* @param string $hash
169168
* @return bool
170169
**/
171-
public function matchesShortHash($compareTo)
170+
public function matchesShortHash()
172171
{
173172
// Full args to method
174173
$args = func_get_args();

src/AngryBytes/Hash/Hasher/Blowfish.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/AngryBytes/Hash/RandomString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function generateBytes($bytes)
3737
}
3838

3939
// Read the required number of bytes
40-
$bytes = fread($fp, $bytes);
40+
$bytes = fread($fp, $bytes);
4141

4242
// Close the file handle
4343
fclose($fp);

tests/AngryBytes/Hash/Test/BlowfishTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,29 @@ public function testWorkFactor()
126126
);
127127
}
128128

129+
/**
130+
* Test salting
131+
**/
132+
public function testSalt()
133+
{
134+
$hasher = $this->createHasher();
135+
$hasher->getHasher()->setWorkFactor(5);
136+
137+
// Test salt with 22 valid characters
138+
$this->assertEquals(
139+
// Pre-generated hash outcome for password 'foo' and given salt
140+
'$2y$05$./A1aaaaaaaaaaaaaaaaaOZW9OJaO6Alj4.ZDbOi6Jrbn.bGZfYRK',
141+
$hasher->getHasher()->hash('foo', './A1aaaaaaaaaaaaaaaaaa')
142+
);
143+
144+
// Test salt with less invalid characters
145+
$this->assertEquals(
146+
// Pre-generated hash outcome for password 'foo' and given salt (md5'ed)
147+
'$2y$05$ceb20772e0c9d240c75ebugm2AOmnuR5.LsdpDZGAjkE1DupDTPFW',
148+
$hasher->getHasher()->hash('foo', 'salt')
149+
);
150+
}
151+
129152
/**
130153
* Create hasher
131154
*

0 commit comments

Comments
 (0)