Learn Cryptography #2 - Hashing using Python

in #utopian-io7 years ago (edited)

learn cryptography.png

Hi Guys!!....

This is the programming part of the previous tutorial "Hashing vs Encryption" in this "Learn Cryptography" series.

View Part 1 in Steemit, Github
In previous tutorial, some basics is covered regarding Hashing & Encryption and the main differences between them.

In this tutorial, we will be programming using Python for hashing any text with hash algorithm.

Tools Installation

We will be using Anaconda for accessing the features.

  • Create a separate environment. It's not necessary but recommended so as to avoid conflicts in case of multiple type of projects running in a PC/ laptop.
    conda create --name learn-cryptography python=3

  • Now, when the notebook opens, choose the correct shell (where all tools are installed)
    3.png

After choosing the "learn-cryptography" notebook it opens like this..
8.png

  • Now, enter into the environment so as to install the required libraries.
    activate learn-cryptography
    1.png

  • Install the nb_conda (specifically for an environment) using
    conda install nb_conda

  • Open the jupyter notebook in your directory (where project files are to be saved)
    Use jupyter notebook
    2.png

  • Now, install the libraries for practising cryptography -
  1. 'cryptography' - pip install cryptography
    5.png
    7.png

  2. pycryptodome - pip install pycryptodomex. Just to avoid any conflict while importing Crypto library. Also , install this using pip install pycryptodome.
    4.png
    6.png

Now, there is no conflict at all importing libraries.

Let's start coding now....

Coding

We are going to cover Hashing in this tutorial. And Encryption-Decryption shall be covered in the subsequent tutorial. In encryption-decryption, there are 2 main types - symmetric and assymetric.

Hashing

Let's start with hashing a string - "abc" using SHA256 method.

from Crypto.Hash import SHA256
SHA256.new(b'abc').hexdigest()

9.png
Here, string 'b' in the input of the function new() represents byte string.

So, ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad is the hash for string abc using SHA256 hash algorithm. And this is unique. Everytime this string will give the same output and no other input shall give this particular hash.

In cryptography hash function, any algorithm which fails the 2 properties (mentioned below) are not used anymore. And this happened with MD5 algorithm-

  1. collision attacks - when 2 different inputs result in the same hash output. It was found in 2004 and 2008.
  2. preimage attacks - when a hash 'h' is put into the MD5 function and message m is obtained. where hash(m) = h that means the function is reversible which violates the property.

Application

Password management and Storage

Used for matching the password input with the hash of the password stored in the database.
Code snippet:

from Crypto.Hash import SHA256

# a function defined for checking if correct password entered?
def check_password(input_password, actual_password):
    
    # here, SHA256.new() function input as bytes. So, used str.encode() function for 
    # encoding string into bytes.
    actual_password_hash = SHA256.new(str.encode(actual_password)).hexdigest() # hash of actual password
    input_password_hash = SHA256.new(str.encode(input_password)).hexdigest() # hash of input password
    
    return input_password_hash == actual_password_hash

Let's make a small application where one enters the password and its hash gets stored.
While entering the password next time, it will return TRUE or FALSE by inserting into
check_password() function.

# save the actual password
a = input("Enter the actual password:") # runs 1 time.

# This loop ends only when the correct password is entered, otherwise it continues.
while(1):
    i = input("Enter your actual password again:")

    result = check_password(i, a)  # checks the password
    
    if result == True:
        print("Congrats!... Correct password.")
        break
    else:
        print("Sorry!... INCORRECT password\n\n")
    
# End of the code...

10.png

So, entering wrong returns FALSE i.e. 'Incorrect password'.

This is how the Login screen works in the back-end of the code.

Conclusion

At last, this is the summary about the hash functions in tabular format.

Hash functionHash output size (bits)Secure?
MD2128No
MD4128No
MD5128No
SHA-1160No
SHA-256256Yes

In recent date, there are many more hash functions like X11 which is present in Bitcoin Segwit2x, the new version of Bitcoin (old one uses SHA-256) for mining purpose.

In the subsequent tutorials, we will learn more about programming Cryptography methods using python.

That's all for now...

Stay tuned for more such tutorials.....

View in Github



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @abhi3700 I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.21
TRX 0.21
JST 0.035
BTC 91680.24
ETH 3137.17
USDT 1.00
SBD 3.00