Skip to content

Commit 10dcb10

Browse files
committed
Remove unnecessary hash comparison method
Since PHP 5.6 is our minimum version this can be safely dropped.
1 parent 2547be8 commit 10dcb10

File tree

2 files changed

+1
-70
lines changed

2 files changed

+1
-70
lines changed

src/Signer/Hmac.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,7 @@ public function doVerify($expected, $payload, Key $key)
3232
return false;
3333
}
3434

35-
$callback = function_exists('hash_equals') ? 'hash_equals' : [$this, 'hashEquals'];
36-
37-
return call_user_func($callback, $expected, $this->createHash($payload, $key));
38-
}
39-
40-
/**
41-
* PHP < 5.6 timing attack safe hash comparison
42-
*
43-
* @internal
44-
*
45-
* @param string $expected
46-
* @param string $generated
47-
*
48-
* @return boolean
49-
*/
50-
public function hashEquals($expected, $generated)
51-
{
52-
$expectedLength = strlen($expected);
53-
54-
if ($expectedLength !== strlen($generated)) {
55-
return false;
56-
}
57-
58-
$res = 0;
59-
60-
for ($i = 0; $i < $expectedLength; ++$i) {
61-
$res |= ord($expected[$i]) ^ ord($generated[$i]);
62-
}
63-
64-
return $res === 0;
35+
return hash_equals($expected, $this->createHash($payload, $key));
6536
}
6637

6738
/**

test/unit/Signer/HmacTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -91,44 +91,4 @@ public function doVerifyShouldReturnFalseWhenExpectedHashIsNotString()
9191
{
9292
$this->assertFalse($this->signer->doVerify(false, 'test', new Key('1234')));
9393
}
94-
95-
/**
96-
* @test
97-
*
98-
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
99-
*/
100-
public function hashEqualsShouldReturnFalseWhenExpectedHashHasDifferentLengthThanGenerated()
101-
{
102-
$this->assertFalse($this->signer->hashEquals('123', '1234'));
103-
}
104-
105-
/**
106-
* @test
107-
*
108-
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
109-
*
110-
* @uses Lcobucci\JWT\Signer\Hmac::createHash
111-
* @uses Lcobucci\JWT\Signer\Key
112-
*
113-
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
114-
*/
115-
public function hashEqualsShouldReturnFalseWhenExpectedHashIsDifferentThanGenerated($expected)
116-
{
117-
$this->assertFalse($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('1234'))));
118-
}
119-
120-
/**
121-
* @test
122-
*
123-
* @depends createHashMustReturnAHashAccordingWithTheAlgorithm
124-
*
125-
* @uses Lcobucci\JWT\Signer\Hmac::createHash
126-
* @uses Lcobucci\JWT\Signer\Key
127-
*
128-
* @covers Lcobucci\JWT\Signer\Hmac::hashEquals
129-
*/
130-
public function hashEqualsShouldReturnTrueWhenExpectedHashIsEqualsThanGenerated($expected)
131-
{
132-
$this->assertTrue($this->signer->hashEquals($expected, $this->signer->createHash('test', new Key('123'))));
133-
}
13494
}

0 commit comments

Comments
 (0)