Python Forum
Password Hacker Science Project - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Password Hacker Science Project (/thread-2885.html)



Password Hacker Science Project - BenjC - Apr-17-2017

Hello,

I need some help with my science fair project. If you want to look at the sit I got my idea from just google "How easily can your password be hacked science fair".   I am not sure that I have the code correct or even that I attached the password txt correctly. Here is my code though:

When I run the code it doesnt even try to use method 3 or 4 so it only tries to guess a string of numbers but no letters.

Moderator sparkz_alot:
Added spoiler tags



RE: Password Hacker Science Project - sparkz_alot - Apr-17-2017

This looks as if it was just copied from the web site.  It also seems the site has it's own forum regarding this particular lesson.  So what leads you to believe it's not using tests 3 and 4?  Did you modify the code? Did you modify the text file? Are you receiving any errors?


RE: Password Hacker Science Project - volcano63 - Apr-17-2017

Wow, that is too much effort Wall.
Before help - 444 lines of code Wall , and many of them redundant Dodgy  - you need Code Review first.
Let me start by - ever heard of lists, elif? You just don't add numbers to names - you make list!

Let's start
passwords = [""] * 6
Now, about global - the only time you need global is when you want to re-assign value within a function - and it is still a bad idea. Outer-scope variables are visible within enclosed functions as they are - without qualifier. Still, arguments are better

But still - you write a lot of redundant code - you first function - since only one condition will be true - all ifs, startig from second and excluding the last, should be elifs, and the last should be else


if which_password == 0:
   .....
elif which_password == 1:
   .....
But even that is still lousy! Why? You repeat yourself! And - as I was recently preached to on FB  Doh , there's a DRY approach (the priestess called it paradigm Huh ), Don't Repeat Yourself

So, instead of this monstrosity, you may write just that
 def check_userpass(password_index, password_guess, passwords):
    if password_index == 0:
        return password_guess == passwords[0]
    elif password_index <len(passwords):
        return MD5me(password_guess) == passwords[password_index]
    return False
Concise, readable and to the point.

I am done for now. Now your turn - start chopping away redundancies in your code