create account

Learn Cryptography #2 - Hashing using Python by abhi3700

View this thread on: hive.blogpeakd.comecency.com
· @abhi3700 · (edited)
$23.83
Learn Cryptography #2 - Hashing using Python
![learn cryptography.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514338762/m5ifbwspszd0v2ggzvkg.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](https://steemit.com/utopian-io/@abhi3700/learn-cryptography-1-hashing-vs-encryption), [Github](https://github.com/abhi3700/My_Learning_Cryptography-Concepts/blob/master/Basics/1_Hashing_vs_Encryption.md)
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](https://anaconda.org/) 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514440688/cqqyfg8xnwpl6t6yppcx.png)

After choosing the "learn-cryptography" notebook it opens like this..
![8.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514446363/sjbgykftpford828e6px.png)

* Now, **enter into the environment** so as to install the required libraries.
```activate learn-cryptography```
![1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514340029/pbehpltggcklkrwqw7kn.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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514440641/dhavtbakldc99rosrfwf.png)


* Now, install the libraries for practising cryptography - 
1. *'cryptography'* - ```pip install cryptography```
![5.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445842/n5kupoexrgzl0b36ecq9.png)
![7.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514446120/iytvf3f0bbzohtjhaswj.png)

2. **pycryptodome** - ```pip install pycryptodomex```. Just to avoid any conflict while *importing Crypto* library. Also , install this using ```pip install pycryptodome```.
![4.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445814/i0xn5r9umgbnsstgevsw.png)
![6.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445977/xxpntxjaqrfhxqrbf9nq.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.
```python
from Crypto.Hash import SHA256
SHA256.new(b'abc').hexdigest()
```
![9.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514449997/n3dacts9uqm4wieswsnu.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:
```python
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.
```python
# 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1514467424/febgnmjmokydf8v0onqr.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 function | Hash output size (bits) | Secure? 
------------ | ------------- | ------------
MD2 | 128 | No 
MD4| 128 | No
MD5 | 128 | No
SHA-1 | 160 | No
SHA-256 | 256 | Yes

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](https://github.com/abhi3700/My_Learning_Cryptography-Concepts/blob/master/Programming/2_Hashing_vs_Encryption.md)

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@abhi3700/learn-cryptography-2-hashing-using-python">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , ,
properties (23)
authorabhi3700
permlinklearn-cryptography-2-hashing-using-python
categoryutopian-io
json_metadata"{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"score":7.968066,"default_branch":"master","watchers":0,"open_issues":0,"forks":0,"license":null,"open_issues_count":0,"archived":false,"mirror_url":null,"forks_count":0,"has_pages":false,"has_wiki":true,"has_downloads":true,"has_projects":true,"has_issues":true,"language":null,"watchers_count":0,"stargazers_count":0,"size":236,"homepage":"","svn_url":"https://github.com/abhi3700/My_Learning_Cryptography-Concepts","clone_url":"https://github.com/abhi3700/My_Learning_Cryptography-Concepts.git","ssh_url":"git@github.com:abhi3700/My_Learning_Cryptography-Concepts.git","git_url":"git://github.com/abhi3700/My_Learning_Cryptography-Concepts.git","pushed_at":"2017-12-26T06:30:34Z","updated_at":"2017-12-03T11:38:36Z","created_at":"2017-12-03T10:31:05Z","deployments_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/deployments","releases_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/releases{/id}","labels_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/labels{/name}","notifications_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/notifications{?since,all,participating}","milestones_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/milestones{/number}","pulls_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/pulls{/number}","issues_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/issues{/number}","downloads_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/downloads","archive_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/{archive_format}{/ref}","merges_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/merges","compare_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/compare/{base}...{head}","contents_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/contents/{+path}","issue_comment_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/issues/comments{/number}","comments_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/comments{/number}","git_commits_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/git/commits{/sha}","commits_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/commits{/sha}","subscription_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/subscription","subscribers_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/subscribers","contributors_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/contributors","stargazers_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/stargazers","languages_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/languages","statuses_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/statuses/{sha}","trees_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/git/trees{/sha}","git_refs_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/git/refs{/sha}","git_tags_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/git/tags{/sha}","blobs_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/git/blobs{/sha}","tags_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/tags","branches_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/branches{/branch}","assignees_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/assignees{/user}","events_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/events","issue_events_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/issues/events{/number}","hooks_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/hooks","teams_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/teams","collaborators_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/collaborators{/collaborator}","keys_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/keys{/key_id}","forks_url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts/forks","url":"https://api.github.com/repos/abhi3700/My_Learning_Cryptography-Concepts","fork":false,"description":"Learn about cryptography concepts - Both basics and core.","html_url":"https://github.com/abhi3700/My_Learning_Cryptography-Concepts","private":false,"owner":{"site_admin":false,"type":"User","received_events_url":"https://api.github.com/users/abhi3700/received_events","events_url":"https://api.github.com/users/abhi3700/events{/privacy}","repos_url":"https://api.github.com/users/abhi3700/repos","organizations_url":"https://api.github.com/users/abhi3700/orgs","subscriptions_url":"https://api.github.com/users/abhi3700/subscriptions","starred_url":"https://api.github.com/users/abhi3700/starred{/owner}{/repo}","gists_url":"https://api.github.com/users/abhi3700/gists{/gist_id}","following_url":"https://api.github.com/users/abhi3700/following{/other_user}","followers_url":"https://api.github.com/users/abhi3700/followers","html_url":"https://github.com/abhi3700","url":"https://api.github.com/users/abhi3700","gravatar_id":"","avatar_url":"https://avatars2.githubusercontent.com/u/16472948?v=4","id":16472948,"login":"abhi3700"},"full_name":"abhi3700/My_Learning_Cryptography-Concepts","name":"My_Learning_Cryptography-Concepts","id":112918746},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","cryptography","bitcoin","programming","python"],"users":["abhi3700"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1514338762/m5ifbwspszd0v2ggzvkg.png","https://steemit.com/utopian-io/@abhi3700/learn-cryptography-1-hashing-vs-encryption","https://github.com/abhi3700/My_Learning_Cryptography-Concepts/blob/master/Basics/1_Hashing_vs_Encryption.md","https://anaconda.org/","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514440688/cqqyfg8xnwpl6t6yppcx.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514446363/sjbgykftpford828e6px.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514340029/pbehpltggcklkrwqw7kn.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514440641/dhavtbakldc99rosrfwf.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445842/n5kupoexrgzl0b36ecq9.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514446120/iytvf3f0bbzohtjhaswj.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445814/i0xn5r9umgbnsstgevsw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445977/xxpntxjaqrfhxqrbf9nq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514449997/n3dacts9uqm4wieswsnu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514467424/febgnmjmokydf8v0onqr.png","https://github.com/abhi3700/My_Learning_Cryptography-Concepts/blob/master/Programming/2_Hashing_vs_Encryption.md"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1514338762/m5ifbwspszd0v2ggzvkg.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514440688/cqqyfg8xnwpl6t6yppcx.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514446363/sjbgykftpford828e6px.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514340029/pbehpltggcklkrwqw7kn.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514440641/dhavtbakldc99rosrfwf.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445842/n5kupoexrgzl0b36ecq9.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514446120/iytvf3f0bbzohtjhaswj.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445814/i0xn5r9umgbnsstgevsw.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514445977/xxpntxjaqrfhxqrbf9nq.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514449997/n3dacts9uqm4wieswsnu.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1514467424/febgnmjmokydf8v0onqr.png"]}"
created2017-12-28 13:35:57
last_update2017-12-28 14:21:24
depth0
children2
last_payout2018-01-04 13:35:57
cashout_time1969-12-31 23:59:59
total_payout_value16.906 HBD
curator_payout_value6.919 HBD
pending_payout_value0.000 HBD
promoted0.020 HBD
body_length6,525
author_reputation1,411,436,389,304
root_title"Learn Cryptography #2 - Hashing using Python"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id25,689,742
net_rshares2,445,688,859,937
author_curate_reward""
vote details (9)
@shreyasgune ·
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorshreyasgune
permlinkre-abhi3700-learn-cryptography-2-hashing-using-python-20171229t064730329z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2017-12-29 06:47:36
last_update2017-12-29 06:47:36
depth1
children0
last_payout2018-01-05 06:47:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation4,924,803,411,962
root_title"Learn Cryptography #2 - Hashing using Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id25,820,335
net_rshares0
@utopian-io ·
### 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. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**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**
properties (22)
authorutopian-io
permlinkre-abhi3700-learn-cryptography-2-hashing-using-python-20171229t122609379z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2017-12-29 12:26:12
last_update2017-12-29 12:26:12
depth1
children0
last_payout2018-01-05 12:26:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,428
author_reputation152,955,367,999,756
root_title"Learn Cryptography #2 - Hashing using Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id25,862,253
net_rshares0