Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python password strength [duplicate]

I am programming a password strength code on python and i'm trying to find out if my password (p) contains a number, I have found out how to see if it contains upper and lower case letter by p.isupper() or p.islower(). I have also put them two together. My friend has told me how to see if the password only contains number but I need your help now.

running=True
while running:
    p=raw_input("What is your Password? ")
    if len(p) <6:
        print "Your Password is too short"
    if len(p) >12:
        print "Your Password is too long"
    if len(p) == 6 or 7 or 8 or 9 or 10 or 11 or 12:
        print "Password Length OK"
        running=False

print "Loop Broken" #this will be deleted, only for my help now

if p.isupper():
    print "Your Password is weak as it only contains capital letters"

if p.islower():
    print "Your Password is weak as it only contains lower case letters"

if p.isupper and p.islower:
    print "Your Password is of medium strength, try adding some numbers"

try:
    int(p)
    print "Your Password is weak as it only contains numbers"
except (ValueError, TypeError):
    pass

All I need now is the code for, if the password contains lower or upper case letters and numbers.

like image 615
Anonymous Avatar asked Mar 23 '23 13:03

Anonymous


2 Answers

To me, regex would definitely be the easiest way of going about this.

Given a sample password password, the way you would check it would be:

import re
# Check if contains at least one digit
if re.search(r'\d', password):
    print "Has a digit"
# Check if contains at least one uppercase letter
if re.search(r'[A-Z]', password):
    print "Has uppercase letter"
# Check if contains at least one lowercase letter
if re.search(r'[a-z]', password):
    print "Has lowercase letter"

For your other pieces, you can continue to use .isupper() and .islower().

By the way, this portion of your code:

if p.isupper and p.islower:
    print "Your Password is of medium strength, try adding some numbers"

Will not function how you want it to. First, you're not actually calling the methods, since you haven't put the parentheses--you need to write if p.isupper() and p.islower():. Second, this doesn't actually do what you want it to. You're trying to check that it contains both lowercase and uppercase numbers. Instead, you're checking both that it is completely uppercase and completely lowercase, and obviously it can't be both, so that if statement will always return False. Instead, you'll want to use something like:

if re.search(r'[a-z]', password) and re.search(r'[A-Z]', password):

Or, alternatively, without re:

import string
if any(letter in string.ascii_lowercase for letter in password) and \
  any(letter in string.ascii_uppercase for letter in password):

Or:

if any(letter.islower() for letter in password) and \
  any(letter.isupper() for letter in password):

I happen to prefer re because it's more concise.

like image 84
jdotjdot Avatar answered Mar 31 '23 20:03

jdotjdot


I guess you want to check whether your password contains all of these : lowercase, uppercase and digits.

>>> from string import ascii_lowercase, ascii_uppercase, digits
>>> s_lc = set(ascii_lowercase)
>>> s_uc = set(ascii_uppercase)
>>> s_d = set(digits)
def func(strs):
    s = set(strs)
    return all(s & x for x in (s_lc,s_uc,s_d ))
... 
>>> func('fooBar09')
True
>>> func('fooBar')
False
>>> func('900')
False
>>> func('900aB')
True

Secondly len (p) == 6 or 7 or 8 or 9 or 10 or 11 or 12 is going to be evaluated as:

(len(p) == 6) or 7 so if len(p) is 6 then it returns True else 7(which is a Truthy value as well)

it should be : if 6 <= len(p) <= 12

>>> 7 or 8 or 9 or 10 or 11 or 12
    7

Use while True instead of using a flag variable, and you need if-elif-else conditions here not if-if:

while True:
    p=raw_input("What is your Password? ")
    le = len(p)
    if le <6:
        print "Your Password is too short"
    elif le >12:
        print "Your Password is too long"
    elif 6 <= le <= 12:
        print "Password Length OK"
        break
like image 27
Ashwini Chaudhary Avatar answered Mar 31 '23 20:03

Ashwini Chaudhary