Python Extras

Password Authentication using Python

Password Authentication using Python

To create a password authentication system using Python you have to follow the steps mentioned below:

  1. Create a dictionary of usernames with their passwords.
  2. Then you have to ask for user input as the username by using the input function in Python.
  3. Then you have to use the getpass module in Python to ask for user input as the password. Here we are using the getpass module instead of the input function to make sure that the user doesn’t get to see what he/she write in the password field.

So let’s follow the steps mentioned above to create a password authentication system using Python:

import getpass
2database = {“aman.kharwal”: “123456”, “kharwal.aman”: “654321”}
3username = input(“Enter Your Username : “)
4password = getpass.getpass(“Enter Your Password : “)
5for i in database.keys():
6if username == i:
7while password != database.get(i):
8password = getpass.getpass(“Enter Your Password Again : “)
9break
10print(“Verified”)
Enter Your Username : aman.kharwal
Enter Your Password : ··········
Enter Your Password Again : ··········
Enter Your Password Again : ··········
Verified

Summary

So this is how we can authenticate the identity of a user by using the Python programming language. Now you can try the same logic with more usernames and other data structures also. I hope you liked this article on how to create a password authentication system using Python. Feel free to ask your valuable questions in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button