Home >Backend Development >Python Tutorial >What's the coolest program you've ever done in Python?
The coolest Python program I’ve ever made is the Python Password Hasher. Let’s first understand what Python password hashing is.
Python password hashing is an advanced form of encryption that can be used to securely store passwords online. In today's interconnected world, user passwords are one of the most vulnerable pieces of sensitive information on the Internet. Convert a password string into a string of random characters using different hashing algorithms, which are used in my program. The user is instructed to enter a password string and then select the appropriate hashing algorithm to use. The output hash is then displayed, which can be stored online.
Create functions for different hashing methods
Accept the password string entered by the user
Accept user input to select hashing method
Convert the string and provide output
First, we create different functions that take the password string as a parameter and convert it into ciphertext form. The ciphertext is actually the hashed form of the data. Different functions contain different hashing algorithms.
def hash_with_MD5(message): print ("MD5:", hashlib.md5(message).hexdigest())
This function takes a message as a parameter and converts it to ciphertext using the MD5 hashing algorithm. Then print the hash digest for the user. If instead of using MD5, you use another hash algorithm, the syntax is the same, only the call to the hash function changes.
Step 1 - Define different functions for different hashing algorithms
Step 2 - Use the string entered by the user as the parameter of the function
Step 3 - In the body of the function, print the hexadecimal digest of the hashed password
def hash_with_MD5(message): encoded=message.encode() print ("Hashed with MD5:", hashlib.md5(encoded).hexdigest()) def hash_with_SHA(message): encoded=message.encode() print ("Hashed with SHA:", hashlib.sha256(encoded).hexdigest()) def hash_with_blake(message): encoded=message.encode() print ("Hashed with blake2b:", hashlib.blake2b(encoded).hexdigest()) message='tutorialspoint' hash_with_MD5(message) hash_with_SHA(message) hash_with_blake(message)
Hashed with MD5: 6c60b3cfe5124f982eb629e00a98f01f Hashed with SHA: 15e6e9ddbe43d9fe5745a1348bf1535b0456956d18473f5a3d14d6ab06737770 Hashed with blake2b: 109f6f017d7a77bcf57e4b48e9c744280ae7f836477c16464b27a3fe62e1353c70ec4c7f938080 60ee7c311094eede0235a43151c3d2b7401a3cb5a8f8ab3fbb
The next step is to get input from the user for the password that needs to be stored. For security reasons, the password to be stored must be hashed, and the user-entered password must be encoded before hashing to ensure that it is suitable for passing to the hash function. This encoding operation is performed by the encode() function.
password=input("message").encode()
The password we receive from the user using the input() function cannot be used for hashing, so it is encoded using the encode() function. These two steps are combined here in one command for ease of coding and simplicity.
Step 1 - Use the input() function to receive user input
Step 2- Convert input to encoding format
password=input(“Enter the password for hashing: ”).encode()
Enter the password for hashing: Python
We will provide users with a choice as to which hashing algorithm we will use to securely hash passwords. Different methods have different advantages and disadvantages, so we let users choose the method that works best for a specific password. Here we use a simple If-else structure to determine the selection entered by the user.
while True: choice = input("Enter choice(1/2/3): ") if choice in ('1', '2', '3'): try: …………………
Here we ask the user what type of hash they performed along with a list of options. The input is then checked against a list of valid inputs and, if true, the required action is performed. Otherwise, program control will break out of the loop.
Step 1 − Request user input
Step 2- Check if user input is valid
Step 3 - Perform the selected action
Step 4 - Ask if you want to perform more actions
import hashlib def hash_with_MD5(password): #encoded=password.encode() print ("Hashed with MD5:", hashlib.md5(password).hexdigest()) def hash_with_SHA(password): #encoded=password.encode() print ("Hashed with SHA:", hashlib.sha256(password).hexdigest()) def hash_with_blake(password): #encoded=password.encode() print ("Hashed with blake2b:", hashlib.blake2b(password).hexdigest()) print("Select hashing operation.") print("1.MD5") print("2.SHA") print("3.blake") while True: # take input from the user choice = input("Enter choice(1/2/3): ") # check if choice is one of the four options if choice in ('1', '2', '3'): try: password=input('Enter the password for hashing: ').encode() except ValueError: print("Invalid input. Please enter a string.") continue if choice == '1': hash_with_MD5(password) elif choice == '2': hash_with_SHA(password) elif choice == '3': hash_with_blake(password) # checking if user wants another calculation # break the while loop if answer is no next_calculation = input("Let's do next calculation? (yes/no): ") if next_calculation == "no": break else: print("Invalid Input")
Select hashing operation. 1.MD5 2.SHA 3.blake Enter choice(1/2/3): 2 Enter the password for hashing:Python Hashed with SHA: 18885f27b5af9012df19e496460f9294d5ab76128824c6f993787004f6d9a7db Let's do next calculation? (yes/no): yes Enter choice(1/2/3): 1 Enter the password for hashing:Tutorialspoint Hashed with MD5: da653faa9f00528be9a57f3474f0e437 Let's do next calculation? (yes/no): no
So here we build a program that hashes user passwords and returns them for secure storage. The program runs successfully and serves an important purpose. Further modifications can be made to implement newer functionality, which we will do later.
The above is the detailed content of What's the coolest program you've ever done in Python?. For more information, please follow other related articles on the PHP Chinese website!