简体   繁体   中英

Secure Python Login

I have been trying to make a secure login on a python program I've been working on, but everything I try doesn't seem to help. I need a snippet of code that I can put in my script. My main issue is that anyone who looks at my script can just see the password. Plus I don't know how to put stars instead of characters when they type in password. This is my login code, it is very basic because I'm pretty much a beginner.

#login
import webbrowser as w
def username():
    print("Enter UserName")
    usrnm = input()
    if(usrnm == "example"):
        password()
    else:
        notusername()
 def notusername():
    print("Try Again")
    username()
 def password():
    print("Enter Password")
    pswrd = input()
    if(pswrd == "password"):
        w.open("www.example.net")
    else:
        notusername()
 username()

First, let me preface this by saying that, especially if you're a beginner, it is usually not a good idea to try to implement your own login/security code for anything that is public and seriously needs security.

Having said that, the general approach to hiding the actual password is to store a hash (eg SHA-1) of the password, not the password itself. You can then safely store that hash value wherever you like (eg database, text file etc.) In python you can do this using something like hashlib eg

import hashlib

sh = hashlib.sha1()
sh.update('password')
hash_value = sh.hexdigest()
# write hash_value to file/db...

When you go to validate against the stored password, you take the hash of the user input and compare it against the stored hash. If they are the same, then the password is correct.

Again, for any serious security, use one of the many frameworks that are available, as they have been tested by many people.

You Should try this code

list1 = ('Password is Correct...' , 'Password is Incorrect' , 'Closing Python...','Hello',
         '''Press Enter to Continue...''', 'Closing Python...' , 'badger123',
         '''Please Enter Your Name: ''', 'Please Enter Your Password: ')

name = input(list1[7])

password = input(list1[8])
if password == list1[6]:
    print(list1[0])
else:
    print(list1[1])
    exit()

import time
time.sleep(0)
input(list1[4])
time.sleep(0)




print (list1[3] , name)



import time

time.sleep(1)

print (list1[5])

import time

time.sleep(5)

input (list1[4])

exit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM