Home > Enterprise >  password validation no Character sequence ( kotlin android studio )
password validation no Character sequence ( kotlin android studio )

Time:05-25

I have the task to create a password validation that has to consider some things. The only problem I have is that one of the criteria of the password validation is that the password must not contain any sequences, e.g. (12345), (abcdef), (asdfghjk). I have already searched a lot and do not know how to implement this. Can anyone help.

CodePudding user response:

Since you didn't give much detail into what code you already have and what you're stuck on about the logic, here's a very generalized description of a strategy you could use to do this:

  1. Create a List<Iterable<Char>> that contains all the possible strings of characters that could be considered a range. For example:
    val charRuns = listOf(
        '0'..'9',
        'a'..'z',
        'A'..'Z',
        "qwertyuiop".asIterable(),
        //...
    )
  1. Iterate these runs to fill a MutableMap<Char, MutableSet<Char>>, where the keys are any of the characters from the runs, and the values are sets of all the chars that if they appear next in a string should be considered a consecutive sequence.

  2. Iterate the potential password String, using the map to check the subsequent Char of each Char to see if it should be considered part of a sequence. Use a counter variable to count the current size of sequence found so far, and reset it whenever a non-sequence is found. If it ever rises above your threshold for allowable sequence size, reject the password immediately.

CodePudding user response:

This is how I implemented it. I also check that there is no sequence in backwards, for example (4321, dcba).

private fun noSequenzes(password: String) : Boolean {
    val charRuns = listOf(
        '0'..'9',
        'a'..'z',
        'A'..'Z',
        "qwertzuiop".asIterable(),
        "asdfghjklöä".asIterable(),
        "yxcvbnm".asIterable()
    )
    var map = emptyMap<Char, MutableSet<Char?>>()
    charRuns.forEach { run ->
        run.forEach { char ->
            val charsToAdd = mutableSetOf(run.elementAtOrNull(run.indexOf(char)   1))
            if (run is CharRange) {
                charsToAdd.add(run.elementAtOrNull(run.indexOf(char) - 1))
            }
            if (map.contains(char)) {
                map.get(char)!!.addAll(charsToAdd)
            }
            else {
                map = map.plus(Pair(char, charsToAdd))
            }
        }
    }
    var sequenceCounter = 1
    var recentChar: Char? = null
    password.toCharArray().forEach { c ->
        recentChar?.let { rc ->
            val isSequence = map.any { me -> me.key == rc && me.value.contains(c) }
            if (isSequence) {
                sequenceCounter = sequenceCounter   1
            }
            else {
                sequenceCounter = 1
            }
            if (sequenceCounter >= 3) {
                return false
            }
        }
        recentChar = c
    }
    return true
}
  • Related