Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
java-cheat-sheet
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Manfred Michaelis
java-cheat-sheet
Commits
bee1ff6b
Commit
bee1ff6b
authored
2 years ago
by
Manfred Michaelis
Browse files
Options
Downloads
Patches
Plain Diff
Updated
parent
dc4ca7cd
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/de/michm/timingSafeEqual/SafeEqualTest.java
+42
-27
42 additions, 27 deletions
examples/de/michm/timingSafeEqual/SafeEqualTest.java
with
42 additions
and
27 deletions
examples/de/michm/timingSafeEqual/SafeEqualTest.java
+
42
−
27
View file @
bee1ff6b
...
...
@@ -10,22 +10,43 @@ import java.util.Arrays;
* of user credential comparisons.
*/
public
class
SafeEqualTest
{
public
static
void
main
(
String
[]
args
)
throws
NoSuchAlgorithmException
{
/**
* Test data initialization.
final
static
private
CompareMethods
compare
=
new
CompareMethods
();
public
static
void
main
(
String
[]
args
)
{
try
{
// Collect data from test
// calls of methods
runTests
();
// Print the results in STD OUT.
System
.
out
.
println
(
compare
.
toCSV
());
}
catch
(
NoSuchAlgorithmException
e
)
{
System
.
err
.
println
(
"Fehler: "
+
e
.
getLocalizedMessage
());
System
.
exit
(
1
);
}
}
/**
* Benchmark 3 test cases
* String.equals() - two strings
* Arrays.equals() - two byte arrays that are derived of SHA3-256 hash values
* TimingSafe.equals() - also two byte arrays that are derived of SHA3-256 hash values
*/
static
void
runTests
()
throws
NoSuchAlgorithmException
{
/*
Test data initialization.
*/
final
int
ITERATIONS
=
5
;
final
String
PASSWD
=
"password123"
;
MessageDigest
msgDigest
=
MessageDigest
.
getInstance
(
"SHA3-256"
);
CompareMethods
compare
=
new
CompareMethods
();
Object
[]
params
=
new
Object
[]
{
"user"
,
PASSWD
,
new
UserData
(
"user"
,
PASSWD
)};
/*
*
*
Case 1:
Simple string comparison.
*
*
@param arguments an array of Object with parameters.
*
@return true if username and password equals test data, false otherwise.
*
/*
Case 1:
Simple string comparison.
@param arguments an array of Object with parameters.
@return true if username and password equals test data, false otherwise.
*/
Check
normalUserCredCheck
=
arguments
->
{
String
username
=
(
String
)
arguments
[
0
];
...
...
@@ -35,12 +56,11 @@ public class SafeEqualTest {
return
data
.
USERNAME
.
equals
(
username
)
&&
data
.
PASSWORD
.
equals
(
password
);
};
/**
* Case 2: Comparison of two byte arrays
* from SHA256 hashed strings.
*
* @param arguments an array of Object with parameters.
* @return true if username and hashed password equals test data, false otherwise.
/*
Case 2:
Comparison of two byte arrays from SHA256 hashed strings.
@param arguments an array of Object with parameters.
@return true if username and hashed password equals test data, false otherwise.
*/
Check
hashedUserCredCheck
=
arguments
->
{
String
username
=
(
String
)
arguments
[
0
];
...
...
@@ -50,14 +70,12 @@ public class SafeEqualTest {
return
data
.
USERNAME
.
equals
(
username
)
&&
Arrays
.
equals
(
data
.
HASHED_PASSWORD
,
password
);
};
/**
* Case 3: Comparison of two byte arrays
* from SHA256 hashed strings. Tries to
* execute the lambda expression in a
* fixed amount of time every call.
*
* @param arguments an array of Object with parameters.
* @return true if username and hashed password equals test data, false otherwise.
/*
Case 3:
Comparison of two byte arrays from SHA256 hashed strings. Tries to execute the
lambda expression in a fixed amount of time every call.
@param arguments an array of Object with parameters.
@return true if username and hashed password equals test data, false otherwise.
*/
Check
timingSafeUserCredCheck
=
arguments
->
{
String
username
=
(
String
)
arguments
[
0
];
...
...
@@ -116,9 +134,6 @@ public class SafeEqualTest {
}
x
++;
}
// Print the results in STD OUT.
System
.
out
.
println
(
compare
.
toCSV
());
}
/**
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment