Start learning free

LESSON 199 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in Go: Rejecting Common Passwords with a Map

Practice Read the full explanation

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

You start with:

common := map[string]bool{"password": true, "123456": true, "qwerty": true, "letmein": true}
password := "qwerty"

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the map 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".
  • The helper boolText(b) returns "True" or "False" for a boolean.

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

Use a map[string]bool and check common[password] (returns true if present).

common := map[string]bool{"password": true, "123456": true, "qwerty": true, "letmein": true}
password := "qwerty"

fmt.Println("Password: " + password)
isCommon := common[password]
fmt.Println("Common: " + boolText(isCommon))
if isCommon {
	fmt.Println("Weak: reject")
} else {
	fmt.Println("Strong: accept")
}

STEP 1 OF 5
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in Go, explained

    Problem overview

    The starter code has a common map, map[string]bool, listing well-known weak passwords: password, 123456, qwerty and letmein. It also has the password to check. The program prints the password, whether it is on the list, and a verdict. For qwerty:

    Password: qwerty
    Common: True
    Weak: reject

    For Tr0ub4dor, which is not on the list, the output is Password: Tr0ub4dor, Common: False and Strong: accept. The helper boolText(b) is provided and returns "True" or "False".

    Approach: a map used as a set

    Checking membership in a list means comparing against each entry until one matches. A map finds a key directly, so the cost of the check does not grow with the number of entries. Go has no built-in set type, so the usual way to make a set is a map[string]bool whose present keys have the value true.

    The lookup common[password] then answers the question in one expression. If the password is a key, the result is its value, true. If it is not a key, Go gives the zero value of the value type, and for bool that is false. So no separate "does the key exist" test is needed.

    The check is exact. Qwerty with a capital Q is a different key from qwerty, so it is not found.

    Step by step

    1. Print the password line. "Password: " + password joins two strings, so no conversion is needed. fmt.Println prints it.
    2. Look the password up. isCommon := common[password] stores the lookup result, a bool.
    3. Print the Common: line with boolText(isCommon) to get the capitalized text. Printing the boolean directly would give lowercase true.
    4. Write the verdict. if isCommon is enough as the condition, since isCommon is already a boolean. Comparing it to true adds nothing. The body prints Weak: reject.
    5. Add the else. It runs when the password is not on the list, and prints Strong: accept. The closing brace of the if and the else share a line.

    The solution in Go

    common := map[string]bool{"password": true, "123456": true, "qwerty": true, "letmein": true}
    password := "qwerty"
    
    fmt.Println("Password: " + password)
    isCommon := common[password]
    fmt.Println("Common: " + boolText(isCommon))
    if isCommon {
    	fmt.Println("Weak: reject")
    } else {
    	fmt.Println("Strong: accept")
    }
    

    Go details worth knowing here:

    • Missing keys give the zero value. Reading common["hello"] does not fail and does not add a key. It gives false.
    • The two-value lookup. v, ok := common[password] also reports whether the key exists. For a set of true values the plain form is enough, because absence and false mean the same. The two-value form matters when false could be a stored value.
    • Booleans need a helper to print as True. fmt.Println(isCommon) prints true. The lesson's output wants a capital letter, which is why boolText exists.
    • Short variable declarations. isCommon := ... declares and assigns in one step, and the type bool is inferred from the map's value type.
    • Empty struct sets. Some Go programs use map[string]struct{} as a set, because the value takes no space. This lesson's map[string]bool is easier to read and is what the starter uses.

    Edge cases and common mistakes

    • Case differences. Qwerty gives Common: False and Strong: accept, because only the exact lowercase text is a key. Converting the password with strings.ToLower before the lookup would catch these.
    • An empty password. "" is not a key, so the program prints Common: False and Strong: accept. This version has no length rule.
    • Calling "accept" strong. The only rule here is the list, so a password that is not on the list is accepted whatever it looks like. The output text follows the lesson's wording.
    • Comparing with == true. if isCommon == true compiles but is redundant.
    • Spelling in the output. The lines must be exactly Weak: reject and Strong: accept.

    Testing

    • qwerty prints Common: True and Weak: reject.
    • Tr0ub4dor prints Common: False and Strong: accept.
    • Qwerty prints Common: False and Strong: accept.
    • An empty string prints Password: , Common: False and Strong: accept.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free