Start learning free

LESSON 198 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in C#: Rejecting Common Passwords with a HashSet

Practice Read the full explanation

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

You start with:

HashSet<string> common = new HashSet<string> { "password", "123456", "qwerty", "letmein" };
string 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 HashSet<string> and call .Contains(password).

HashSet<string> common = new HashSet<string> { "password", "123456", "qwerty", "letmein" };
string password = "qwerty";

Console.WriteLine("Password: " + password);
bool isCommon = common.Contains(password);
Console.WriteLine("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
    Console.WriteLine("Weak: reject");
} else {
    Console.WriteLine("Strong: accept");
}

STEP 1 OF 5
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in C#, explained

    Problem overview

    Some passwords are weak no matter how they look, because everyone tries them first. This lesson keeps a small collection of well-known passwords, common, and checks a candidate against it. The program prints three lines: the password, whether it is common, and a verdict.

    The collection is a HashSet<string> holding "password", "123456", "qwerty" and "letmein". For the password "qwerty" the output is:

    Password: qwerty
    Common: True
    Weak: reject
    

    For "Tr0ub4dor", which is not in the set, the lines are Password: Tr0ub4dor, Common: False and Strong: accept.

    Approach: ask a set whether it holds a value

    The question is "is this exact text one of the known ones?". A HashSet answers it directly with Contains. A set stores each value once and finds a value by its hash code instead of walking through all of them, so a lookup takes about the same time whether the set holds four passwords or four million. A List<string> would also have a Contains method, but it compares against the items one after another, so the work grows with the size of the list.

    The answer is a bool, which the program stores once and uses twice: in the second output line and in the verdict. The verdict is a plain if/else on that variable. A common password is rejected, whatever else is true of it.

    Step by step

    1. Print the password line. The label Password: is joined to password with +.
    2. Look it up. common.Contains(password) returns true when the set holds that exact text. Store it in a bool named isCommon.
    3. Print the second line. The ternary isCommon ? "True" : "False" picks the wording, and parentheses keep it from being swallowed by the +.
    4. Write the verdict. An if (isCommon) prints Weak: reject, and the else prints Strong: accept.

    The solution in C#

            HashSet<string> common = new HashSet<string> { "password", "123456", "qwerty", "letmein" };
            string password = "qwerty";
    
            Console.WriteLine("Password: " + password);
            bool isCommon = common.Contains(password);
            Console.WriteLine("Common: " + (isCommon ? "True" : "False"));
            if (isCommon) {
                Console.WriteLine("Weak: reject");
            } else {
                Console.WriteLine("Strong: accept");
            }
    

    The C# details worth a closer look:

    • The generic type. HashSet<string> is a set whose items are strings. It lives in the System.Collections.Generic namespace, like List<T> and Dictionary<K, V>.
    • The braces after the constructor. new HashSet<string> { "password", ... } is a collection initializer: C# calls the set's Add method once for each item. Repeating a value is harmless, because a set keeps one copy. Add returns false when the value was already there.
    • The set has no order and no indexing. There is no common[0]. A set is for membership questions, and Contains is the main one.
    • Text equality. A HashSet<string> compares strings by their content, so a different string object with the same characters is found.
    • A bool prints as True in C#. "Common: " + isCommon would print the same words as the lesson's ternary. The ternary makes the text explicit.

    Matching is exact

    By default the set compares strings exactly, including case and spaces. With this set, "QWERTY", "Qwerty" and "qwerty " (with a trailing space) are all not found, so each of them would be accepted. To make the lookup ignore case, build the set with a comparer: new HashSet<string>(common, StringComparer.OrdinalIgnoreCase). With that set, Contains("QWERTY") is true. The lesson uses the default comparison, so only an exact match is rejected.

    Edge cases and common mistakes

    • Not in the set. "Tr0ub4dor" gives Common: False and Strong: accept. The word Strong here only means "not on the list". The earlier length and character rules aren't part of this lesson's verdict.
    • Different case. "QWERTY" is not found, as described above.
    • The empty string. "" is not in the set, so it is accepted. Contains never fails on it.
    • A null password. Contains(null) is allowed on a HashSet<string> and returns false, because the set does not hold null.
    • Forgetting to store the result. Calling common.Contains(password) twice works, but a named bool is clearer and does the lookup once.
    • Reversing the verdict. Weak: reject belongs to the true branch. A password found in the set is the bad one.

    Testing

    • "qwerty" gives Password: qwerty, Common: True, Weak: reject.
    • "Tr0ub4dor" gives Password: Tr0ub4dor, Common: False, Strong: accept.
    • "letmein" gives Common: True and Weak: reject.
    • "QWERTY" gives Common: False and Strong: accept.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free