public class SecureCharArray extends Object implements CharSequence
It's a very bad idea to store passwords in a java.lang.String object because once that object is created, you cannot guarantee that the contents will be erased. You can't get at the actual bytes inside the String object. Thus you cannot reset all the bytes to 0.
This object stores a plain array of chars allowing the data to be erased back to 0 at any given time.
ALWWAYS reset your password storage variables when you are through with them and be very wary of the stack.
| Modifier | Constructor and Description |
|---|---|
|
SecureCharArray() |
|
SecureCharArray(byte[] bytes) |
protected |
SecureCharArray(byte[] bytes,
boolean decodeAsUTF8) |
|
SecureCharArray(char[] chars) |
|
SecureCharArray(int size) |
|
SecureCharArray(SecureCharArray copy) |
|
SecureCharArray(String str)
Creates the object from a string (don't pass passwords in this way).
|
| Modifier and Type | Method and Description |
|---|---|
void |
append(SecureCharArray arr)
Appends another SecureCharArray to this one.
|
char |
charAt(int index) |
void |
erase()
Erases the data in this array by writing a pattern over every element
of the array.
|
char |
getCharAt(int index)
Obtains the character at an index.
|
char[] |
getData()
Returns a reference to the data.
|
int |
length() |
void |
prepend(SecureCharArray arr)
Prepends this array with another SecureCharArray.
|
void |
replace(SecureCharArray arr)
Replaces the contents of this array with a copy of those in arr, resizing
the array as needed.
|
void |
resize(int size,
boolean retainData)
Resizes the array, optionally keeping the old data.
|
void |
setCharAt(int index,
char c)
Sets the character at an index.
|
int |
size()
Gets the size of this array.
|
CharSequence |
subSequence(int start,
int end) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitchars, codePoints, toStringpublic SecureCharArray()
public SecureCharArray(int size)
public SecureCharArray(byte[] bytes)
bytes - - assumes no character encoding... Each byte will become its own charprotected SecureCharArray(byte[] bytes,
boolean decodeAsUTF8)
public SecureCharArray(char[] chars)
public SecureCharArray(SecureCharArray copy)
public SecureCharArray(String str)
str - The string object to get the data from.public void append(SecureCharArray arr) throws Exception
arr - The array to append.Exception - Upon size problems. (lol)public void erase()
The pattern part probably isn't needed and probably doesn't do anything but it's there anyway. The important part is to reset all the elements back to 0.
public char getCharAt(int index)
throws ArrayIndexOutOfBoundsException
index - The index to obtain the character at.ArrayIndexOutOfBoundsException - Upon bad index.public char[] getData()
public void prepend(SecureCharArray arr) throws Exception
arr - The array to prepend.Exception - Upon size problems. (lol)public void replace(SecureCharArray arr) throws Exception
arr - The copy to use.Exception - on exceptionpublic void resize(int size,
boolean retainData)
throws Exception
size - The new size of the array. This must be 1 or larger.retainData - Whether or not to keep the old data.Exception - If the size is 0 or less.public void setCharAt(int index,
char c)
throws ArrayIndexOutOfBoundsException
index - The index to set at.c - The character to set.ArrayIndexOutOfBoundsException - Upon bad index.public int size()
public char charAt(int index)
charAt in interface CharSequencepublic int length()
length in interface CharSequencepublic CharSequence subSequence(int start, int end)
subSequence in interface CharSequenceArrayIndexOutOfBoundsException - - if from < 0 or from > original.length()IllegalArgumentException - - if from < toCharSequence.subSequence(int, int)Copyright © 2018. All rights reserved.