This repository was archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Method for calculating a value for N depending on CPU speed #12
Open
HungryHobo
wants to merge
4
commits into
wg:master
Choose a base branch
from
HungryHobo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6802634
Add a method for calculating a value for N depending on CPU speed
HungryHobo f96c0c1
Move timedIterations() to SCryptUtil (thanks GrmpCerber)
HungryHobo b2069dc
Refactor, improve comment
HungryHobo 5e3d9a6
Print test parameters on failure
HungryHobo 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,12 @@ | |
package com.lambdaworks.crypto.test; | ||
|
||
import com.lambdaworks.codec.Base64; | ||
import com.lambdaworks.crypto.SCrypt; | ||
import com.lambdaworks.crypto.SCryptUtil; | ||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.ThreadMXBean; | ||
import java.security.GeneralSecurityException; | ||
import java.util.Random; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
@@ -56,4 +61,27 @@ public void format_0_rp_max() throws Exception { | |
assertEquals(r, params >> 8 & 0xff); | ||
assertEquals(p, params >> 0 & 0xff); | ||
} | ||
|
||
@Test | ||
public void testTimedIterations() throws GeneralSecurityException { | ||
byte[] salt = "1234".getBytes(); | ||
int dkLen = 32; | ||
|
||
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); | ||
boolean cpuTimeSupported = threadBean.isCurrentThreadCpuTimeSupported(); | ||
Random random = new Random(); | ||
for (int i=0; i<5; i++) { | ||
int targetDuration = 100 + random.nextInt(900); | ||
int numIterations = SCryptUtil.timedIterations(targetDuration, 8, 1); | ||
long startTime = cpuTimeSupported ? threadBean.getCurrentThreadUserTime() : System.nanoTime(); | ||
SCrypt.scrypt(passwd.getBytes(), salt, numIterations, 8, 1, dkLen); | ||
long endTime = cpuTimeSupported ? threadBean.getCurrentThreadUserTime() : System.nanoTime(); | ||
long actualDuration = (endTime-startTime) / 1000000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you take a time in nano an divide it to a lower resolution ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
|
||
// check that actual duration is within targetDuration - 50% and targetDuration + 60% | ||
String failMessage = "Target duration=" + targetDuration + ", actual=" + actualDuration; | ||
assertTrue(failMessage, actualDuration > targetDuration*0.5); | ||
assertTrue(failMessage, actualDuration < targetDuration*1.6); | ||
} | ||
} | ||
} |
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.
Why is your target duration randomly choosen ?
From what I've been taught this is not really a good practice in unit testing.
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.
Is there anything in particular you're thinking of?
I'd say the benefit is that it covers a wider range of inputs. The downside is that test failures are harder to reproduce.
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.
That's exactly what I mean.
Beside you're not tracking the currently choosen value.
One thing you could do is running the two tests : one with a fixed value and another randomly choosen.
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.
You have a point about the value not being logged. I'll add that.