java.lang.Object
com.inductiveautomation.ignition.gateway.secrets.Plaintext
All Implemented Interfaces:
Closeable, AutoCloseable

public class Plaintext extends Object implements Closeable
Wrapper around a byte array representing plaintext to be encrypted into ciphertext or decrypted from ciphertext. Provides helper methods for creating a Plaintext instance from a byte array or String and for retrieving the wrapped byte array or encoding the wrapped byte array as a String. Also provides a method to clear the byte array's contents from memory by zero-ing each byte.

This class is not thread safe and the wrapped bytes are mutable by any code with access to the Plaintext instance.

  • Method Details

    • fromBytes

      public static Plaintext fromBytes(byte[] bytes)
      Create a new Plaintext instance from a byte array. The resulting Plaintext instance wraps the given byte array directly. No copy is made which means that changes to the given byte array are directly reflected in the byte array wrapped by the resulting Plaintext instance.
      Parameters:
      bytes - the byte array to be wrapped directly by the resulting Plaintext instance. cannot be null.
      Returns:
      the new Plaintext instance directly wrapping the given byte array. never null.
    • fromString

      public static Plaintext fromString(String str)
      Create a new Plaintext instance from a String. The resulting Plaintext instance wraps a byte array encoding the String using the UTF-8 Character set.
      Parameters:
      str - the String to be encoded into a byte array. cannot be null.
      Returns:
      the new Plaintext instance wrapping the given String encoded using the UTF-8 Charset. never null.
      Throws:
      NullPointerException - if the str argument is null
      See Also:
    • fromString

      public static Plaintext fromString(String str, Charset charset)
      Create a new Plaintext instance from a String. The resulting Plaintext instance wraps a byte array encoding the String using the given Character set.
      Parameters:
      str - the String to be encoded into a byte array. cannot be null.
      charset - the Charset used to encode the given String into a byte array. cannot be null.
      Returns:
      the new Plaintext instance wrapping the given String encoded using the given Charset. never null.
      Throws:
      NullPointerException - if either str or charset arguments are null
    • getBytes

      public byte[] getBytes()
      Returns:
      a direct reference to the wrapped byte array. never null.
      Throws:
      IllegalStateException - if the Plaintext instance is cleared
    • getAsString

      public String getAsString()
      UTF-8 decode the wrapped byte array into a String.
      Returns:
      a new String instance from decoding the wrapped UTF-8 encoded byte array. never null.
      Throws:
      IllegalStateException - if the Plaintext instance is cleared
      See Also:
    • getAsString

      public String getAsString(Charset charset)
      Decode the wrapped byte array into a String given a Character set.
      Parameters:
      charset - the Charset used to decode the wrapped byte array into a String
      Returns:
      a new String instance from decoding the wrapped byte array. never null.
      Throws:
      IllegalStateException - if the Plaintext instance is cleared
    • clear

      public void clear()
      Zeroes the wrapped byte array and clears the reference to the byte array. The Plaintext instance is no longer usable after it is cleared.
      Throws:
      IllegalStateException - if the Plaintext instance has already been cleared
    • close

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