Class Plaintext
java.lang.Object
com.inductiveautomation.ignition.gateway.secrets.Plaintext
- All Implemented Interfaces:
Closeable
,AutoCloseable
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 Summary
Modifier and TypeMethodDescriptionvoid
clear()
Zeroes the wrapped byte array and clears the reference to the byte array.void
close()
static Plaintext
fromBytes
(byte[] bytes) Create a newPlaintext
instance from a byte array.static Plaintext
fromString
(String str) Create a newPlaintext
instance from a String.static Plaintext
fromString
(String str, Charset charset) Create a newPlaintext
instance from a String.UTF-8 decode the wrapped byte array into a String.getAsString
(Charset charset) Decode the wrapped byte array into a String given a Character set.byte[]
getBytes()
-
Method Details
-
fromBytes
Create a newPlaintext
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
Create a newPlaintext
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
Create a newPlaintext
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
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
Decode the wrapped byte array into a String given a Character set.- Parameters:
charset
- theCharset
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-