Package org.owasp.esapi.crypto
Class PlainText
- java.lang.Object
-
- org.owasp.esapi.crypto.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 andString
s. 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 anUnsupportedEncodingException
never actually be thrown. Therefore, in order to to keep client code uncluttered, any possibleUnsupportedEncodingException
s will be first logged, and then re-thrown as aRuntimeException
with the originalUnsupportedEncodingException
as the cause.Copyright © 2009 - The OWASP Foundation
- Since:
- 2.0
- Author:
- kevin.w.wall@gmail.com
- See Also:
CipherText
, Serialized Form
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
asBytes()
Convert thePlainText
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 asthis.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 thePlainText
object to a UTF-8 encodedString
.
-
-
-
Constructor Detail
-
PlainText
public PlainText(java.lang.String str)
Construct aPlainText
object from aString
.- Parameters:
str
- TheString
that is converted to a UTF-8 encoded byte array to create thePlainText
object.- Throws:
java.lang.IllegalArgumentException
- Ifstr
argument is null.
-
PlainText
public PlainText(byte[] b)
Construct aPlainText
object from abyte
array.- Parameters:
b
- Thebyte
array used to create thePlainText
object.
-
-
Method Detail
-
toString
public java.lang.String toString()
Convert thePlainText
object to a UTF-8 encodedString
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A
String
representing thePlainText
object.
-
asBytes
public byte[] asBytes()
Convert thePlainText
object to a byte array.- Returns:
- A byte array representing the
PlainText
object.
-
equals
public boolean equals(java.lang.Object anObject)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
Same asthis.toString().hashCode()
.- Overrides:
hashCode
in classjava.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 constructorPlainText(String str)
, then this length might not necessarily agree withstr.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.
-
-