Skip to content

Commit

Permalink
Merge pull request #8 from jossemarGT/feature/synchronized-hasher-engine
Browse files Browse the repository at this point in the history
Synchronize signature hasher engine
  • Loading branch information
jossemarGT authored May 20, 2018
2 parents 0d8e2c5 + 9f0d1f9 commit 43b9ded
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
testImplementation 'org.mockito:mockito-core:2.8+'
testImplementation 'org.powermock:powermock-module-junit4:1.7+'
testImplementation 'org.powermock:powermock-api-mockito2:1.7+'
testImplementation 'net.jodah:concurrentunit:0.4.+'
testImplementation 'pl.pragmatists:JUnitParams:1.1.1'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public abstract class SignatureHasher {
/** The hasher instance from {@link Mac}. */
protected Mac hasher;

/** The initialized variable flags the hasher instance state. **/
protected boolean initialized = false;

/**
* Instantiates a new signature hasher with a secret key String and a
* un-initialized {@link Mac} instance.
Expand All @@ -64,11 +67,21 @@ public SignatureHasher(String secret) {
* @return the formatted hexadecimal string
*/
public String computeSignature(String... values) {
for (String v : values) {
hasher.update(v.getBytes(StandardCharsets.UTF_8));
if (!initialized) {
throw new IllegalStateException("Un-initialized signature hasher");
}

byte[] result;

synchronized (hasher) {
for (String v : values) {
hasher.update(v.getBytes(StandardCharsets.UTF_8));
}

result = hasher.doFinal();
}

return toHexString(hasher.doFinal());
return toHexString(result);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void init() throws InvalidKeyException {
e.printStackTrace();
} finally {
hasher.init(signingKey);
this.initialized = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void init() throws InvalidKeyException {
e.printStackTrace();
} finally {
hasher.init(signingKey);
this.initialized = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@
import static org.junit.Assert.fail;

import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.jossemargt.cookietwist.signature.SignatureHasher;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import net.jodah.concurrentunit.Waiter;

@RunWith(JUnitParamsRunner.class)
public class Sha1SignatureHasherTest {
Expand Down Expand Up @@ -85,4 +91,32 @@ private Object parametersForTestInit() {

}

@Test
public void concurrentTest() throws TimeoutException, InvalidKeyException {
final Waiter waiter = new Waiter();
final String[][] valueGrid = new String[][] {
new String[] { "eaf80caf307333d1c148b9d7dd226689300389c8", "value", "|", "1521518443" },
new String[] { "45237e4af472bdbb4ca1eda4767374dead8bc1d6", "value1", "value2", "value3" },
new String[] { "eaf80caf307333d1c148b9d7dd226689300389c8", "value", "|", "1521518443" },
new String[] { "45237e4af472bdbb4ca1eda4767374dead8bc1d6", "value1", "value2", "value3" },
new String[] { "eaf80caf307333d1c148b9d7dd226689300389c8", "value", "|", "1521518443" },
new String[] { "45237e4af472bdbb4ca1eda4767374dead8bc1d6", "value1", "value2", "value3" },
new String[] { "eaf80caf307333d1c148b9d7dd226689300389c8", "value", "|", "1521518443" },
new String[] { "45237e4af472bdbb4ca1eda4767374dead8bc1d6", "value1", "value2", "value3" } };

final SignatureHasher subject = new Sha1SignatureHasher(secretkey);
subject.init();

for (String[] values : valueGrid) {
new Thread(() -> {
String expectedSignature = values[0];
String computedSignature = subject.computeSignature(Arrays.copyOfRange(values, 1, values.length));
waiter.assertEquals(expectedSignature, computedSignature);
waiter.resume();
}).start();
}

waiter.await(10, TimeUnit.SECONDS, valueGrid.length);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@
import static org.junit.Assert.fail;

import java.security.InvalidKeyException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.jossemargt.cookietwist.signature.SignatureHasher;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import net.jodah.concurrentunit.Waiter;

@RunWith(JUnitParamsRunner.class)
public class Sha256SignatureHasherTest {
Expand Down Expand Up @@ -86,4 +92,32 @@ private Object parametersForTestInit() {

}

@Test
public void concurrentTest() throws TimeoutException, InvalidKeyException {
final Waiter waiter = new Waiter();
final String[][] valueGrid = new String[][] {
new String[] { "09958263c46fde29c341ece86496314d7680eee3df9a04e7845b2bc8c16e3792", "1", "2", "3" },
new String[] { "552c8e8474e35f830feff11484e78e36dee95baeed93831a54619413241874fa", "v1", "v2", "v3" },
new String[] { "09958263c46fde29c341ece86496314d7680eee3df9a04e7845b2bc8c16e3792", "1", "2", "3" },
new String[] { "552c8e8474e35f830feff11484e78e36dee95baeed93831a54619413241874fa", "v1", "v2", "v3" },
new String[] { "09958263c46fde29c341ece86496314d7680eee3df9a04e7845b2bc8c16e3792", "1", "2", "3" },
new String[] { "552c8e8474e35f830feff11484e78e36dee95baeed93831a54619413241874fa", "v1", "v2", "v3" },
new String[] { "09958263c46fde29c341ece86496314d7680eee3df9a04e7845b2bc8c16e3792", "1", "2", "3" },
new String[] { "552c8e8474e35f830feff11484e78e36dee95baeed93831a54619413241874fa", "v1", "v2", "v3" } };

final SignatureHasher subject = new Sha256SignatureHasher(secretkey);
subject.init();

for (String[] values : valueGrid) {
new Thread(() -> {
String expectedSignature = values[0];
String computedSignature = subject.computeSignature(Arrays.copyOfRange(values, 1, values.length));
waiter.assertEquals(expectedSignature, computedSignature);
waiter.resume();
}).start();
}

waiter.await(10, TimeUnit.SECONDS, valueGrid.length);
}

}

0 comments on commit 43b9ded

Please sign in to comment.