Table of Contents
What is password hashing?
Steps to use
Step 1: Create functions for different hashing methods
grammar
algorithm
Example
Output
Step 2: Get the password string entered by the user
Step 3: Accept user input to select hashing method
in conclusion
Home Backend Development Python Tutorial What's the coolest program you've ever done in Python?

What's the coolest program you've ever done in Python?

Sep 08, 2023 pm 09:21 PM
nested functions iterators decorators

Whats the coolest program youve 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.

What is password hashing?

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.

Steps to use

  • 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

Step 1: Create functions for different hashing methods

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.

grammar

1

2

def hash_with_MD5(message):

   print ("MD5:", hashlib.md5(message).hexdigest())

Copy after login

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.

algorithm

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

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

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)

Copy after login

Output

1

2

3

4

5

6

Hashed with MD5: 6c60b3cfe5124f982eb629e00a98f01f

Hashed with SHA:

15e6e9ddbe43d9fe5745a1348bf1535b0456956d18473f5a3d14d6ab06737770

Hashed with blake2b:

109f6f017d7a77bcf57e4b48e9c744280ae7f836477c16464b27a3fe62e1353c70ec4c7f938080

60ee7c311094eede0235a43151c3d2b7401a3cb5a8f8ab3fbb

Copy after login

Step 2: Get the password string entered by the user

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.

grammar

1

password=input("message").encode()

Copy after login

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.

algorithm

Step 1 - Use the input() function to receive user input

Step 2- Convert input to encoding format

Example

1

password=input(“Enter the password for hashing: ”).encode()

Copy after login

Output

1

Enter the password for hashing: Python

Copy after login

Step 3: Accept user input to select hashing method

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.

grammar

1

2

3

4

5

while True:

   choice = input("Enter choice(1/2/3): ")

      if choice in ('1', '2', '3'):

      try:

      …………………

Copy after login

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.

algorithm

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

Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

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")

Copy after login

Output

1

2

3

4

5

6

7

8

9

10

11

12

13

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

Copy after login

in conclusion

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles