Start learning free

LESSON 199 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in Kotlin: Common Password Set

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

val common = setOf("password", "123456", "qwerty", "letmein")
val password = "qwerty"

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the set common and store the result as a boolean.
  • Print "Common: " followed by the boolean. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

Use a Set<String> built with setOf(...) and call .contains(password).

val common = setOf("password", "123456", "qwerty", "letmein")
val password = "qwerty"

println("Password: " + password)
val isCommon = common.contains(password)
println("Common: " + (if (isCommon) "True" else "False"))
if (isCommon) {
    println("Weak: reject")
} else {
    println("Strong: accept")
}

STEP 1 OF 5
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in Kotlin, explained

    Problem overview

    A password can pass every length and character rule and still be a bad choice, because attackers try well-known passwords first. This version rejects passwords found in a small list of common ones. The program starts with a set common holding "password", "123456", "qwerty" and "letmein", and a password to check, here "qwerty".

    It prints the password, whether it is common, and a verdict. For qwerty:

    Password: qwerty
    Common: True
    Weak: reject

    For Tr0ub4dor, which is not in the set:

    Password: Tr0ub4dor
    Common: False
    Strong: accept

    The Common line uses True or False with a capital first letter, so the program picks that text itself.

    Approach: ask a set whether it holds the value

    The question is a membership test: is this exact text one of the known ones? A Set is built for that. It stores each value once and answers "do you contain this?" quickly, however many values it holds. Scanning a list from the start would also work for four entries, but it would get slower as the list of common passwords grew to thousands. With a set, the code stays the same and the lookup stays fast.

    The result of the lookup is a boolean, which the program stores in a variable and uses twice: once to print the Common line and once to decide the verdict. In this version, being common settles the verdict by itself, whatever else is true of the password.

    Step by step

    1. Print the password. The first line is "Password: " joined to password with +.
    2. Do the lookup. Create isCommon from common.contains(password), which is true when the set holds that exact text.
    3. Print the Common line. Choose "True" or "False" with an if expression, because Kotlin prints a Boolean in lowercase.
    4. Write the verdict. An if on isCommon prints Weak: reject.
    5. Add the else. It prints Strong: accept for every password the set did not contain.

    The solution in Kotlin

            val common = setOf("password", "123456", "qwerty", "letmein")
            val password = "qwerty"
    
            println("Password: " + password)
            val isCommon = common.contains(password)
            println("Common: " + (if (isCommon) "True" else "False"))
            if (isCommon) {
                println("Weak: reject")
            } else {
                println("Strong: accept")
            }
    

    Kotlin details worth a closer look:

    • setOf. It builds a read-only Set<String> from its arguments. Duplicates are dropped, and the set is only ever read in this lesson, so read-only is the right choice. Use mutableSetOf if you need to add entries later.
    • contains and in. common.contains(password) can also be written password in common, which many Kotlin programmers prefer for readability. Both give the same answer.
    • Exact matches only. Strings are compared by content, using equals, and case counts. QWERTY is not qwerty, so the set does not contain it, and the program prints Common: False and Strong: accept for it.
    • Booleans in text. println("Common: " + isCommon) would show lowercase true. The if (isCommon) "True" else "False" expression gives the exact text needed; the parentheses make it one operand of +.
    • The word "Strong" is relative. In this version Strong: accept means only "not on the common list". The program does not check length or character classes.

    Complexity

    A lookup in a hash-based set takes constant time on average, so the check costs the same however long the list is, apart from hashing the password itself. The set takes space proportional to the number of stored passwords.

    Edge cases and common mistakes

    • Case differences. A common password in capitals slips through, as the test below shows. Real checkers often lowercase the password before the lookup; this lesson matches exactly.
    • Printing before looking up. The order of the lines is fixed: password, common flag, verdict.
    • Printing the flag raw. The output needs a capital letter, which a plain Boolean can't give.
    • Mixing up the branches. A common password must be rejected: the if branch is the rejecting one.

    Testing

    Try QWERTY in capitals. The set holds only the lowercase form, so the program prints:

    Password: QWERTY
    Common: False
    Strong: accept

    This is the gap an exact lookup leaves, and it is worth knowing before relying on such a list.

    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free