Skip to main content

How to Use Cryptographic hashes Using Python


Python Secret #Author: ManishHacker1

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 Hash function 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 = raw_input('Enter String to hash: ')
# Assumes the default UTF-8
hash_value = hashlib.md5(key.encode())
print "MD5 hash value: ",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 "sha1 hash value: ",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 "sha224 hash value: ",hash_value

Uses SHA256:


import hashlib
key = hashlib.sha256(b'ManishHacker1')
hash_value = key.hexdigest()
print "sha256 hash value: ",hash_value

Uses SHA384:


import hashlib
key = hashlib.sha384(b'ManishHacker1')
hash_value = key.hexdigest()
print "sha384 hash value: ",hash_value

Uses SHA512:


import hashlib
key = hashlib.sha512(b'ManishHacker1')
hash_value = key.hexdigest()
print "sha512 hash value: ",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


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 given below link and share it.

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

Follow ManishHacker1

Comments

Popular posts from this blog

How to create Folder Lock with Password Protected using Python

Amazon.in Widgets Python Blog #Author: ManishHacker1 Hello guys, Today we will be learn how to create folder lock with password protected using Python programming language. What is Folder Lock? Folder Lock is a data security software that is allows its users to encrypt thier files and folder. Lock, hide and password protects files and folder on your computer. You can use Folder Lock to secure your files and folder on windows. So, I am going to tell you how to make your own Folder Lock, without using any software. Pre-Reuisites: You will need only pyhton installed in your operating system. if you want to distribute your Folder Lock then you will be also need pyinstaller using to convert our python file to .exe file. Compatibility: Compatible with any Microsoft Windows Operating system. Instructions: First open your python IDE or Notepad, where you want to write code. If you use to notepad please remember indentation using to write code. Or copy th...

How to Connect Window Machine to Linux Machine Using Python via SSH

Python Secret #Author: ManishHacker1 Hello everyone, This is my another article about SSH connectivity to the client machine. In my previous article we have some trouble using pxssh module on windows machine. That's why I am writing another article for the SSH connection. In this article we will use paramiko module to connect another machine. This is fully supported windows, Mac and Linux machine users. In this article, I will show you how to use Paramiko SSH (a Python SSH library) to connect and gather information from another Machine. What is Paramiko? Paramiko is a Python (2.6+, 3.3+) implementation of the SSHv2 protocol [1], providing both client and server functionality. While it leverages a Python C extension for low level cryptography (Cryptography), Paramiko itself is a pure Python interface around SSH networking concepts. Read Full Documentation Let start our demonstration: Requirment: Paramiko( For SSH connection) How to Install paramilko modu...

How to Create MD5 Brute Force Script Using Python

Python Secret #Author: ManishHacker1 How to Create MD5 Brute Force Script Using Python Hello guys, Today we will be learn How to create MD5 brute force script using Python. What is brute-force Attack? Brute-force attack also known exhaustive key search Process of checking all possible keys Using a dictionary to attack with Dictionary is usually more effective than searching the whole key space. Exponentially grow with increasing key size. Brute force Attack Limit Because time/energy required to crack a key grows exponentially with key size, encryption in today's standards and computing power are safe brute-force attack. A 256 bit key would take on 50 of today's super computer 3x10^51 years What is Dictionary Attack Much faster than a whole key space search Not guaranteed Commonaly used on passwords Dictionaries can be found online A popular one is darkc0de.lst Good to run before a big dictionary like darkc0de. Can eliminate the most com...