/*
 * Copyright 2014 http://Bither.net
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.bither.bitherj.crypto;


import java.util.Arrays;

public class SecureCharSequence implements CharSequence {
    private char[] chars;

    public SecureCharSequence(CharSequence charSequence) {
        this(charSequence, 0, charSequence.length());
    }

    public SecureCharSequence(char[] chars) {
        wipe();
        this.chars = chars;
    }

    private SecureCharSequence(CharSequence charSequence, int start, int end) {
        // pulled from http://stackoverflow.com/a/15844273
        wipe();
        int length = end - start;
        chars = new char[length];
        for (int i = start;
             i < end;
             i++) {
            chars[i - start] = charSequence.charAt(i);
        }
    }

    public void wipe() {
        if (chars != null) {
            Arrays.fill(chars, ' ');
        }
    }

    protected void finalize() {
        wipe();
    }

    @Override
    public int length() {
        if (chars != null) {
            return chars.length;
        }
        return 0;
    }

    @Override
    public char charAt(int index) {
        if (chars != null) {
            return chars[index];
        }
        return 0;
    }

    @Override
    public String toString() {
        return String.valueOf(this.chars);
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof SecureCharSequence) {
            return Arrays.equals(chars, ((SecureCharSequence) o).chars);
        }
        return false;
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        SecureCharSequence s = new SecureCharSequence(this, start, end);
        return s;
    }
}

@@ -631,16 +631,51 @@
 ypto;%0A%0A%0A
+import java.security.SecureRandom;%0A
 import j
@@ -1450,16 +1450,583 @@
 , ' ');%0A
+            SecureRandom r = new SecureRandom();%0A            int index = 0;%0A            while (index %3C chars.length) %7B%0A                char%5B%5D cs = Character.toChars(Math.abs(r.nextInt()) %25 Character.MAX_CODE_POINT);%0A                if (cs != null) %7B%0A                    for (int i = 0;%0A                         i %3C cs.length && i + index %3C chars.length;%0A                         i++) %7B%0A                        chars%5Bindex + i%5D = cs%5Bi%5D;%0A                    %7D%0A                    index += cs.length;%0A                %7D%0A            %7D%0A            Arrays.fill(chars, ' ');%0A
         

edit secure char sequence