Wednesday, 28 December 2016

what is hash function?

A hash function  converts a numerical input value into another compressed  value. The input to the hash function is of arbitrary length but output is always of fixed length.
Values returned by a hash function are known as hash values, hash codes, digests, simply hashes or checksum.

Most used hash functions are:
1.MD5:Message digest algorithm producing a 128 bit hash value. This is widely used to check data integrity. It is not suitable for use in other fields due to the security vulnerabilities of MD5.

2.SHA: Group of algorithms designed by the U.S's NSA that are part of the U.S Federal Information processing standard. These algorithms are used widely in several cryptographic applications. The message length ranges from 160 bits to 512 bits.

There is module in python "hashlib"
This module implements a common interface to many different secure hash and message digest algorithms.It includes  FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384,SHA512 ,RSA’s MD5 algorithm.
Constructors for hash algorithms  in this module are md5(), sha1(), sha224(), sha256(), sha384(), and sha512().

 Example (MD5):
>>>import hashlib

>>> print(hashlib.algorithms_available)
{'dsaEncryption', 'DSA', 'SHA384', 'MD5', 'ripemd160', 'SHA256', 'sha256', 'SHA224', 'SHA', 'MD4', 'sha', 'RIPEMD160', 'sha224', 'dsaWithSHA', 'SHA1', 'md4', 'whirlpool', 'sha512', 'DSA-SHA', 'ecdsa-with-SHA1', 'sha384', 'md5', 'sha1', 'SHA512'}

>>> print(hashlib.algorithms_guaranteed)
{'sha512', 'sha384', 'sha256', 'md5', 'sha1', 'sha224'}


>>> x.digest()

>>> x.digest_size
>>> x.block_size

>>> hashlib.sha224(b"I am a new bloggers").hexdigest()

>>>import hashlib

>>> y=hashlib.sha1(b"I am a new bloggers") #use sha224,sha 256,sha384,sha512

>>> y
<sha1 HASH object @ 0x02DE4750>

>>> z=y.hexdigest()

>>> z


Why Hash Function?
1.Hash functions are used inside some cryptographic algorithms,in digital signatures,message authentication codes, manipulation detection, fingerprints,checksums,hash tables,password storage and much more.
2.To check for duplicate data or files, to check data integrity when you transmit information over a network, to securely store passwords in databases, or maybe some work related to cryptography.

1.It is a one way function(This means if f is the hashing function, calculating f(x) is pretty fast and simple, but trying to obtain x again will take years).
2.Hash functions are not a cryptographic protocol, they do not encrypt or decrypt information, but they are a fundamental part of many cryptographic protocols and tools.

Program Related to Hash Function:Password Hash Function:
import uuid
import hashlib

def hash_password(password):
    # uuid is used to generate a random number
    x = uuid.uuid4().hex
    return hashlib.sha256(x.encode() + password.encode()).hexdigest() + ':' +x
def check_password(hashed_password, user_password):
    password, x= hashed_password.split(':')
    return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()

new_password = input('Please enter a password: ')
hashed_password = hash_password(new_password)
print('The string to store in the db is: ' + hashed_password)
old_password = input('Now please enter the password again to check: ')
if check_password(hashed_password, old_password):
    print('Right password')
    print('Hey passwords does not match')