com.wm.util.security
Class WmSecureString

java.lang.Object
  extended by com.wm.util.security.WmSecureString
All Implemented Interfaces:
java.io.Serializable

public class WmSecureString
extends java.lang.Object
implements java.io.Serializable

A simple wrapper for character array.

This class addresses security concerns related to String storage in memory, specifically the immutable string issue that prevents explicit erasure of string characters. This class is a mutable alternative to String, allowing characters to be removed from memory. The stored characters are kept in memory as clear text until erased.

See Also:
Serialized Form

Constructor Summary
WmSecureString(byte[] inArray)
          Constructs WmSecureString from a byte array, using the default encoding.
WmSecureString(byte[] inArray, java.lang.String inEncoding)
          Constructs WmSecureString from a byte array, using a specified encoding.
WmSecureString(char[] inArray)
          Constructs WmSecureString from a character array.
WmSecureString(java.lang.String inStr)
          Constructs WmSecureString from a string.
WmSecureString(WmSecureString inBuff)
          Constructs WmSecureString from another secure string buffer.
 
Method Summary
 boolean equals(java.lang.Object inObj)
          Overrides java.lang.Object.equals() to compare chars.
 void erase()
          Erases the buffer contents.
 char[] getChars()
          Creates a byte array from the buffer contents, using the input encoding.
 int hashCode()
          Overrides java.lang.Object.hashCode().
 void initFromByteArray(byte[] inArray, java.lang.String inEncoding)
          Initializes the buffer with a byte array.
 int length()
          Returns the number of characters in the buffer.
 byte[] toByteArray()
          Creates a byte array from the buffer contents, using the default encoding.
 byte[] toByteArray(java.lang.String inEnc)
          Creates a byte array from the buffer contents, using a specified encoding.
 java.lang.String toString()
          Creates a string from the buffer contents.
 boolean wasErased()
          Returns true if the secure string buffer is currently empty.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

WmSecureString

public WmSecureString(char[] inArray)
Constructs WmSecureString from a character array.

Parameters:
inArray - Character array from which to populate the buffer.

WmSecureString

public WmSecureString(byte[] inArray)
Constructs WmSecureString from a byte array, using the default encoding.

Parameters:
inArray - Byte array from which to populate the buffer.

WmSecureString

public WmSecureString(byte[] inArray,
                      java.lang.String inEncoding)
               throws java.io.UnsupportedEncodingException
Constructs WmSecureString from a byte array, using a specified encoding.

Parameters:
inArray - Byte array from which to populate the buffer.
inEncoding - Identifier for the encoding to be used for conversion.
Throws:
java.io.UnsupportedEncodingException - Thrown if the specified encoding is not supported.

WmSecureString

public WmSecureString(java.lang.String inStr)
Constructs WmSecureString from a string.

Parameters:
inStr - String from which to populate the buffer.

WmSecureString

public WmSecureString(WmSecureString inBuff)
Constructs WmSecureString from another secure string buffer.

Parameters:
inBuff - WmSecureString from which to populate the buffer.
Method Detail

length

public int length()
Returns the number of characters in the buffer.

Returns:
Character count.

toByteArray

public byte[] toByteArray()
Creates a byte array from the buffer contents, using the default encoding.

Returns:
Byte array containing buffer characters.

toByteArray

public byte[] toByteArray(java.lang.String inEnc)
                   throws java.io.UnsupportedEncodingException
Creates a byte array from the buffer contents, using a specified encoding.

Parameters:
inEnc - Identifier for the encoding to be used for the conversion.
Returns:
Byte array containing buffer characters.
Throws:
java.io.UnsupportedEncodingException - Thrown if the specified encoding is not supported.

getChars

public char[] getChars()
Creates a byte array from the buffer contents, using the input encoding.

Returns:
Byte array containing buffer characters.

toString

public java.lang.String toString()
Creates a string from the buffer contents.

Overrides:
toString in class java.lang.Object
Returns:
String containing buffer characters.

erase

public void erase()
Erases the buffer contents.


equals

public boolean equals(java.lang.Object inObj)
Overrides java.lang.Object.equals() to compare chars.

Overrides:
equals in class java.lang.Object
Parameters:
inObj - Target object for comparison.
Returns:
true if and only if input object has identical contents.

hashCode

public int hashCode()
Overrides java.lang.Object.hashCode().

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code.

wasErased

public boolean wasErased()
Returns true if the secure string buffer is currently empty.


initFromByteArray

public void initFromByteArray(byte[] inArray,
                              java.lang.String inEncoding)
                       throws java.io.UnsupportedEncodingException
Initializes the buffer with a byte array.

Parameters:
inArray - Byte array to replace the current buffer contents.
inEncoding - Identifier for the encoding to be used for the conversion.
Throws:
java.io.UnsupportedEncodingException - Thrown if the specified encoding is not supported.