Hiding Text in Kotlin – How many ways?

At some point you may want to show that you know a text value, but not show the value itself. For example, when you have sensitive data stored like a password, you may want to show a representation of the length of that password but not the characters themselves.

How would you do this in Kotlin? Let’s have a little explore at some of the possibilities. (Please tweet me any more you think of, really interested in how many ways this can be done.)

A)

Creating a new string in a ‘for‘ loop, appending * for the length of the password String.

var asterisk = ""
for(int i=0; i<"Password".length) {
 asterisk = asterisk + "*"
}
someTextView.text = asterisk

B)

Creating a new string in a ‘for each‘ loop, appending * for the length of the password String.

var asterisk = ""
(0.."Password".length).forEach { _ ->
 asterisk = asterisk + "*"
}
someTextView.text = asterisk

C)

Using the TextView’s current value CharSequence in a ‘for each‘ loop, appending * for the length of the password.

var asterisk = ""
(0.."Password".length).forEach { _ ->
 someTextView.text = someTextView.text + "*"
}
someTextView.text = asterisk

D)

Using the Text.replace() method with a RegEx to create a new String.

someTextView.setText("Password".replace("[.]", "*"))

E)

Using Text.map() to map each character then a folding function with + operator to concatonate.

someTextView.setText("Password"
     .map { "*" }
     .fold("", {a,b -> a+b}))

F)

Using Text.map() to map each character then a folding function with a String template to concatonate.

someTextView.setText("Password"
     .map { "*" }
     .fold("", {a,_ -> "$a*"}))

G)

Using android.text.method.PasswordTransformationMethod to allow the platform to ensure it’s always changed.

someTextView.transformationMethod = BlogPasswordTransformationMethod()

class BlogPasswordTransformationMethod : PasswordTransformationMethod() {
    override fun getTransformation(source: CharSequence, view: View): CharSequence {
        return source.toString().replace("[.]", "*") // Or any of the other methods here..
    }
}

H)

Using a Android framework XML attribute for declaring a field will be a password. (That is deprecated)

android:password="true"

I)

Using a Android framework XML attribute for declaring the inputType of the field, that it will be a password.

android:inputType="numberPassword"

J)

Using Kotlin Text.repeat function, to repeat the given String for the length of the password.

someTextView.setText("*".repeat("Password".length))

This is just for fun and I’m sure you could measure the performance of each, or compare the comprehensibility of the chosen solution, hope you enjoyed all the choices, keeping my favourite a close guarded secret. 😉

For all those who want closure on the blog title, current best estimate, 10 ways!

Thanks to everyone that contributed ideas – please share what you think is the best and why, and if you have a new original way to do this, tweet me!

Awesome Contributors:

One thought on “Hiding Text in Kotlin – How many ways?

Comments are closed.