I am using de.mkammerer.argon2 library (Java) to encode a string using Argon2 algorithm. I am using the advanced interface of Argon2 as I need to provide a few config params such as fixed salt and fixed hash length.
The thing I am failing to achieve is getting the value of p with a fixed length of 17 characters (defaultHashLen). Based on the documentation the param is in bytes.(https://javadoc.io/static/de.mkammerer/argon2-jvm/2.8/de/mkammerer/argon2/Argon2Advanced.html#hashAdvanced(int,int,int,byte%5B%5D,byte%5B%5D,int,de.mkammerer.argon2.Argon2Version)).
The encoding is UTF-8.
Is there any way to force Argon2 to return the encoded value with a max length of 17? Is it even possible?
I tried to substring the result but changing the value of defaultHashLen (i.e 17,22 ) makes the p value completely different on each execution (not sure why).
import de.mkammerer.argon2.*;
import java.time.Instant;
import java.util.Base64;
public class PasswordArgon2Jvm {
private static final int defaultSaltLen = "abcdefghijklmnopqrst".getBytes().length;
private static final byte[] saltArray = "abcdefghijklmnopqrst".getBytes();
private static final int defaultHashLen = 17;
private static final int iterations = 3;
private static final int memory = 1258;
private static final int parallelism = 3;
public void hashPasswordWithCustomSalt(String pass){
// public static Argon2Advanced createAdvanced(Argon2Factory.Argon2Types type, int defaultSaltLength, int defaultHashLength) {
Argon2Advanced argon3 = Argon2Factory.createAdvanced(defaultSaltLen, defaultHashLen);
byte[] password = pass.toUpperCase().getBytes();
HashResult hashInBytes = argon3.hashAdvanced(iterations, memory, parallelism, password, saltArray, defaultHashLen, Argon2Version.DEFAULT_VERSION);
String hashWithFixedSalt = hashInBytes.getEncoded();
String lastBit = hashWithFixedSalt.substring(hashWithFixedSalt.lastIndexOf("$")+1);
System.out.println(
pass + " \n" +
hashWithFixedSalt + " \n" +
lastBit + " \n" +
"length: " + lastBit.length() + " \n" +
"decoded: " + Base64.getDecoder().decode(lastBit));
}
public static void main(String[] args) {
PasswordArgon2Jvm pa2jvm = new PasswordArgon2Jvm();
pa2jvm.hashPasswordWithCustomSalt("santiago2");
}
}
output:
santiago2
$argon2i$v=19$m=1258,t=3,p=3$ZFh2dGZIWDRYdGpsV3J0OFh4NEM$2kNm9mxm8XaWcW4KIL+G7Eo
2kNm9mxm8XaWcW4KIL+G7Eo
length: 23
decoded: [B@1a86f2f1
Thanks!