[foogallery id=”1024″]







How to Use Cryptographic Hashes Using Python

#Author: ManishHacker1

How to Use Cryptographic Hashes Using Python

Hello everyone, Today we will be going to learn about Cryptographic Hashes and message digest.

What is hash Function?

A cryptographic hash function is a function which takes an input or message and convert it to a fixed-size alphanumeric bit string.The output string is called the ‘hash value’, ‘hash code’, ‘message digest’, ‘digital fingerprint’, ‘digest’ or ‘checksum’).

It is a one way hash function. It means if “f” is a hashing function and we convert in to F(x) is very fast and easy. But if you want to obtain again “x” value, It will take many years.

Now suppose you want to hash the string “ManishHacker1” with the md5 Function, the result is f9dece57a97af246e7132b02d5c76d77

Uses of Hash Function

One use is a data structure called a hash table, widely used in computer software for rapid data lookup.

  • One use is a data structure called a hash table
  • Cryptographic Algorithm
  • Database lookup by detecting duplicated records in a large file.For example is finding similar stretches in DNA sequences.
  • In digital signatures
  • Fingerprints
  • Password Storage
  • Message authentication code
  • Checksums



and many more.

How to Use Cryptographic Hashes Using Python

In python contain a hashlib module which is provide some most popular hashes algorithm. Today we will be learn, how to use hashes algorithm with the help of hashlib module.

Hashlib module is “backed” by OpenSSL, all of of the algorithms provided by that library are available, including:

  • md5
  • sha1
  • sha224
  • sha256
  • sha384
  • sha512

Uses md5:


import hashlib
key = input('Enter String to hash: ')
# Assumes the default UTF-8
hash_value = hashlib.md5(key.encode())
print hash_value.hexdigest()


The above example calculate the md5 and prints the HEX digest of that string.
If you want to sequence of byte then we should be use digest instead.

Uses SHA1:


import hashlib
key = hashlib.sha1(b'ManishHacker1')
hash_value = key.hexdigest()
print hash_value

In above code we used ‘b’ string because this converts the string to bytes.the Hashing function only takes a sequence of bytes as a parameter.

Uses SHA224:


import hashlib
key = hashlib.sha224(b'ManishHacker1')
hesh_value = key.hexdigest()
print hash_value

Uses SHA256:


import hashlib
key = hashlib.sha256(b'ManishHacker1')
hash_value = key.hexdigest()
print hash_value

Uses SHA384:


import hashlib
key = hashlib.sha384(b'ManishHacker1')
hash_value = key.hexdigest()
print hash_value

Uses SHA512:


import hashlib
key = hashlib.sha512(b'ManishHacker1')
hash_value = key.hexdigest()
print hash_value

Using update function:


import hashlib

lorem = '''Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.'''

The above sample data save as hashlib_data.py in same directory where our md5.py are exist. After that calculate MD5 digest:


import hashlib

from hashlib_data import lorem

hash = hashlib.md5()
hash.update(lorem)
print hash.hexdigest()

Practicle Example

How to Use Cryptographic Hashes Using Python


import hashlib
import uuid
 
def hash_password(password):
    # uuid is used to generate a random number
    random_no = uuid.uuid4().hex
    return hashlib.md5(random_no.encode() + password.encode()).hexdigest() + ':' + random_no
    
def check_password(hash_password, user_password):
    password, random_no = hash_password.split(':')
    return password == hashlib.md5(random_no.encode() + user_password.encode()).hexdigest()
 
new_pass = raw_input('Please enter a password: ')
hash_password = hash_password(new_pass)
print('The string to store in the database is: ' + hash_password)
old_pass = raw_input('Now please enter the password again to check: ')
if check_password(hash_password, old_pass):
    print('You entered the right password')
else:
    print('Your password is does not match.')


In the above example we are hashing the password and store in our database. uuid used for generate random number sequence added to the password before using the hash function.

The above example prevent the rainbow table attack and dictionary attacks.You can used above example to real world application if you are using user and password utility.

If you want to read full article about secure password please refer to link:



Secure Password


Thank You for reading this article.
And also like my FB page givin below link and share it.

Best Python, PHP and Ethical Hacking Training in Meerut, Noida and Delhi.

  • Ethical Hacking Training in Meerut
  • Python Training in Meerut
  • Python Training in Noida
  • Best Python Training in India
  • Best Hacking Training in Noida
  • Black Hat Hacking Training in Meerut
  • Black Hat Hacking Training in Noida
  • Artificial Intelligence Training in Meerut
  • Machine Learning Training in Meerut
  • Data Analysis Training in Meerut
  • PHP Training in Meerut
  • PHP Training in Noida
  • Data Analysis Training in Noida

Official Website: Kryptora | Planet of Technology

Follow ManishHacker1