Hashing | Hashing Example | Hashing using Python

 

hashing using python
Hashing using python

What is the hype word in today's world's devices and for technology? The answer is 'Security'. Everyone needs security in their life and to their devices(Smartphone, Dekstop, tabs, etc.). In every aspect of the web, the security of data should be settled. One of the securing data techniques is called Hashing. A programmer or web developer should be known about hashing algorithms to secure their data. Suppose, a user opens an account on your website. The password is very sensitive from a user to the website. It should be unknown to you, especially from the hackers. What will do you do for making it unknown? Simply, convert the password string into another complex format that is complicated to read. That is how, the new converted key values of the password string will be unknown from the developer, normal user, and also from the hackers. In this way, the hashing algorithm secures the user password data. Not only password, but you can also convert the username, user location, profile image, etc. can also be hidden using the hashing algorithms.

What is hashing?

Hashing is a mathematics process to convert a given input key or data into another complex format with arbitrary length. The hashing algorithm is easy to use and more secure and faster to convert than the other encrypted data technique. The hashing is considered as "Fingering data" because the algorithm generates unique hash values for each conversion

Example

Suppose, you have a password "abc123". Now, you want to store it in a database also hide it from others. Use hash algorithm, convert the "abc123" into a hash value (ab34234bdsksj343xlksld334). You can acquire the password by reversing the hash algorithm. That is how your password data is secured from others.

Application

The application of hash algorithms is found on the database to store the password. Almost all databases (MySql, MongoDb, SqlLite, etc) and web frameworks (Laravel, Django, Flask, etc) support and use the hash algorithm. The SHA (Secure Hash Algorithm) is a cryptographical hashing algorithm and it is mostly used on the web.

sending data using hashing algorithm
Sending data to the database using the hashing algorithm


Retrieving data using the hashing algorithm
Retrieving data from the database using the hashing algorithm


The introduction part is ended. Now the implementation of the hashing algorithm is shown below.


Hash algorithm using the python 'hashlib' library

No installation is needed for this program. We are using the built-in function 'hashlib' from python.

Code

from hashlib import sha256, sha3_256, sha1, sha3_384

def hash_sha1(input_text):
return sha1(input_text.encode('utf-8')).hexdigest()

def hash_sha3_384(input_text):
return sha3_384(input_text.encode('utf-8')).hexdigest()


def hash_sha3_256(input_text):
return sha3_256(input_text.encode('utf-8')).hexdigest()


def hash_sha256(input_text):
return sha256(input_text.encode('utf-8')).hexdigest()

if __name__ == '__main__':
input_text = input('Enter a string: ')
print("Generated hash value using sha-256: ",hash_sha256(input_text))
print("Generated hash value using sha3-256: ", hash_sha3_256(input_text))
print("Generated hash value using sha3-384: ", hash_sha3_384(input_text))
print("Generated hash value using sha-1: ", hash_sha1(input_text))

Sample input:

Enter a string: school-of-beginners

Sample output:

Generated hash value using sha-256130af55d2531be982a230240b2fcc34aba22fa6e75ef9797e9ef089e07069f20

Generated hash value using sha3-256ec4c2d0bafcf2385d6334c588c3f7ede3f187ecb35941c1607e745e1a71ceec6

Generated hash value using sha3-384daabab4ac6c9367ae61c1ef50418f6c57350a21ef241419ba31143440404f2cf9a6405165c9b7d900243348ba7355652

Generated hash value using sha-16dbee133f4ac9253c5d85662badba9719c678bd4

Geo hash using python

The below programs are used to analyze the geographical co-ordinate (latitude, longitude) using the hashing algorithm.

Installation

# Fow windows: Run this command
pip install pygeohash


Encoding geographical co-ordinate (latitude, longitude) using 'pygeohash'

import pygeohash as pygh

def encode(lat, long):
return pygh.encode(latitude=lat, longitude=long)

if __name__ == '__main__':
lat=float(input("lat: "))
long = float(input("long: "))
print('Generated Geo hash: ',encode(lat, long))

Sample input:

lat: 41.6
long: -5.6

Sample output:

Generated Geo hash:  ezkh8t41wd13

Decoding geographical co-ordinate (latitude, longitude) using 'pygeohash'

import pygeohash as pygh

def decode(ghash):
return pygh.decode(geohash=ghash)

if __name__ == '__main__':
g_hash = input("Enter a geo hash: ")
print(decode(g_hash))

Sample input:

Enter a geo hash: ezs42e44yx96

Sample output:

(42.6, -5.6)

Calculating distance between two Geo hashed values using 'pygeohash'

import pygeohash as pgh

def distance(ghash_1, ghash_2):
return pgh.geohash_approximate_distance(geohash_1=ghash_1, geohash_2=ghash_2)

if __name__ == '__main__':
geo_h1=input("Geo hash 1: ")
geo_h2 = input("Geo hash 2: ")
print('Distance between two Geo hash: ',distance(geo_h1, geo_h2))

Sample input:

Geo hash 1: ezkh8t41wd13
Geo hash 2: ezs42e44yx96

Sample output:

Distance between two Geo hash:  625441


Generate perfect hash function

This section is written to show the application of hash algorithms. For this, we use the 'perfection' library to generate a perfect value from a set of inputs. This simply takes the input of the set and gives the output of a perfect value from the set using the hashing algorithm.

Installation

# Run this command in windows
pip install perfection

Code

import perfection
set_of_input = (0, 2, 4, 7 , 12, 13, 15, 17, 19, 21, 22, 25, 26, 29, 33, 39)
hash_gen = perfection.make_hash(set_of_input)
print(hash_gen(1))
print(hash_gen(5))


Sample output:

7
11

Hash algorithm (SHA-256) from scratch using python

The SHA-256 algorithm is discussed with proper description in this link. Now, the implementation of this algorithm is in Github. We do not want to copy those valuable resources.

Post a Comment

Previous Post Next Post