/** * Generates a hash of the specified password using the specified algorithm, * salt size and number of iterations into account. * * @param password The password to be hashed. * @param algorithm The desired hash algorithm. If the algorith is * {@code null} the {@link #DEFAULT_ALGORITHM} will be used. * @param saltSize The desired salt size. If the specified integer is lower * that {@link #DEFAULT_SALT_SIZE} the default is used. * @param iterations The desired number of iterations. If the specified * integer is lower than 1 the {@link #DEFAULT_ITERATIONS default} value is used. * @return The password hash. * @throws NoSuchAlgorithmException If the specified algorithm is not supported. * @throws UnsupportedEncodingException If utf-8 is not supported. */ public static String buildPasswordHash(@NotNull String password, @Nullable String algorithm, int saltSize, int iterations) throws NoSuchAlgorithmException, UnsupportedEncodingException { checkNotNull(password); if (iterations < NO_ITERATIONS) { iterations = DEFAULT_ITERATIONS; } if (saltSize < DEFAULT_SALT_SIZE) { saltSize = DEFAULT_SALT_SIZE; } String salt = generateSalt(saltSize); String alg = (algorithm == null) ? DEFAULT_ALGORITHM : algorithm; return generateHash(password, alg, salt, iterations); }
/** * Generates a hash of the specified password using the specified algorithm, * salt size and number of iterations into account. * * @param password The password to be hashed. * @param algorithm The desired hash algorithm. If the algorith is * {@code null} the {@link #DEFAULT_ALGORITHM} will be used. * @param saltSize The desired salt size. If the specified integer is lower * that {@link #DEFAULT_SALT_SIZE} the default is used. * @param iterations The desired number of iterations. If the specified * integer is lower than 1 the {@link #DEFAULT_ITERATIONS default} value is used. * @return The password hash. * @throws NoSuchAlgorithmException If the specified algorithm is not supported. * @throws UnsupportedEncodingException If utf-8 is not supported. */ public static String buildPasswordHash(@Nonnull String password, @Nullable String algorithm, int saltSize, int iterations) throws NoSuchAlgorithmException, UnsupportedEncodingException { checkNotNull(password); if (iterations < NO_ITERATIONS) { iterations = DEFAULT_ITERATIONS; } if (saltSize < DEFAULT_SALT_SIZE) { saltSize = DEFAULT_SALT_SIZE; } String salt = generateSalt(saltSize); String alg = (algorithm == null) ? DEFAULT_ALGORITHM : algorithm; return generateHash(password, alg, salt, iterations); }