Class PasswordUtil


  • public final class PasswordUtil
    extends Object
    Utility to generate and compare password hashes.
    • Method Detail

      • buildPasswordHash

        public static String buildPasswordHash​(@NotNull
                                               @NotNull String password,
                                               @Nullable
                                               @Nullable String algorithm,
                                               int saltSize,
                                               int iterations)
                                        throws NoSuchAlgorithmException,
                                               UnsupportedEncodingException
        Generates a hash of the specified password using the specified algorithm, salt size and number of iterations into account.
        Parameters:
        password - The password to be hashed.
        algorithm - The desired hash algorithm. If the algorith is null the DEFAULT_ALGORITHM will be used.
        saltSize - The desired salt size. If the specified integer is lower that DEFAULT_SALT_SIZE the default is used.
        iterations - The desired number of iterations. If the specified integer is lower than 1 the default value is used.
        Returns:
        The password hash.
        Throws:
        NoSuchAlgorithmException - If the specified algorithm is not supported.
        UnsupportedEncodingException - If utf-8 is not supported.
      • isPlainTextPassword

        public static boolean isPlainTextPassword​(@Nullable
                                                  @Nullable String password)
        Returns true if the specified string doesn't start with a valid algorithm name in curly brackets.
        Parameters:
        password - The string to be tested.
        Returns:
        true if the specified string doesn't start with a valid algorithm name in curly brackets.
      • isSame

        public static boolean isSame​(@Nullable
                                     @Nullable String hashedPassword,
                                     @NotNull
                                     @org.jetbrains.annotations.NotNull char[] password)
        Returns true if hash of the specified password equals the given hashed password.
        Parameters:
        hashedPassword - Password hash.
        password - The password to compare.
        Returns:
        If the hash created from the specified password equals the given hashedPassword string.
      • isSame

        public static boolean isSame​(@Nullable
                                     @Nullable String hashedPassword,
                                     @NotNull
                                     @NotNull String password)
        Returns true if hash of the specified password equals the given hashed password.
        Parameters:
        hashedPassword - Password hash.
        password - The password to compare.
        Returns:
        If the hash created from the specified password equals the given hashedPassword string.