Class PassphraseUtils

java.lang.Object
de.craftsblock.craftsnet.utils.PassphraseUtils

public class PassphraseUtils extends Object
This utility class provides helper methods for generating passphrases.
Since:
3.3.5-SNAPSHOT
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The base chars used to created passphrases.
    static final String
    The special chars used to created passphrases when special chars are required.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    erase(byte @NotNull [] passphrase)
    Overwrites the contents of the given byte array with zeros.
    static void
    erase(char @NotNull [] passphrase)
    Overwrites the contents of the given char array with null characters ('\0').
    static byte[]
    Generates a secure random passphrase with a default length between 12 and 16 characters.
    static byte[]
    generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int length)
    Generates a secure random passphrase with a specified length and the option to include special characters.
    static byte[]
    generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int min, @org.jetbrains.annotations.Range(from=1L, to=2147483647L) int max, boolean specialChars)
    Generates a secure random passphrase with a specified length range and the option to include special characters.
    static byte[]
    generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int min, @org.jetbrains.annotations.Range(from=1L, to=2147483647L) int max, @NotNull String chars)
    Generates a secure random passphrase with a specified length range.
    static byte[]
    generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int length, boolean specialChars)
    Generates a secure random passphrase with a specified length and the option to include special characters.
    static byte[]
    generateSecure(boolean specialChars)
    Generates a secure random passphrase with a default length between 12 and 16 characters.
    static int
    secureRandomLength(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int min, @org.jetbrains.annotations.Range(from=1L, to=2147483647L) int max)
    Generates a secure random passphrase length within a specified range.
    static String
    stringify(byte @NotNull [] passphrase)
    Converts the given UTF-8 encoded byte array into a String.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CHARS

      public static final String CHARS
      The base chars used to created passphrases.
      See Also:
    • SPECIAL_CHARS

      public static final String SPECIAL_CHARS
      The special chars used to created passphrases when special chars are required.
      See Also:
  • Method Details

    • generateSecure

      public static byte[] generateSecure()
      Generates a secure random passphrase with a default length between 12 and 16 characters. The passphrase can include digits, uppercase and lowercase letters, and special characters.
      Returns:
      a secure random passphrase as a byte[].
    • generateSecure

      public static byte[] generateSecure(boolean specialChars)
      Generates a secure random passphrase with a default length between 12 and 16 characters. The passphrase can include digits, uppercase and lowercase letters, and optionally special characters.
      Parameters:
      specialChars - whether to include special characters in the passphrase.
      Returns:
      a secure random passphrase as a byte[].
    • generateSecure

      public static byte[] generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int length)
      Generates a secure random passphrase with a specified length and the option to include special characters. The passphrase is generated from a character pool consisting of digits, lowercase and uppercase letters, and special characters.
      Parameters:
      length - the length of the generated passphrase.
      Returns:
      a secure random passphrase as a byte[].
    • generateSecure

      public static byte[] generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int length, boolean specialChars)
      Generates a secure random passphrase with a specified length and the option to include special characters. The passphrase is generated from a character pool consisting of digits, lowercase and uppercase letters, and optionally special characters.
      Parameters:
      length - the length of the generated passphrase.
      specialChars - whether to include special characters in the passphrase.
      Returns:
      a secure random passphrase as a byte[].
    • generateSecure

      public static byte[] generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int min, @org.jetbrains.annotations.Range(from=1L, to=2147483647L) int max, boolean specialChars)
      Generates a secure random passphrase with a specified length range and the option to include special characters. The passphrase is generated from a character pool consisting of digits, lowercase and uppercase letters, and optionally special characters.
      Parameters:
      min - The minimum length of the generated passphrase (inclusive).
      max - The maximum length of the generated passphrase (exclusive).
      specialChars - Whether to include special characters in the passphrase.
      Returns:
      A secure random passphrase as a byte[].
    • generateSecure

      public static byte[] generateSecure(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int min, @org.jetbrains.annotations.Range(from=1L, to=2147483647L) int max, @NotNull @NotNull String chars)
      Generates a secure random passphrase with a specified length range. The passphrase is generated from the specified character pool.
      Parameters:
      min - The minimum length of the generated passphrase (inclusive).
      max - The maximum length of the generated passphrase (exclusive).
      chars - The character pool from which the passphrase will be generated. Must include more than 15 characters.
      Returns:
      A secure random passphrase as a byte[].
      Since:
      3.4.3-SNAPSHOT
    • erase

      public static void erase(byte @NotNull [] passphrase)
      Overwrites the contents of the given byte array with zeros. This is useful for securely erasing sensitive data such as passwords from memory to reduce the risk of leaking secrets.
      Parameters:
      passphrase - The byte array to be cleared; must not be null
      Since:
      3.4.1-SNAPSHOT
    • erase

      public static void erase(char @NotNull [] passphrase)
      Overwrites the contents of the given char array with null characters ('\0'). This is useful for securely erasing sensitive data such as passwords from memory to reduce the risk of leaking secrets.
      Parameters:
      passphrase - The char array to be cleared; must not be null
      Since:
      3.4.1-SNAPSHOT
    • stringify

      public static String stringify(byte @NotNull [] passphrase)
      Converts the given UTF-8 encoded byte array into a String.

      WARNING: This method converts sensitive byte arrays (e.g., passwords) into a String, which is immutable in Java and cannot be explicitly cleared from memory. This may lead to sensitive data lingering in memory longer than necessary and increase the risk of information leakage.

      Use this method only if you fully understand the security implications.

      Parameters:
      passphrase - The byte array containing UTF-8 encoded characters; must not be null
      Returns:
      A String representing the decoded characters
      Throws:
      NullPointerException - If passphrase is null
    • secureRandomLength

      public static int secureRandomLength(@org.jetbrains.annotations.Range(from=1L, to=2147483647L) int min, @org.jetbrains.annotations.Range(from=1L, to=2147483647L) int max)
      Generates a secure random passphrase length within a specified range. The length is chosen randomly from the given range using a secure random number generator.
      Parameters:
      min - The minimum length (inclusive).
      max - The maximum length (exclusive).
      Returns:
      A randomly generated length within the specified range.