Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
4768dcdb
Commit
4768dcdb
authored
Jun 27, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Method `Security::compareString()` extracted
parent
4a47a593
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
13 deletions
+26
-13
Security.php
framework/base/Security.php
+23
-13
SecurityTest.php
tests/unit/framework/base/SecurityTest.php
+3
-0
No files found.
framework/base/Security.php
View file @
4768dcdb
...
...
@@ -184,12 +184,11 @@ class Security extends Component
$calculatedHash
=
hash_hmac
(
$algorithm
,
$pureData
,
$key
);
// timing attack resistant approach:
$diff
=
0
;
for
(
$i
=
0
;
$i
<
StringHelper
::
byteLength
(
$calculatedHash
);
$i
++
)
{
$diff
|=
(
ord
(
$calculatedHash
[
$i
])
^
ord
(
$hash
[
$i
]))
;
if
(
$this
->
compareString
(
$hash
,
$calculatedHash
))
{
return
$pureData
;
}
else
{
return
false
;
}
return
$diff
===
0
?
$pureData
:
false
;
}
else
{
return
false
;
}
...
...
@@ -322,14 +321,7 @@ class Security extends Component
return
false
;
}
// Use a for-loop to compare two strings to prevent timing attacks. See:
// http://codereview.stackexchange.com/questions/13512
$check
=
0
;
for
(
$i
=
0
;
$i
<
$n
;
++
$i
)
{
$check
|=
(
ord
(
$test
[
$i
])
^
ord
(
$hash
[
$i
]));
}
return
$check
===
0
;
return
$this
->
compareString
(
$test
,
$hash
);
}
/**
...
...
@@ -365,4 +357,21 @@ class Security extends Component
return
$salt
;
}
/**
* Performs string comparison using timing attack resistant approach.
* @see http://codereview.stackexchange.com/questions/13512
* @param string $expected string to compare.
* @param string $actual string to compare.
* @return boolean whether strings are equal.
*/
protected
function
compareString
(
$expected
,
$actual
)
{
// timing attack resistant approach:
$diff
=
0
;
for
(
$i
=
0
;
$i
<
StringHelper
::
byteLength
(
$actual
);
$i
++
)
{
$diff
|=
(
ord
(
$actual
[
$i
])
^
ord
(
$expected
[
$i
]));
}
return
$diff
===
0
;
}
}
\ No newline at end of file
tests/unit/framework/base/SecurityTest.php
View file @
4768dcdb
...
...
@@ -24,8 +24,11 @@ class SecurityTest extends TestCase
{
parent
::
setUp
();
$this
->
security
=
new
Security
();
$this
->
security
->
derivationIterations
=
100
;
// speed up test running
}
// Tests :
public
function
testPasswordHash
()
{
$password
=
'secret'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment