Password managers are essential in replacing the old school notepad of writing passwords down. Just in case it gets lost or gets burned in a fire it can be quite a pain to recover. Instead it’s possible to keep passwords on the cloud or on multiple devices that are encrypted with the latest encryption algorithms. No matter what happens to the device they will be kept secured. It’s also possible to recover very easily on a new device. THAT IS A KEY DIFFERENCE. It’s extraordinarily diverse with features being able to store credit cards, personal info, private keys etc. Not all password managers have the same features but, if you don’t see a password manager that meets your needs. Why not create your own?
import hashlib
import getpass
password_manager = {}
def create_account():
username = input("Enter your desired username: ")
password = getpass.getpass("Enter your desired username: ")
hashed_password = hashlib.sha256(password.encode()).hexdigest()
password_manager[username] = hashed_password
print("Account created succcessfully!")
def login():
username = input("Enter your username: ")
password = getpass.getpass("Enter your username: ")
hashed_password = hashlib.sha256(password.encode()).hexdigest()
if username in password_manager.keys() and password_manager[username] == hashed_password:
print("Login successful!")
else:
print("Invalid username or password.")
def main():
while True:
choice = input("Enter 1 to create an account, 2 to login, or 0 to exit: ")
if choice == "1":
create_account()
elif choice == "2":
login()
elif choice == "0":
break
else:
print("Invalid choice.")
if __name__ == "_main_":
main()
It imports the hashlib and getpass library modules. It initialize an empty variable called “password manager” to store username and password credentials. It defines two main fuctions:
create_account(): This function allows a user to create an account by providing a username and password. It then hashes the password using SHA-256 and stores the username and hashed password in the “password_manager” dictionary.
‘login()’: This function prompts the user to enter their username and password, hashes the password input, and cross checks the username and hashed password match those stored in the “password_manager” dictionary. If they match, it prints “Login successful!” If not there’s a rule that specifies if otherwise it prints, “Invalid username or password.”
The ‘main()’ function defines to manage the overall flow of the program. It presents the user with options to create an account, log in, or exit in a loop. If the user inputs “1” it displays ‘create_account()’. If the user chooses “2” it displays ‘login()’. If the user chooses “0” it exits the program. If the user enters an invalid choice, it prints “Invalid choice” and nothing would occur.
This script would essentially users to create usernames and passwords, securely store hashed passwords in a dictionary, and allows a way for a user to login to verify those credentials.
Now we are going to break down the code line by line 🙂
import hashlib
import getpass
These lines import two Python library modules: ‘hashlib’ for cryptographic hash functions(used to store and check passwords securely) and ‘get pass’ for securely inputting passwords.
password_manager = {}
This line states an empty dictionary called ‘password_manager’ where the usernames and their hashed passwords will be stored.
def create_account():
username = input(“Enter your desired username: “)
password = getpass.getpass(“Enter your desired password: “)
hashed_password = hashlib.sha256(password.encode()).hexdigest()
password_manager[username] = hashed_password
print(“Account created successfully!”)
It defines a function called ‘create_account()’ that lets you create an account. It prompts the user to enter a username and password while obscuring it(using ‘getpass’). It then hashes the password using SHA-256 and stores it in the ‘password_manager’ dictionary alongside the username. After successful creation, the code will print a success message.
def login():
username = input(“Enter your username: “)
password = getpass.getpass(“Enter your password: “)
hashed_password = hashlib.sha256(password.encode()).hexdigest()
if username in password_manager.keys() and password_manager[username] == hashed_password:
print(“Login successful!”)
else:
print(“Invalid username or password.”)
This defines the funtion ‘login()’ for user login input. It prompts the user to enter their username and password securely. It then hashes the entered password and cross checks if the username exists in the password_manager’ dictionary and if the hashed password matches. If both conditions are met, it prints “Login successful!”; otherwise, it prints “Invalid username or password.”
def main():
while True:
choice = input(“Enter 1 to create an account, 2 to login, or 0 to exit: “)
if choice == “1”:
create_account()
elif choice == “2”:
login()
elif choice == “0”:
break
else:
print(“Invalid choice.”)
This main function is called a wild loop and its default state is always True until its given a break statement. So its given a choice of input choices. If its “1” we may ‘create_account(). If its choice “2” we may login (). If its anything else we break and print out a statement stating “Invalid choice.”
if __name__ == “_main_”:
main()
Lastly, this is known as a python idiom. It’s basically a way of ordering our functions in order. In this case we want our main function to go first to be prompted to start the program.
That is it to establish a simple password manager in getting you started in having yourself or your organzation keeping your credentials secure. I hope this helped as much as I enjoyed making it for you until next time, be legendary! 🙂