Class SecureString

java.lang.Object
org.flasby.crypto.SecureString
All Implemented Interfaces:
AutoCloseable, CharSequence

public final class SecureString extends Object implements CharSequence, AutoCloseable
This is not a string but a CharSequence that can be cleared of its memory. Important for handling passwords. Represents text that should be kept confidential, such as by deleting it from memory when no longer needed or garbaged collected.

Modified from an original post in stackoverflow by Melloware who modified something from OWASP.

  • Constructor Details

    • SecureString

      public SecureString(CharSequence original)
    • SecureString

      public SecureString(int start, int end, CharSequence original)
  • Method Details

    • charAt

      public char charAt(int i)
      Specified by:
      charAt in interface CharSequence
    • length

      public int length()
      Specified by:
      length in interface CharSequence
    • subSequence

      public CharSequence subSequence(int start, int end)
      Specified by:
      subSequence in interface CharSequence
    • asString

      public String asString()
      Convert array back to String but not using toString(). See toString() docs below. Calling should be avoided as it leaves passwords in memory which can't be cleared.
    • clear

      public void clear()
      Manually clear the underlying array holding the characters
    • toString

      public String toString()
      Protect against using this class in log statements.

      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable