PasswordHash { public static boolean verify(byte[] password, String argon2Hash) throws IOException { Argon2Hash input = Argon2Hash.parse(argon2Hash); int algorithmId = Argon2Parameters.ARGON2_i; for (Map.Entry<Integer, String> entry: ALGORITHM_NAME_MAP.entrySet()) { if (entry.getValue().equals(input.getAlgorithm())) { algorithmId = entry.getKey(); break; } } Argon2Parameters.Builder builder = new Argon2Parameters.Builder(algorithmId) .withVersion(input.getVersion()) .withIterations(input.getIterations()) .withMemoryAsKB(input.getMemory()) .withParallelism(input.getParallelism()) .withSalt(input.getSalt()); Argon2Parameters parameters = builder.build(); Argon2Hash expectedHash = hash(password, parameters); return input.hashEquals(expectedHash); }  static String hash(byte[] password); static boolean verify(byte[] password, String argon2Hash);  }
@Test public void testArgon2Hashes() throws IOException { System.out.println("Testing Argon2 hash verification using various passwords."); PasswordHash.verify("".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=32768,t=3,p=16$VFo3SnVYSnk$WZrqp36HmoSPeGhVCt5Ly3s9pU1OpnRnJMPjnlVcdy8"); PasswordHash.verify("maC2kBxu".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=32768,t=3,p=16$YndXNDhGV0o$E2cHqAeGlUyO26haRT/a7VPzcSeLbISimR98F7CAooI"); PasswordHash.verify("EbyBt7U4djF6G84Y".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=32768,t=3,p=16$UjN6elpVcGI$cRyh9e5nfu6ToXKR1pbPiFudEenRYYuvgea4hvsTB0Y"); PasswordHash.verify("The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=32768,t=3,p=16$a3c5RzYzOXo$LLM1F2EBBKjuoUc3CRlPNsmGI3vc3mnvNmafao8askc"); PasswordHash.verify("Příliš žluťoučký kůň úpěl ďábelské ódy.".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=32768,t=3,p=16$cTZ5NUtYNFg$ma0FJ8SJ/U1YXZ4pmtOMgF23WfodzNEIizke8zG+Auc"); PasswordHash.verify("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=32768,t=3,p=16$eTZQV0NuVWU$Xi8HXgnunRNBRjH+U5mXEUC7b9uX1JnWVYZtHMzQYmg"); }
@Test public void testArgon2DifferentParameters() throws IOException { System.out.println("Testing Argon2 hash verification using various algorithm parameters."); PasswordHash.verify("password".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=65536,t=2,p=1$c29tZXNhbHQ$wWKIMhR9lyDFvRz9YTZweHKfbftvj+qf+YFY4NeBbtA"); PasswordHash.verify("password".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=256,t=2,p=1$c29tZXNhbHQ$iekCn0Y3spW+sCcFanM2xBT63UP2sghkUoHLIUpWRS8"); PasswordHash.verify("password".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=256,t=2,p=2$c29tZXNhbHQ$T/XOJ2mh1/TIpJHfCdQan76Q5esCFVoT5MAeIM1Oq2E"); PasswordHash.verify("password".getBytes(StandardCharsets.UTF_8), "$argon2i$v=19$m=65536,t=1,p=1$c29tZXNhbHQ$0WgHXE2YXhPr6uVgz4uUw7XYoWxRkWtvSsLaOsEbvs8"); }
@Test public void testArgon2id() throws IOException { System.out.println("Testing Argon2 hash verification using Argon2id algorithm."); PasswordHash.verify("password".getBytes(StandardCharsets.UTF_8), "$argon2id$v=19$m=256,t=2,p=2$c29tZXNhbHQ$bQk8UB/VmZZF4Oo79iDXuL5/0ttZwg2f/5U52iv1cDc"); }