Resetting Password Using Python
In this discussion, we explore creating a robust Python password reset tool using the regex library. We define a specific pattern for the password format, enhancing security. The tool incorporates parameters ensuring passwords meet criteria like uppercase/lowercase letters, digits, and special characters. It strategically uses Python libraries, such as getpass
for secure input and time
for a delay, adding security, and improving user experience. The article provides a step-by-step process, offering insights into each development stage. By the end, you’ll not only have a functional password reset tool but also a deep understanding of Python’s libraries and regex for secure password management.
Reset Password Using Python
Follow the steps below to reset a password using Python.
Step 1: Install Necessary libraries
In this step, we are going to install getpass and regex library.
pip install getpass4
Step 2: Import Libraries
In this step will be importing all the required libraries in our Python program.
Python3
import re import time from getpass import getpass |
Step 3: Create a Pattern For Your Password Using Regex
In this step we will be creating a pattern for the password to be entered by the user. We be following some basic case for password such as
- A password must starts with capital letter.
- A password must contains a digit, a special character.
- It must be at least 8 characters long.
Breakdown of the pattern
- (?=.*[a-z]|[A-Z]) this will check if it contains a letter or not.
- (?=.*\d) this will check for digit.
- (?=.*[!@#$%^&*()_+{}|:<>?/~`]) this will check for special character.
- [A-Za-z\d!@#$%^&*()_+{}|:<>?/~`]{8,} this will check for length.
Python3
#create a pattern for your desired paasword type pattern = re. compile (r '^(?=.*[a-z]|[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}|:<>?/~`])[A-Za-z\d!@#$%^&*()_+{}|:<>?/~`]{8,}$' ) def check(password): if bool (pattern.match(password)) and password[ 0 ].isupper(): return True return False |
Step 4: Create a Log in Method
In this step we will create a logged-In method that will be log us in our website or app after successfully resetting the password.
Python3
def loggedIn(): #Login to the wesbiste/App time.sleep( 5 ) print ( "Logged in Sucessfully........." ) |
Step 5: Create a Main Method
In this step, we’ll establish the main method responsible for receiving user input and validating specified conditions. Let’s examine the code implementation. Within this step, the check() method is invoked, verifying the basic measures prompted to the user during password entry. Additionally, we prompt the user to input the password twice, allowing for correction of any typing errors unnoticed during the initial entry. This step encompasses the invocation of all the previously created methods.
Python3
def main(): print ( "Entered password must:-" ) print ( " -Starts with Capital Letter" ) print ( " -conatins a digit and special character" ) print ( " -at least 8 characters long" ) password1 = getpass( "Enter the Password: " ) if check(password1) = = False : print ( "Enter a vaild password" ) else : password2 = getpass( "Enter the Password again: " ) if password1 = = password2: print ( "Successfully changed password" ) loggedIn() else : print ( "You entered a differnt password" ) if __name__ = = '__main__' : main() |
Complete Code
Python3
import re import time from getpass import getpass #create a pattern for your desired paasword type pattern = re. compile (r '^(?=.*[a-z]|[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+{}|:<>?/~`])[A-Za-z\d!@#$%^&*()_+{}|:<>?/~`]{8,}$' ) def check(password): if bool (pattern.match(password)) and password[ 0 ].isupper(): return True return False def loggedIn(): #Login to the wesbiste/App time.sleep( 5 ) print ( "Logged in Sucessfully" ) def main(): print ( "Entered password must:-" ) print ( " -Starts with Capital Letter" ) print ( " -conatins a digit and special character" ) print ( " -at least 8 characters long\n" ) password1 = getpass( "Enter the Password: " ) if check(password1) = = False : print ( "Enter a vaild password" ) else : password2 = getpass( "Enter the Password again: " ) if password1 = = password2: print ( "Successfully changed password" ) loggedIn() else : print ( "You entered a differnt password" ) if __name__ = = '__main__' : main() |
Output :