MD5 Hashing in Python

Code Example


This hash function accepts a sequence of bytes and returns 128-bit hash value, usually used to check data integrity but has security issues. There are many hash functions defined in the “hashlib” library in python.


# What is Hash?

Hash is a function that takes a variable-length sequence of bytes as input and converts it to a fixed-length sequence. However, to get your original data(input bytes) back is not easy. For example, x is your input and f is the f is the hashing function, then calculating f(x) is quick and easy but trying to obtain x again is a very time-consuming job.

 

{'md4', 'sha512', 'sha3_512', 'whirlpool', 'sha1', 'sha3_256', 'sha3_384', 'mdc2', 'ripemd160', 'md5-sha1', 'sha3_224', 'md5', 'shake_128', 'sha512_224', 'shake_256', 'blake2b', 'sha224', 'sha512_256', 'sm3', 'blake2s', 'sha256', 'sha384'}

this many buitlin algo are available in hashlib


Example 1:

import hashlib

message = input("Enter your message : \n")

txt = message.encode()

hash_code = hashlib.md5(txt).hexdigest()

print("Hash Code : {} Lenght is : {}".format(hash_code, len(hash_code)))


Output: 

Enter your message : 

Hey, how are you ?

Hash Code : 5bb6387a23390ca186b57ec08acd7ace Lenght is : 32



Example 2:

import binascii

message = input("Enter your message : \n")

hash_code = binascii.crc32(message.encode('utf8'))

print("Hash Code : {} Lenght is : {}".format(hash_code, len(hash_code)))


Output: 

Enter your message : 

Hey, how are you ?

Hash Code : 1151853981 Lenght is : 10







Enjoy Hashlib Tutorial 😉


Quiz Level 1

Quiz Level 2


Request Me

Comments

Popular posts from this blog

How To Create .ENV File in Python

Create a Large file for Test Data Processing using Python

How to solve the Liner Equation using Python