Start learning free

LESSON 172 OF 404 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 - Common Password Dictionary in Go

Practice

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

    Password Strength V5 in Go

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