All Courses

Hash Functions in Python

Shashank Shanu

2 years ago

hash Function in Python | insideAIML
Table of Contents
  • Introduction
  • Hash Function
  • Flowchart


       Python supports a Hash function which helps to map a large amount of data to a fixed value with a specified length. This function always ensures that an input results in the same output, which is known as a Hash sum. Hash sum helps to hold a characteristic with specific information.
It is impossible to revert this hash function, so we will almost never find a third-party attack like brute force. This kind of algorithm is also known as a one-way cryptographic algorithm.
Some of the main properties of an ideal cryptographic hash function are as follows –
  • It is almost easy to compute the hash value for any given input.
  • It is not feasible to generate the original input from a given hash value.
  • It is not feasible to modify the input without changing the hash value.
  • It is not feasible to find two different inputs with the same hash value.
Let’s take an example to get a better understanding
In this example we will how it helps in matching passwords using characters in hexadecimal format.
import uuid
import hashlib
def hash_password(password):
    # userid is used to generate a random number
    salt = uuid.uuid4().hex #salt is stored in hexadecimal value
    return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
def check_password(hashed_password, user_password):
    # for storing passwords hexdigest is used as an algorithm
    password, salt = hashed_password.split(':')
    return password == hashlib.sha256(salt.encode()
      + user_password.encode()).hexdigest()

new_pass = input('Hello User, Please enter required password ')
hashed_password = hash_password(new_pass)
print('String to store in the db is: ' + hashed_password)
old_pass = input('Please, Re-enter new password ')

if check_password(hashed_password, old_pass):
   print('You entered the right password')
   print('Sorry but the password does not match')
Hello User, Please enter required password 123
String to store in the db is: 24391247aaaaa26070f9b5f1f06c4526f9819ad511d2356ebb75ad9037b5baa8:e88744d2fda34e3b9b0d76c35bf049b1
Please, Re-enter new password 234
Sorry but the password does not match

Hello User, Please enter required password 123
String to store in the db is: a53693b8e064159fc8ad2926405fa7ac878890072c75d6415b5c1eae772a3198:46c3623f467b42f69fb19e668f491214
Please, Re-enter new password 123
You entered the right password
Now let’s try to understand with the help of a flowchart


The logic of this program is explained with the help of the following flowchart −
Flowchart | Insideaiml
Here the password entered twice matches with the hash function. It ensures that the password entered twice is accurate, which helps in gathering useful data and save them in an encrypted format.
I hope you enjoyed reading this article and finally, you came to know about Hash Functions In Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review