w3resource

Python Cyber Security - Check password strength

Python Cyber Security: Exercise-3 with Solution

Write a Python program to check if a password meets the following criteria:

At least 8 characters long and

Contains at least one uppercase letter, one lowercase letter, one digit, and one special character (!, @, #, $, %, or &)

If the password meets the criteria, print a message that says "Valid Password." If it doesn't meet the criteria, print a message that says "Password does not meet requirements."

Sample Solution:

Python Code:

# Solution to Exercise 3
import re

def validate_password(password):
    # Check if the password has at least 8 characters
    if len(password) < 8:
        return False
    
    # Check if the password contains at least one uppercase letter
    if not re.search(r'[A-Z]', password):
        return False
    
    # Check if the password contains at least one lowercase letter
    if not re.search(r'[a-z]', password):
        return False
    
    # Check if the password contains at least one digit
    if not re.search(r'\d', password):
        return False
    
    # Check if the password contains at least one special character
    if not re.search(r'[[email protected]#$%^&*(),.?":{}|<>]', password):
        return False
    
    # If all the conditions are met, the password is valid
    return True

password = input("Input your password: ")
is_valid = validate_password(password)

if is_valid:
    print("Valid Password.")
else:
    print("Password does not meet requirements.")

Sample Output:

Input your password:  Aji1#der
Valid Password.

Input your password:  A123d$%h
Valid Password. 

Input your password:  A12&ki1
Password does not meet requirements.

Input your password:  KI342$&H
Password does not meet requirements.

Flowchart:

Flowchart: Check password strength

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Generate random passwords of specified length.
Next: Generate random passwords of specified length.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value:

Example:

def tips_map_values(obj, fn):
  ret = {}
  for key in obj.keys():
    ret[key] = fn(obj[key])
  return ret
users = {
  'Owen': { 'user': 'Owen', 'age': 29 },
  'Eddie': { 'user': 'Eddie', 'age': 15 }
}

print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}

Output:

{'Owen': 29, 'Eddie': 15}

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook