Pbkdf2PasswordHashImpl implements Pbkdf2PasswordHash { @Override public boolean verify(char[] password, String hashedPassword) { EncodedPasswordHash encodedPasswordHash = new EncodedPasswordHash(hashedPassword); byte[] hashToVerify = pbkdf2( password, encodedPasswordHash.getSalt(), encodedPasswordHash.getAlgorithm(), encodedPasswordHash.getIterations(), encodedPasswordHash.getHash().length); return PasswordHashCompare.compareBytes(hashToVerify, encodedPasswordHash.getHash()); }  @Override void initialize(Map<String, String> parameters); @Override String generate(char[] password); @Override boolean verify(char[] password, String hashedPassword);  }
@Test public void verify() { Assert.assertTrue(hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA256", 16))); Assert.assertTrue(hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash2, salt2, "PBKDF2WithHmacSHA512", 1024))); Assert.assertTrue(hashImpl.verify("SomethingElse".toCharArray(), encodedPassword(encodedHash3, salt1, "PBKDF2WithHmacSHA512", 1024))); Assert.assertTrue(hashImpl.verify("SomethingElse".toCharArray(), encodedPassword(encodedHash4, salt2, "PBKDF2WithHmacSHA256", 32))); }
@Test public void verify_failure() { Assert.assertFalse(hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA256", 64))); Assert.assertFalse(hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA512", 16))); Assert.assertFalse(hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash1, salt2, "PBKDF2WithHmacSHA256", 16))); Assert.assertFalse(hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash2, salt1, "PBKDF2WithHmacSHA256", 16))); Assert.assertFalse(hashImpl.verify("SomethingElse".toCharArray(), encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA256", 16))); }
@Test(expected = IllegalArgumentException.class) public void verify_wrong1() { hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA1", 64)); }
@Test(expected = IllegalArgumentException.class) public void verify_wrong2() { String encodedPassword = encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA256", 12); String newValue = encodedPassword.replaceAll(":12:", ":hihihi:"); hashImpl.verify("Soteria".toCharArray(), newValue); }
@Test(expected = IllegalArgumentException.class) public void verify_wrong3() { hashImpl.verify("Soteria".toCharArray(), encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA256", -1)); }
@Test(expected = IllegalArgumentException.class) public void verify_wrong4() { String encodedPassword = encodedPassword(encodedHash1, salt1, "PBKDF2WithHmacSHA256", 64); String newValue = encodedPassword.substring(0, 30) + "$" + encodedPassword.substring(31); hashImpl.verify("Soteria".toCharArray(), newValue); }