Hashing texts by python

in programming •  6 years ago 

First of all
1.What is hash?
hash is a kind of coded string that is not unlockable and it works like something like this :
#in here we made a hash code from the "Bizhan" in sha512 algorithm

The text : Bizhan
hashed text: b8f809a869657e258531b5fd587a8f7641aa50912d899ac2ec7fd70872ba4c7aa2b0d2d5e5c429613b8ab7dc33ca97133dcfbe37f002882014d169a8ca1c6b25

And this unknown text with numbers is a hash code .
And you must know that by changing a letter or making a letter uppercase or downer case will effect on hash code!

For egxample you saw the "Bizhan"s hash code now look at this :
We are going to hash the "bizhan" in here and as you see the "b" is lowercase.

446b9b8fd87cfd4d0fe4aa98c99ec66748ce6519723c2eca7e10b70c4a048d2b06ff7929da9581c1d9a32f1e4e203fdd4308e08f2c3ca5ef81a4497d2e93179e

And now compare this with the first one.
They are compeletly different!

2.why use hashcodes?they are not unlockable!

The answer is the second sentence! They are not unlockable and hackers cannot find their key and it is the good point!

3.how people use hashes?

Let me give you an egxample.
Imagin a website that saves your username and password in it's self. Like instagram. If they save any thing you enter ther in it's database , very easy a hacker after accessing database can take all of the user's username and password and it means he hacked all users!
But the websites use the hashcode like this:
First.You enter your username and password.
Second.the website make hashcodes from them
Third.the website saves the hashcode in database
Fourth.if hacker can accsess database he cannot understand the usernames and passwords
Fifth.when you want loggin the website you enter the normal username and password like the first time then the website will compare the text's hascode now you entered with the hashcode is saved in it's database!

Now we are going to make a simple program that take normal texts and convert it to hashcode.
#just in this code iam using the SHA512 algorithm you can change the "sha512" to any algorithm that python supports.
#and in here iam using python 3

And I will explain it after the code
Know the code:

import hashlib
A = input (" ")
B = hashlib.sha512 (A.encode ())
C = B.hexdigest ()
print (C)

In first line we import the hashlibrary that has the hashs algorithms

In second line we are taking text from user

In third line we are making the text ready for making it's hascode.

In fourth line we are taking the last line's output means the hashcode is generated here

And last line we print the hashcode to show it to user. In this line you can write it on files exchange of printing it

And finish
Good luck

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!