Class PlainText

  • All Implemented Interfaces:
    java.io.Serializable

    public final class PlainText
    extends java.lang.Object
    implements java.io.Serializable
    A class representing plaintext (versus ciphertext) as related to cryptographic systems. This class embodies UTF-8 byte-encoding to translate between byte arrays and Strings. Once constructed, this object is immutable.

    Note: Conversion to/from UTF-8 byte-encoding can, in theory, throw an UnsupportedEncodingException. However, UTF-8 encoding should be a standard encoding for all Java installations, so an UnsupportedEncodingException never actually be thrown. Therefore, in order to to keep client code uncluttered, any possible UnsupportedEncodingExceptions will be first logged, and then re-thrown as a RuntimeException with the original UnsupportedEncodingException as the cause.

    Copyright © 2009 - The OWASP Foundation

    Since:
    2.0
    Author:
    kevin.w.wall@gmail.com
    See Also:
    CipherText, Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      PlainText​(byte[] b)
      Construct a PlainText object from a byte array.
      PlainText​(java.lang.String str)
      Construct a PlainText object from a String.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] asBytes()
      Convert the PlainText object to a byte array.
      protected boolean canEqual​(java.lang.Object other)
      Needed for correct definition of equals for general classes.
      boolean equals​(java.lang.Object anObject)
      int hashCode()
      Same as this.toString().hashCode().
      int length()
      Return the length of the UTF-8 encoded byte array representing this object.
      void overwrite()
      First overwrite the bytes of plaintext with the character '*'.
      java.lang.String toString()
      Convert the PlainText object to a UTF-8 encoded String.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PlainText

        public PlainText​(java.lang.String str)
        Construct a PlainText object from a String.
        Parameters:
        str - The String that is converted to a UTF-8 encoded byte array to create the PlainText object.
        Throws:
        java.lang.IllegalArgumentException - If str argument is null.
      • PlainText

        public PlainText​(byte[] b)
        Construct a PlainText object from a byte array.
        Parameters:
        b - The byte array used to create the PlainText object.
    • Method Detail

      • toString

        public java.lang.String toString()
        Convert the PlainText object to a UTF-8 encoded String.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A String representing the PlainText object.
      • asBytes

        public byte[] asBytes()
        Convert the PlainText object to a byte array.
        Returns:
        A byte array representing the PlainText object.
      • equals

        public boolean equals​(java.lang.Object anObject)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Same as this.toString().hashCode().
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        this.toString().hashCode().
      • length

        public int length()
        Return the length of the UTF-8 encoded byte array representing this object. Note that if this object was constructed with the constructor PlainText(String str), then this length might not necessarily agree with str.length().
        Returns:
        The length of the UTF-8 encoded byte array representing this object.
      • overwrite

        public void overwrite()
        First overwrite the bytes of plaintext with the character '*'.
      • canEqual

        protected boolean canEqual​(java.lang.Object other)
        Needed for correct definition of equals for general classes. (Technically not needed for 'final' classes though like this class though; this will just allow it to work in the future should we decide to allow * sub-classing of this class.)

        See How to write an Equality Method in Java for full explanation.