Skip to content

Commit

Permalink
#143: fix salt generation for Argon2
Browse files Browse the repository at this point in the history
  • Loading branch information
firaja committed Mar 7, 2024
1 parent 33377fd commit ded2c06
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/password4j/Argon2Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private Hash internalHash(byte[] plainTextPassword, byte[] salt, CharSequence pe
initialize(plainTextPassword, salt, Utils.fromCharSequenceToBytes(pepper), null, blockMemory);
fillMemoryBlocks(blockMemory);
byte[] hash = ending(blockMemory);
Hash result = new Hash(this, encodeHash(hash, salt), hash, Utils.fromBytesToString(salt));
Hash result = new Hash(this, encodeHash(hash, salt), hash, salt);
result.setPepper(pepper);
return result;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/password4j/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ private Hash()
* @param bytes the hash without additional information.
* @param salt the salt used for the computation.
* @since 0.1.0
* @deprecated As of 1.8.1 because of the salt conversion from {@link String} to byte[].
* {@link Hash#Hash(HashingFunction, String, byte[], byte[])} should be used instead.
*/
@Deprecated
public Hash(HashingFunction hashingFunction, String result, byte[] bytes, String salt)
{
this(hashingFunction, Utils.fromCharSequenceToBytes(result), bytes, Utils.fromCharSequenceToBytes(salt));
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/password4j/ScryptFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,18 @@ public Hash hash(byte[] plainTextPasswordAsBytes, byte[] salt)

private Hash internalHash(byte[] plainTextPassword, byte[] salt)
{
String stringedSalt = Utils.fromBytesToString(salt);

try
{
byte[] derived = scrypt(plainTextPassword, salt, derivedKeyLength);
String params = Long.toString((long) Utils.log2(workFactor) << 16 | (long) resources << 8 | parallelization, 16);
String sb = "$" + params + '$' + Utils.encodeBase64(salt) + '$'
+ Utils.encodeBase64(derived);
return new Hash(this, sb, derived, stringedSalt);
return new Hash(this, sb, derived, salt);
}
catch (IllegalArgumentException | GeneralSecurityException e)
{
String stringedSalt = Utils.fromBytesToString(salt);
String message = "Invalid specification with salt=" + stringedSalt + ", N=" + workFactor + ", r=" + resources + " and p=" + parallelization;
throw new BadParametersException(message, e);
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/com/password4j/Argon2FunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,28 @@ public void testOWASP()
PropertyReader.properties = oldProps;
}

@Test
public void testWithGeneratedSalt()
{
for (TestCase test : CASES)
{
Argon2Function f = getFunction(test.memory, test.iterations, test.parallelism, test.outLength, test.type, test.version);
Hash hash = Password.hash(test.plainTextPassword).addRandomSalt().with(f);
assertTrue(Password.check(test.plainTextPassword, hash));
}
}

@Test
public void testWithGeneratedSaltAndSetLenght()
{
for (TestCase test : CASES)
{
Argon2Function f = getFunction(test.memory, test.iterations, test.parallelism, test.outLength, test.type, test.version);
Hash hash = Password.hash(test.plainTextPassword).addRandomSalt(128).with(f);
assertTrue(Password.check(test.plainTextPassword, hash));
}
}




Expand Down

0 comments on commit ded2c06

Please sign in to comment.