Bretsky
Bretsky

Reputation: 431

Hashed passcode matching stuck python

I am doing a fun little project, making a kind of secret code program with a GUI(Tkinter) to encrypt my text(not very securely). I am trying to make a password on this program linked to a txt file. The program has default password 'abc', stored in a text file in sha-224. When the user enters 'abc', it will hash their input in sha-224, and compare it to the stored password in _hexutf2.txt. Once they have logged in, the user will have the option to choose a new password by clicking New Passcode, entering their previous passcode, clicking next, and then clicking new code. After clicking new code, the user enters a new passcode in the entrypassVariable, then clicks enter passcode, which will write the new passcode on the txt file. Instead, the program hangs on the first pressing of Enter passcode, despite the fact that I had entered 'abc' the default passcode. The program worked before I added the password element, so I will post only the password code here, but I will link to the entire program if anyone wants to see it.

EDIT

Posting just the essential code here. The problem in my main program is caused by this. For some reason, this program prints this:

Stored Password: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632

Enter Password: Moo
Password Attempt Hash: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
Password Incorrect

http://snipplr.com/view/75865/cryptographer/ <----Entire program code

import hashlib

passfile = open('pass.txt','r')
stored_password = str(passfile.read())
print 'Stored Password: ' + stored_password
password = raw_input('Enter Password: ')
enter_pass_sha = hashlib.sha224(password)
enter_password = str(enter_pass_sha.hexdigest())
print 'Password Attempt Hash: ' + enter_password
if stored_password == enter_password:
    print 'Password Correct'
else:
    print 'Password Incorrect'

Upvotes: 0

Views: 84

Answers (2)

jonrsharpe
jonrsharpe

Reputation: 122091

You should have noticed this:

Stored Password: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
    # blank line?!
Enter Password: Moo
Password Attempt Hash: cd62248424c8057fea8fff161ec753d7a29f47a7d0af2036a2e79632
Password Incorrect

When you read the stored_password in from the passfile, it comes with a newline character '\n' at the end. You need to do:

with open('pass.txt') as passfile:
    stored_password = passfile.read().strip()
print 'Stored Password: ' + stored_password

Note str.strip, called without arguments, removes all whitespace, including newlines, from the start and end of the string. Note also the use of the with context manager for file handling, which handles errors and closes the file for you.

Upvotes: 3

Yann Vernier
Yann Vernier

Reputation: 15887

One issue is hexCode = passfile.read - that's a bound method, not a string (you never called it with ()). This will not match any of your strings, and causes an exit from OnPassClick or OnNextClick, or a funny message from OnNewClick. Similarly, you raised the class SystemExit type instead of an instance of it.

Upvotes: 1

Related Questions