Cannot encrypt using python

This is the place for queries that don't fit in any of the other categories.

Cannot encrypt using python

Postby mesql » Mon Sep 09, 2013 1:23 am

Ok, I've been trying to create a script that compares two encrypted passwords to each other, with one of the passwords being user input and the other having been previously inputted. I couldn't get my two passwords to match up when I was entering one myself, so I tried defining both passwords in the script, encrypting them, and then comparing, yet they still don't match, and I'm lost. This is my code as of right now.

Code: Select all
import hashlib

password = hashlib.new('sha1')
password.update(b'password')
password.hexdigest()

password2 = hashlib.new('sha1')
password2.update(b'password')
password.digest()

if(password == password2):
    print ("They match!")

else:
    print ("They don't match.")


Additionally, I'm using Python 3.3.2, and have also just stared programming in python, so any help is appreciated.
mesql
 
Posts: 1
Joined: Mon Sep 09, 2013 1:15 am

Re: Cannot encrypt using python

Postby micseydel » Mon Sep 09, 2013 3:41 am

Ok, a few things here. Hashing and encryption are very different. Hashing takes some input, often an arbitrarily large string, and then creates a finite-length hash from that. Inevitably, this means that there are collisions. Because of this (and a little bit more than that), hashing is called "one way". That is, you can't take a hash and programatically know what the string was that was the basis for that hash. This is different from encryption, which is "two way" and can be reversed precisely.

What you may be trying to do here is replicate how passwords are stored in real life. In real life, when you transmit your password to Facebook or anything, rather than storing it in plain text on their server or even encrypted, which isn't considered secure enough, a hash of your password is stored. When you go to log in, they hash your password and compare it to the hash they have stored. As I mentioned, there are collisions, so there is a very small probability that the wrong password was entered, but it's small enough that it isn't an issue.

Now, what you have here
Code: Select all
if(password == password2):

will never be true. That's because "password" is a 40-digit hex digest and "password2" is a 20-digit binary digest. They can never be equal because their lengths are different. I'm not quite sure what you were really trying to accomplish here.

I hope to have illuminated some misconception, but if you have any more specific questions, do feel free to post. In fact, it'd be much appreciated if you let us know what you were trying to accomplish and what you end up with, so that if we see similar questions in the future we could help people more quickly.
Last edited by micseydel on Mon Sep 16, 2013 1:40 am, edited 1 time in total.
Reason: Typo, link.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1385
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Cannot encrypt using python

Postby vijaykumbhani » Fri Sep 13, 2013 5:30 am

micseydel right
Code: Select all
import hashlib

password = hashlib.new('sha1')
password.update(b'password')
[color=#FF4040]password.hexdigest()[/color]

password2 = hashlib.new('sha1')
password2.update(b'password')
[color=#FF4040]password.digest()[/color]

if(password == password2):
    print ("They match!")

else:
    print ("They don't match.")



First you check both colored font are different or not ?
if both different , then answer is not match
else answer is match
[b][i]With Regards,
Vijay Kumbhani[/i][/b]
vijaykumbhani
 
Posts: 33
Joined: Wed Aug 14, 2013 4:26 am
Location: Surat, Gujarat, India

Re: Cannot encrypt using python

Postby micseydel » Fri Sep 13, 2013 8:48 pm

vijaykumbhani wrote:micseydel right
Code: Select all
import hashlib

password = hashlib.new('sha1')
password.update(b'password')
[color=#FF4040]password.hexdigest()[/color]

password2 = hashlib.new('sha1')
password2.update(b'password')
[color=#FF4040]password.digest()[/color]

if(password == password2):
    print ("They match!")

else:
    print ("They don't match.")



First you check both colored font are different or not ?
if both different , then answer is not match
else answer is match

I'm not sure what you're trying to say here but it's probably wrong. They will never be equal. There is no "if both are different". They are different by definition.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1385
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Cannot encrypt using python

Postby dboxall123 » Sun Sep 15, 2013 6:59 pm

Don't really understand what you're trying to do, also don't really understand what any of that code does, but I've noticed that in the first password you use
Code: Select all
password.hexdigest()

and in the second you just use
Code: Select all
password.digest()

Could that have anything to do with it?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 3 guests