/*
 * 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.security.SecureRandom;
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, ' ');
            SecureRandom r = new SecureRandom();
            int index = 0;
            while (index < chars.length) {
                char[] cs = Character.toChars(Math.abs(r.nextInt()) % Character.MAX_CODE_POINT);
                if (cs != null) {
                    for (int i = 0;
                         i < cs.length && i + index < chars.length;
                         i++) {
                        chars[index + i] = cs[i];
                    }
                    index += cs.length;
                }
            }
            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;
    }
}

@@ -1515,50 +1515,32 @@
     
-int index = 0;%0A            while (index %3C 
+byte%5B%5D bytes = new byte%5B
 char
@@ -1547,19 +1547,18 @@
 s.length
-) %7B
+%5D;
 %0A       
@@ -1566,135 +1566,28 @@
     
-    char%5B%5D cs = Character.toChars(Math.abs(r.nextInt()) %25 Character.MAX_CODE_POINT);%0A                if (cs != null) %7B%0A        
+r.nextBytes(bytes);%0A
     
@@ -1631,44 +1631,11 @@
     
-       
- 
 i %3C
- cs.length && i + index %3C
  cha
@@ -1658,32 +1658,24 @@
             
-        
 i++) %7B%0A     
@@ -1689,121 +1689,35 @@
     
-      
-  
 chars%5Bi
-ndex + i%5D = cs%5Bi%5D;%0A                    %7D%0A                    index += cs.length;%0A                %7D
+%5D = (char) bytes%5Bi%5D;
 %0A   

edit secure char sequence