问题
I want to hide my password but I don't know how. I have seen show="*"
and also getpass
but I don't know how to place them into this code. I'm using Python 2.7.3 and coding on a Raspberry Pi.
ans = True
while ans:
print("""
-------------
| 1. Shutdown |
| 2. Items |
-------------
""")
ans=raw_input("""
Please Enter A Number: """)
if ans == "1":
exit()
elif ans == "2":
pa=raw_input("""
Please Enter Password: """)
if pa == "zombiekiller":
print("""
----------------
| 1. Pi password |
| 2. Shutdown |
----------------
""")
pe=raw_input ("""
Please Enter A Number: """)
if pe == "1":
print ("""
Pi's Password Is Adminofpi""")
import time
time.sleep(1)
exit()
elif pe == "2":
exit()
else:
print("""
You Have Entered An Inccoredt Option. Terminating Programm""")
import time
time.sleep(1)
exit()
else:
print("""
You Have Entered An Inccorect Password. Terminating Programm""")
import time
time.sleep(1)
exit()
回答1:
getpass
hides the input, just replace raw_input
after importing the module getpass
, like this:
import getpass
.
.
.
pa = getpass.getpass()
回答2:
Use the hashlib library of Python to take the MD5 hash of the input, and compare it against a hashed version of your password in the script. Here's an example of how you could do it.
回答3:
Never store userId and password in a source file, that's security vulnerability!
Store them in a text file and encrypt them with some symmetric key cryptography (at least MD5, or the currently suggested min standard, the SHA-3) to encode the password.
The result would look something like:
:admin:$1$dqx/Wdy5$QQrH98XjvFBOm6vqu3qN/1::Administrator:admin:changeme@example.com:
In your code block your read the file and use the same algorithm to decrypt the password.
来源:https://stackoverflow.com/questions/18290340/hiding-raw-input-password-input