Class Secret

java.lang.Object
codes.grow.packages.betterstack.core.Secret

public final class Secret extends Object
A credential held in a char[] that never prints itself.

toString() and JSON serialisation return "[REDACTED]". Call reveal() only where the value is sent, and clear() to overwrite the characters when the credential is no longer needed.

  • Method Details

    • of

      public static Secret of(String value)
      Wraps a string value.
      Parameters:
      value - the credential
      Returns:
      a new secret
    • of

      public static Secret of(char[] value)
      Wraps a copy of a character array; the caller may clear its own array afterwards.
      Parameters:
      value - the credential
      Returns:
      a new secret
    • reveal

      public String reveal()
      Returns the credential as a string.
      Returns:
      the value
      Throws:
      IllegalStateException - after clear()
    • revealChars

      public char[] revealChars()
      Returns a copy of the credential characters.
      Returns:
      a new array the caller should clear after use
      Throws:
      IllegalStateException - after clear()
    • isEmpty

      public boolean isEmpty()
      Returns whether the value is empty.
      Returns:
      true for an empty credential
    • isCleared

      public boolean isCleared()
      Returns whether clear() was called.
      Returns:
      true once cleared
    • clear

      public void clear()
      Overwrites the characters with zeros; later reveal() calls throw. Idempotent.
    • sameValue

      public boolean sameValue(Secret other)
      Compares the values in constant time.
      Parameters:
      other - another secret
      Returns:
      true when both hold the same characters
    • toString

      public String toString()
      Always "[REDACTED]".
      Overrides:
      toString in class Object
      Returns:
      the redaction marker