saffieee
saffieee

Reputation: 19

Checking Password Validation in Python

Define a function to set a password, check the string if it is has one upper case, if it has at least one number and of it has maximum 8 characters, display successful password or try again.

password = str(input("Please enter your password: "))
len_pass = len(password)
upper_pass = password.upper()
number = ['1', '2', '3', '4', '5', '6', '7', '8', '9']


def pass_check(word, upper_word, integer, x):
    for letter in word:
        for num in integer:
            for element in upper_word:
                if element in letter:
                    if num == letter:
                        if x < 8:
                            return 1
    return 2

result = pass_check(password, upper_pass, number, len_pass)
if result == 1:
    print("Successful Password")
elif result == 2:
    print("Fail")

Upvotes: 1

Views: 1304

Answers (2)

Thegerdfather
Thegerdfather

Reputation: 409

@Phoenix's solution is good but not optimized for performance. I've taken their code and made some tweaks.

def check_password(password):
    if len(password) > 8:
        return False

    has_uppercase = False
    has_number = False

    for c in password:
        if not has_uppercase and c.isupper():
            if has_number:
                return True
            has_uppercase = True
        elif not has_number and c.isdigit():
            if has_uppercase:
                return True
            has_number = True

    return False

password = str(input("Please enter your password: "))

if check_password(password):
    print("Successful password")
else:
    print("Try again")

Benchmarks:

# check_password.py

import random
import string


def rand_password(max_length):
    return "".join(
        random.choices(
            string.ascii_letters + string.digits, k=random.randint(1, max_length)
        )
    )


def check_password_1(password):
    # Check if password has at least one uppercase letter
    has_uppercase = any(c.isupper() for c in password)
    # Check if password has at least one number
    has_number = any(c.isdigit() for c in password)
    # Check if password is no more than 8 characters long
    is_short_enough = len(password) <= 8

    # Return True if password meets all criteria, False otherwise
    return has_uppercase and has_number and is_short_enough


def check_password_2(password):
    if len(password) > 8:
        return False

    has_uppercase = False
    has_number = False

    for c in password:
        if not has_uppercase and c.isupper():
            if has_number:
                return True
            has_uppercase = True
        elif not has_number and c.isdigit():
            if has_uppercase:
                return True
            has_number = True

    return False
In [1]: import random
   ...: from check_password import rand_password, check_password_1, check_password_2

In [2]: random.seed(1)

In [3]: %timeit check_password_1(rand_password(16))
5.38 µs ± 50.4 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [4]: random.seed(1)

In [5]: %timeit check_password_2(rand_password(16))
4.3 µs ± 62.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Upvotes: 1

forecastman
forecastman

Reputation: 61

Using regular expressions:

import re

def pass_check(passwd):
    if len(passwd) <= 8 and re.search(r"\d", passwd) and re.search(r"[A-Z]", passwd):
       return True 
    return False

if pass_check(password):
    print("Successful password")
else:
    print("Try again")

The function pass_check() checks for:

  • len(passwd) <= 8 password length is at most 8
  • re.search(r"\d", passwd) searches for a digit character, returns None (evaluates to False) if doesn't find any
  • re.search(r"[A-Z]", passwd) searches for an uppercase character, returns None if doesn't find any.

If you wish to check for exactly ONE uppercase character, put len(re.findall(r"[A-Z]", passwd))==1 instead of the last expression. The re.findall(r"[A-Z]", passwd) part simply returns a list with all the uppercase characters in the string.

Upvotes: 2

Related Questions