Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask String with characters

Tags:

java

Hey guy's I tried to find a way to hide a string, but the code that I found just work with my application... Is there a way to hide the characters in a string with either * or - and if there is can someone please explain

like image 902
Jaun Lloyd Avatar asked Aug 27 '12 19:08

Jaun Lloyd


People also ask

Can you remove a char from a string?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

How do you mask a string in Java?

Create a new string with StringBuilder, copy over first 4 characters. Then loop until length of the string and mask them with a * character.

How do you replace characters except last with the specified mask character?

We use some inbuilt JavaScript function slice to slice our given string here we write slice(0,-n) where -n will skip the last n character of the string. Then we use replace function to replace the character with a specified mask. Replace all characters we use regex /./g where '.


2 Answers

Is this for making a password? Consider the following:

class Password {
    final String password; // the string to mask
    Password(String password) { this.password = password; } // needs null protection
    // allow this to be equal to any string
    // reconsider this approach if adding it to a map or something?
    public boolean equals(Object o) {
        return password.equals(o);
    }
    // we don't need anything special that the string doesnt
    public int hashCode() { return password.hashCode(); }
    // send stars if anyone asks to see the string - consider sending just
    // "******" instead of the length, that way you don't reveal the password's length
    // which might be protected information
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; < password.length(); i++) 
            sb.append("*");
        return sb.toString();
    }
}

Or for the hangman approach

class Hangman {
    final String word;
    final BitSet revealed;
    public Hangman(String word) {
        this.word = word;
        this.revealed = new BitSet(word.length());
        reveal(' ');
        reveal('-');
    }
    public void reveal(char c) {
        for(int i = 0; i < word.length; i++) {
            if(word.charAt(i) == c) revealed.set(i);
        }
    }
    public boolean solve(String guess) {
        return word.equals(guess);
    }
    public String toString() {
         StringBuilder sb = new StringBuilder(word.length());
         for(int i = 0; i < word.length; i++) {
             char c = revealed.isSet(i) ? word.charAt(i) : "*";
         }
         return sb.toString();
    }
}
like image 84
corsiKa Avatar answered Oct 27 '22 00:10

corsiKa


Just create a string with the same number of characters as your original, with instead your "obfuscating" character.

String x = "ABCD";

String output = "";
for (int i = 0; i < x.length(); i++) {
    output += "*";
}

Alternatively you could use x.replaceAll("\\S", "*"), which would preserve whitespace as well.

like image 26
Roddy of the Frozen Peas Avatar answered Oct 26 '22 23:10

Roddy of the Frozen Peas