Python 隐藏 raw_input() 密码输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18290340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hiding raw_input() password input
提问by Steven Sharman
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.
我想隐藏我的密码,但我不知道如何。我已经看到了show="*"
,getpass
但我不知道如何将它们放入此代码中。我正在使用 Python 2.7.3 并在 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()
回答by Stephen Whitworth
回答by tamasgal
getpass
hides the input, just replace raw_input
after importing the module getpass
, like this:
getpass
隐藏输入,只需raw_input
在导入模块后替换getpass
,如下所示:
import getpass
.
.
.
pa = getpass.getpass()
回答by gabor_the_kid
Never store userId and password in a source file, that's security vulnerability!
永远不要将用户 ID 和密码存储在源文件中,这是安全漏洞!
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.
将它们存储在文本文件中,并使用一些对称密钥密码术(至少是 MD5,或当前建议的最小标准,SHA-3)对它们进行加密以对密码进行编码。
The result would look something like:
结果将类似于:
:admin:$1$dqx/Wdy5$QQrH98XjvFBOm6vqu3qN/1::Administrator:admin:[email protected]:
:admin:$1$dqx/Wdy5$QQrH98XjvFBOm6vqu3qN/1::Administrator:admin:[email protected]:
In your code block your read the file and use the same algorithm to decrypt the password.
在您的代码块中,您读取文件并使用相同的算法来解密密码。