-1

I am making a password strength checker as a school project, but I am struggling to find a way to check how many integers are in the password, as such I am checking the password on three things, but I just cannot figure out how to check for integers. If the solution you guys could help me with could include the re library that would be even better. Thanks!

Here is my problematic code so far.

    import re

    i_pwdStrength = i_pwdLength + i_pwdChar + i_pwdInt
    i_pwdLength = 0
    i_pwdChar = 0
    i_pwdInt = 0
    i_attempts = 0
    numbAttemps = int(input("Enter the amount of passwords you would like to check \n(MUST BE A NUMBER)"))
    pwd = input("Enter Password")


    while numbAttempts < i_attempts
        pwd = pwd.lower()

    i_pwdLength = pwd.length
    if i_pwdLength <= 3:
        i_pwdLength = 1
    elif i_pwdLength > 3 && <= 6:
        i_pwdLength = 2
    elif i_pwdLength > 6 && <= 9:
        i_pwdLength = 3
    else i_pwdLength > 9:
        i_pwdLength = 4

    i_pwdChar =      #This is a very long way of writing this code as I could not find a way to compress these lines. I may have used the 'or' command but I could not
    if re.search(r'[x]',pwd):              # work out how to integrate it and i thought that this code looked neater
        i_pwdChar = 1
    elif re.search(r'[w]'pwd):
        i_pwdChar = 1
    elif re.search(r'[y]'pwd):
        i_pwdChar = 1
    elif re.search(r'[z]'pwd):
        i_pwdChar = 1
    elif re.search(r'[x]',pwd) and re.search(r'[w]',pwd):
        i_pwdChar = 2
    elif re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 2
    elif re.search(r'[x]',pwd) and re.search(r'[z]',pwd):
        i_pwdChar = 2
    elif re.search(r'[w]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 2
    elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd):
        i_pwdChar = 2
    elif re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
        i_pwdChar = 2
    elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 3
    elif re.search(r'[z]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 3
    elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd) and re.search(r'[y]',pwd):
        i_pwdChar = 3
    else re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd)
        i_pwdChar = 4

Due to all the help I have received I have finished the code.

    import re

    i_numbAttempts = int(input("How many passwords do you want to try?\nMUST BE A NUMBER!")) # Determines how many times the while loop further down
    attempts = 0    # sets the variable attempts to 0 to ensure that the while loop works                # repeats itself

    while i_numbAttempts > attempts:      # while the passwords entered is more than attempts:
        pwd = input("Enter Password: ")   # user enters password to check
        pwd = pwd.lower()                 # sets the password to lowercase (QjwtwWyeRvgTRDU would become qjwtwwyervgtrdu)

        if len(pwd) == 0:                        # if the user does not enter a password, it outputs: You must ypre something!
            print("You must type something!")
            i_pwdLength = 0
        elif len(pwd) >= 1 and len(pwd) <= 3:    # if the length is a certain length,
            i_pwdLength = 1                      # it will assign a corresponding value to the i_pwdLength variable
        elif len(pwd) > 3 and len(pwd) <= 6:     
            i_pwdLength = 2
        elif len(pwd) > 6 and len(pwd) <= 9:
            i_pwdLength = 3
        elif len(pwd) > 9:
            i_pwdLength = 4

        if re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
            i_pwdChar = 4
        elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
            i_pwdChar = 3
        elif re.search(r'[z]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd):
            i_pwdChar = 3
        elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd) and re.search(r'[y]',pwd):
            i_pwdChar = 3
        elif re.search(r'[w]',pwd) and re.search(r'[x]',pwd) and re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
            i_pwdChar = 4
        elif re.search(r'[x]',pwd) and re.search(r'[w]',pwd):              # This section checks the password for any 
            i_pwdChar = 2
        elif re.search(r'[x]',pwd) and re.search(r'[y]',pwd):               # Combination of the letters w,x,y and z.
            i_pwdChar = 2
        elif re.search(r'[x]',pwd) and re.search(r'[z]',pwd):               # I could not find a way to compress these
            i_pwdChar = 2
        elif re.search(r'[w]',pwd) and re.search(r'[y]',pwd):               # Lines so i left them as is as i thought
            i_pwdChar = 2
        elif re.search(r'[w]',pwd) and re.search(r'[z]',pwd):               # That it looked neater
            i_pwdChar = 2
        elif re.search(r'[y]',pwd) and re.search(r'[z]',pwd):
            i_pwdChar = 2
        elif re.search(r'[x]',pwd):
            i_pwdChar = 1
        elif re.search(r'[w]',pwd):
            i_pwdChar = 1
        elif re.search(r'[y]',pwd):
            i_pwdChar = 1
        elif re.search(r'[z]',pwd):
            i_pwdChar = 1
        else:
            i_pwdChar = 0

        if (len([l for l in pwd if l.isdigit()])) == 0:                                                     # This code checks how many integers are included
            i_pwdInt = 0
        elif (len([l for l in pwd if l.isdigit()])) >= 1 and (len([l for l in pwd if l.isdigit()])) < 3:    # In the password, and assigns a number to the 
            i_pwdInt = 1
        elif (len([l for l in pwd if l.isdigit()])) >= 3 and (len([l for l in pwd if l.isdigit()])) < 5:    # Variable i_pwdInt accordingly
            i_pwdInt = 2
        elif (len([l for l in pwd if l.isdigit()])) >=5 and (len([l for l in pwd if l.isdigit()])) <7:
            i_pwdInt = 3
        elif (len([l for l in pwd if l.isdigit()])) >=7:
            i_pwdInt = 4

        i_pwdStrength = i_pwdLength + i_pwdChar + i_pwdInt     # this determines the passwords overall strength

        if i_pwdStrength > 0 and i_pwdStrength <= 4 :
            print("This password is bad")
        elif i_pwdStrength > 4 and i_pwdStrength <= 8:
            print("This password is OK")
        elif i_pwdStrength > 8 and i_pwdStrength <= 11:
            print("this password is good")
        elif i_pwdStrength == 12:
            print("this password is the best!!")

        attempts = attempts + 1
Student
  • 25
  • 1
  • 9
  • 1
    A better way to calculate password strength is to use entropy: https://xkcd.com/936/ – duffymo Jun 03 '16 at 09:09
  • Have you tried splitting the string into characters and counting the occurrences of digits that way? You won't need to use regex this way. – Farhan.K Jun 03 '16 at 09:09
  • No, and im sorry to say i wouldn't happen to know how. – Student Jun 03 '16 at 09:10
  • There's probably a library out there that does this for you, and i would go looking for it but as i say it is for school and i have to write the code. – Student Jun 03 '16 at 09:11
  • The code in your question is not indented properly – Farhan.K Jun 03 '16 at 09:12
  • hmm. i would run it on an ide but i cant on the laptop i am using at the moment – Student Jun 03 '16 at 09:14
  • `for c in pwd: pwd = pwd.lower()` fyi This line is unnecessary. If you want to convert to lowercase you simply need `pwd = pwd.lower()`. The `for` loop is not needed. – Farhan.K Jun 03 '16 at 09:18
  • How so? I change the letters to lowercase so i can check with regex more simply. Otherwise my thinking was that i would have to check for both cases of each letter I was checking for, and if the password did include integers, the ide we use at school will play up – Student Jun 03 '16 at 09:21
  • Checking both cases would make the password checker more accurate.. but either way, `for c in pwd: pwd = pwd.lower()` converts `pwd` to lowercase, not the character. And it does this `n` times where `n` is the length of the password. – Farhan.K Jun 03 '16 at 09:24
  • Thank you. Do you have any ideas on compressing the lines needed to check i_pwdChar ? i would be really grateful – Student Jun 03 '16 at 09:27

1 Answers1

4

I think you should use isdigit method. The following command will return you the number of digits in your password.

len([l for l in pwd if l.isdigit()])
Eduard Stepanov
  • 1,183
  • 8
  • 9