Skip to content

Commit

Permalink
balloong hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
firaja committed Mar 3, 2024
2 parents 43af647 + 27e4f42 commit 9441b43
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/main/java/com/password4j/Argon2Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class Argon2Function extends AbstractHashingFunction

public static final int ARGON2_QWORDS_IN_BLOCK = ARGON2_BLOCK_SIZE / 8;

private ExecutorService service;

private final int iterations;

private final int memory;
Expand Down Expand Up @@ -96,6 +98,11 @@ public class Argon2Function extends AbstractHashingFunction
{
initialBlockMemory[i] = new long[ARGON2_QWORDS_IN_BLOCK];
}

if (parallelism >= 1)
{
service = Executors.newFixedThreadPool(Utils.AVAILABLE_PROCESSORS);
}
}

/**
Expand Down Expand Up @@ -588,8 +595,6 @@ private void fillMemoryBlockSingleThreaded(long[][] blockMemory)

private void fillMemoryBlockMultiThreaded(long[][] blockMemory)
{

ExecutorService service = Executors.newFixedThreadPool(parallelism);
List<Future<?>> futures = new ArrayList<>();

for (int i = 0; i < iterations; i++)
Expand Down Expand Up @@ -621,8 +626,6 @@ private void fillMemoryBlockMultiThreaded(long[][] blockMemory)
}
}
}

service.shutdownNow();
}

private void fillSegment(int pass, int lane, int slice, long[][] blockMemory)
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/password4j/BalloonHashingFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package com.password4j;

import com.password4j.types.Argon2;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand All @@ -42,6 +40,8 @@ public class BalloonHashingFunction extends AbstractHashingFunction

private final String algorithm;

private ExecutorService service;

private final int spaceCost;

private final int timeCost;
Expand All @@ -57,6 +57,10 @@ public class BalloonHashingFunction extends AbstractHashingFunction
this.timeCost = timeCost;
this.parallelism = parallelism;
this.delta = delta;
if (parallelism > 1)
{
this.service = Executors.newFixedThreadPool(Utils.AVAILABLE_PROCESSORS);
}

}

Expand Down Expand Up @@ -119,7 +123,7 @@ protected Hash internalHash(byte[] plainTextPassword, byte[] salt)
}
else if (parallelism > 1)
{
ExecutorService service = Executors.newFixedThreadPool(parallelism);

List<Future<?>> futures = new ArrayList<>();

for (int i = 0; i < parallelism; i++)
Expand Down Expand Up @@ -153,7 +157,6 @@ else if (parallelism > 1)
Thread.currentThread().interrupt();
}

service.shutdownNow();

output = hashFunc(messageDigest, plainTextPassword, salt, output);
}
Expand Down Expand Up @@ -249,6 +252,10 @@ private byte[] hashFunc(MessageDigest md, Object... args)
{
t = Utils.append(t, Utils.intToLittleEndianBytes((Integer) arg, 8));
}
else if (arg instanceof CharSequence)
{
t = Utils.append(t, Utils.fromCharSequenceToBytes((CharSequence) arg));
}
else if (arg instanceof byte[])
{
t = Utils.append(t, (byte[]) arg);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/password4j/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Utils

private static final Pattern STRONG_PATTERN = Pattern.compile("\\s*([\\S&&[^:,]]*)(\\:([\\S&&[^,]]*))?\\s*(\\,(.*))?");

static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

private Utils()
{
//
Expand Down
42 changes: 42 additions & 0 deletions src/test/com/password4j/BalloonHashingFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@

package com.password4j;

import com.password4j.types.Argon2;
import com.password4j.types.Bcrypt;
import org.junit.Assert;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class BalloonHashingFunctionTest
Expand Down Expand Up @@ -70,11 +73,50 @@ public void testInstance()
{
balloonHashingFunction = BalloonHashingFunction.getInstance((String) testVector[2], (Integer) testVector[3], (Integer) testVector[4], (Integer) testVector[5], (Integer) testVector[6]);
Assert.assertEquals(testVector[7], balloonHashingFunction.hash((String) testVector[0], (String) testVector[1]).getResult());
Assert.assertEquals(testVector[7], balloonHashingFunction.hash(((String) testVector[0]).getBytes(), ((String) testVector[1]).getBytes()).getResult());

Assert.assertTrue(balloonHashingFunction.check((String) testVector[0], (String) testVector[7], (String) testVector[1]));
Assert.assertTrue(balloonHashingFunction.check(((String) testVector[0]).getBytes(), ((String) testVector[7]).getBytes(), ((String) testVector[1]).getBytes()));
}

}

@Test
public void testEquality()
{
// GIVEN
String m = "SHA-256";
int i = 2;
int p = 3;
int l = 4;
int v = 5;
BalloonHashingFunction balloonHashingFunction = BalloonHashingFunction.getInstance(m, i, p, l, v);

// THEN
boolean eqNull = balloonHashingFunction.equals(null);
boolean eqClass = balloonHashingFunction.equals(new BcryptFunction(Bcrypt.A, 10));
boolean sameInst = balloonHashingFunction.equals(BalloonHashingFunction.getInstance(m, i, p, l, v));
boolean sameInst2 = balloonHashingFunction.equals(new BalloonHashingFunction(m, i, p, l, v));
String toString = balloonHashingFunction.toString();
int hashCode = balloonHashingFunction.hashCode();
boolean notSameInst1 = balloonHashingFunction.equals(new BalloonHashingFunction("SHA-512", i, p, l, v));
boolean notSameInst2 = balloonHashingFunction.equals(new BalloonHashingFunction(m, i+1, p, l, v));
boolean notSameInst3 = balloonHashingFunction.equals(new BalloonHashingFunction(m, i, p+1, l, v));
boolean notSameInst4 = balloonHashingFunction.equals(new BalloonHashingFunction(m, i, p, l+1, v));
boolean notSameInst6 = balloonHashingFunction.equals(new BalloonHashingFunction(m, i, p, l, v+1));

// END
Assert.assertFalse(eqNull);
Assert.assertFalse(eqClass);
Assert.assertTrue(sameInst);
Assert.assertTrue(sameInst2);
Assert.assertNotEquals(toString, new BalloonHashingFunction(m, i+1, p, l, v).toString());
Assert.assertNotEquals(hashCode, new BalloonHashingFunction(m, i, p, l, v+1).hashCode());
Assert.assertFalse(notSameInst1);
Assert.assertFalse(notSameInst2);
Assert.assertFalse(notSameInst3);
Assert.assertFalse(notSameInst4);
Assert.assertFalse(notSameInst6);
}

}

0 comments on commit 9441b43

Please sign in to comment.