-
Notifications
You must be signed in to change notification settings - Fork 421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add compatibility for PHP 5.6's hash_equals
function
#61
Closed
timemachine3030
wants to merge
4
commits into
ircmaxell:master
from
timemachine3030:feature/hash_equals
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2d4129a
Add compatibility for PHP 5.6's `hash_equals` function
timemachine3030 7a89894
Attempting to use the testSuiteLoaderFile directly in phpunit
timemachine3030 a2d54a9
Return false if the input hash is not a string
timemachine3030 510d3da
Checking the hash str length
timemachine3030 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
class HashEquals extends PHPUnit_Framework_TestCase { | ||
|
||
public function testFuncExists() { | ||
$this->assertTrue(function_exists('hash_equals')); | ||
} | ||
public function testExamples() { | ||
$expected = crypt('12345', '$2a$07$usesomesillystringforsalt$'); | ||
$correct = crypt('12345', '$2a$07$usesomesillystringforsalt$'); | ||
$incorrect = crypt('apple', '$2a$07$usesomesillystringforsalt$'); | ||
|
||
$this->assertTrue(hash_equals($expected, $correct)); | ||
$this->assertFalse(hash_equals($expected, $incorrect)); | ||
} | ||
|
||
public function testProvider() { | ||
return array( | ||
array("same", "same", true), | ||
array("not1same", "not2same", false), | ||
array("short", "longer", false), | ||
array("longer", "short", false), | ||
array("", "notempty", false), | ||
array("notempty", "", false), | ||
array("", "", true), | ||
); | ||
} | ||
|
||
public function warningProvider() { | ||
return array( | ||
array(123, "NaN", true), | ||
array("NaN", 123, true), | ||
array(123, 123, true), | ||
array(null, "", true), | ||
array(null, 123, true), | ||
array(null, null, true), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider testProvider | ||
*/ | ||
public function testHashEquals($knownString, $userString, $result) { | ||
$this->assertSame($result, hash_equals($knownString, $userString)); | ||
} | ||
|
||
/** | ||
* @expectedException PHPUnit_Framework_Error_Warning | ||
* @dataProvider warningProvider | ||
*/ | ||
public function testHashEqualsWarning($knownString, $userString, $result) { | ||
$this->assertSame($result, hash_equals($knownString, $userString)); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the following block of code is equivalent to the xor + or loop e.g.
However, it appears to still resolve the side-channel. Just worth examining before merging, I suspect. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally left out the
ord
diff as it is not UTF-8 safe and I wanted to stay future compatible. But it doesn't matter as bcrypt, scrypt, PBKDF2, and SHA2x all generate ASCII safe hashes. Updated.