- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What's the coolest program you've made in Python?
The coolest Python program I have ever made is Python password hasher. Let's first understand what python password hashing is.
What is Password Hashing?
Python Password hashing is a form of advanced encryption which can be used to store passwords online safely. In today’s world of everything being online, user passwords are one of the most easily attacked sensitive information on the internet. The password string is converted to a string of random characters using different hashing algorithms, which have been used in my program. The user is instructed to input the password string, then select the appropriate hashing algorithm to be used. The output hash is then shown, which can be stored online.
Steps Used
Create the functions for different hashing methods
Take user input for password string
Take user input for choice of hashing method
Convert the string and provide output
Step 1: Create the functions for different hashing methods
First, we create the different functions which take as an argument the password string and convert it into the ciphertext form. Ciphertext is actually the form of the data after it has been hashed. The different functions contain different hashing algorithms.
Syntax
def hash_with_MD5(message): print ("MD5:", hashlib.md5(message).hexdigest())
Here the function takes the message as the argument, and converts it into ciphertext using the MD5 hashing algorithm. The hash digest is then printed for the user. Instead of MD5, if any other hashing algorithm is used, then the syntax will be the same, just the calling of the hash function will be changed.
Algorithm
Step 1 − Define the different functions for different hashing algorithms
Step 2 − Take the user inputted string as argument for the function
Step 3 − In the function body, print the hexdigest of the hashed password
Example
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)
Output
Hashed with MD5: 6c60b3cfe5124f982eb629e00a98f01f Hashed with SHA: 15e6e9ddbe43d9fe5745a1348bf1535b0456956d18473f5a3d14d6ab06737770 Hashed with blake2b: 109f6f017d7a77bcf57e4b48e9c744280ae7f836477c16464b27a3fe62e1353c70ec4c7f938080 60ee7c311094eede0235a43151c3d2b7401a3cb5a8f8ab3fbb
Step 2: Take user input for password string
The next step is taking input from the user, for the password that needs to be stored. The password to be stored must be hashed for security reasons, and before hashing, the password inputted by the user must be encoded to ensure it is suitable for passing to the hashing function. This encoding action is performed by the encode() function.
Syntax
password=input("message").encode()
The password that we receive from the user using the input() function cannot be used for hashing, thus it is encoded using the encode() function. These two steps are combined in a single command here, for ease of coding and simplicity.
Algorithm
Step 1 − Receive input from user using input() function
Step 2 − Convert the input into encoded format
Example
password=input(“Enter the password for hashing: ”).encode()
Output
Enter the password for hashing: Python
Step 3: Take user input for choice of hashing method
We will provide users choices, as to which hashing algorithm we will use for securely hashing the password. There are different benefits and demerits of different methods, so we have given the user the choice to decide which will be the most suitable for the particular password. Here we use a simple If-else structure to determine the choice inputted by the user.
Syntax
while True: choice = input("Enter choice(1/2/3): ") if choice in ('1', '2', '3'): try: …………………
Here we ask the user the type of hashing they perform, along with the list of options. The input is then checked for in a valid list of inputs, if it is true, then the asked operation is performed. Otherwise, the program control breaks out of the loop.
Algorithm
Step 1 − Ask user for input
Step 2 − Check if user input is valid
Step 3 − Perform actions as selected
Step 4 − Ask if more actions are to be performed
Example
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")
Output
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
Conclusion
Thus here we have built the program for hashing user passwords and returning for safe storage. The program runs successfully and solves an important purpose. Further modifications can be also done to implement newer features, which we will do later.