PasswordUtil { public static String buildPasswordHash(@NotNull String password) throws NoSuchAlgorithmException, UnsupportedEncodingException { return buildPasswordHash(password, DEFAULT_ALGORITHM, DEFAULT_SALT_SIZE, DEFAULT_ITERATIONS); } private  PasswordUtil(); static String buildPasswordHash(@NotNull String password); static String buildPasswordHash(@NotNull String password,
                                           @Nullable String algorithm,
                                           int saltSize, int iterations); static String buildPasswordHash(@NotNull String password,
                                           @NotNull ConfigurationParameters config); static boolean isPlainTextPassword(@Nullable String password); static boolean isSame(@Nullable String hashedPassword, @NotNull char[] password); static boolean isSame(@Nullable String hashedPassword, @NotNull String password); static final String DEFAULT_ALGORITHM; static final int DEFAULT_SALT_SIZE; static final int DEFAULT_ITERATIONS; }
@Test public void testBuildPasswordHash() throws Exception { for (String pw : plainPasswords) { String pwHash = PasswordUtil.buildPasswordHash(pw); assertFalse(pw.equals(pwHash)); } List<Integer[]> l = new ArrayList<>(); l.add(new Integer[] {0, 1000}); l.add(new Integer[] {1, 10}); l.add(new Integer[] {8, 50}); l.add(new Integer[] {10, 5}); l.add(new Integer[] {-1, -1}); for (Integer[] params : l) { for (String pw : plainPasswords) { int saltsize = params[0]; int iterations = params[1]; String pwHash = PasswordUtil.buildPasswordHash(pw, DEFAULT_ALGORITHM, saltsize, iterations); assertFalse(pw.equals(pwHash)); } } }
@Test public void testBuildPasswordHashInvalidAlgorithm() throws Exception { List<String> invalidAlgorithms = new ArrayList<>(); invalidAlgorithms.add(""); invalidAlgorithms.add("+"); invalidAlgorithms.add("invalid"); for (String invalid : invalidAlgorithms) { try { PasswordUtil.buildPasswordHash("pw", invalid, PasswordUtil.DEFAULT_SALT_SIZE, PasswordUtil.DEFAULT_ITERATIONS); fail("Invalid algorithm " + invalid); } catch (NoSuchAlgorithmException e) { } } }